4

Let's say you have this text:

sed -n '1,4p'

Commas are used in every sed expression to target a range.

It's part of some notes i have...

The cursor is right behind the to, but I want to get the cursor to the comma in '1,4p'. What is the quickest way to do that? I typically have stayed with nano because i can set it up just to click to that spot, but i'm wondering how a professional using vim would do that. Would they just prefer search and replace to that type of fine-tuned navigation?

3 Answers 3

10

What is the quickest way to do that?

If such things as "purity" or "efficiency" are important to you, then the fastest native method is:

?,<CR>

where:

  • :help ? starts backward search,
  • , is your target,
  • <CR> is the Enter key.

See also :help 'incsearch'.

pure

If you don't care too much about purity, you can enable mouse support in your vimrc:

set mouse=a

and just use your mouse/trackpad.

impure

The first method would be the preferred native method because it is…

  • deterministic: no matter where the cursor is, the effort to get to the target is either constant (best case) or growing linearly (worst case),
  • precise: the cursor ends up exactly where you want it to be,
  • fast: with incsearch on and possibly a little sprinkling of :help c_ctrl-g, the time to get to the target is equal to the time to express your intent,
  • expressive: you basically tell Vim what to do and it does it.

The second method is rather intuitive and familiar. And it works. But pointing devices are also imprecise, and the farther the target, the harder and slower it is to acquire it. If you look at the screen capture, you will see that the pointer starts in the general direction of the target and then proceeds to make a number of adjustments to its trajectory until it lands on the target. This is too slow and random.

That sounds a bit like nitpicking but that's the kind of lossy interactions seasoned Vim users notice and try to avoid as much as possible.

3
  • That is what i was looking for...both vim and nano are rough to use within a GUI, but overall nano is much simpler. I'll test that out sometime today and respond if i have issues. Dec 4, 2022 at 16:08
  • If such things as "doing things by the book" or "efficiency" are important to you, If vim were not more efficient than other text editors, there would be no reason to stick with it, imo :-) [especially if people have to turn off the system since they can't get out of vim] I think Bram Moolenar has a video where he says that he observes what he does repetitively and tries to automate it and doing that is much easier in vim than in any other editor.
    – Tryer
    Dec 4, 2022 at 16:48
  • i agree with you @Tryer, the end goal with any IDE or text editor should just be speed and ease of use. Vim is really rough on your noggin but i haven't found a text editor that has as quick a way of searching and navigating documents as vim does. Dec 4, 2022 at 17:50
4

kkb puts you right after. kkbb if you really want to be right on it. Just regular directional movement. Stays more in the home row. No shifting needed.

To include Kevin's comment, you can also use kkF,. F jumps back in the same line to the given character. Those class of movements are f, which finds a character forward in the line, t, which moves forward 'till the given character on the line (stopping one character before), and F and T which do the same but moving backwards. These commands exist by default, but only move within the same line. The plugin suggested in Alex's answer extends them to function like / and ?, searching the whole buffer.

3
  • so after kk we would be on the "p" in the document, why? Dec 4, 2022 at 18:52
  • 1
    @thinksinbinary No, you'd be on the '. Vim tries to keep you in the same column as you switch lines. If it can't because there's less columns, then you end up on the last column of the line.
    – JoL
    Dec 4, 2022 at 19:15
  • 1
    See also the f and t motions (and F and T for going backwards), which are often handy for longer and more complicated texts, especially in conjunction with ; and ,.
    – Kevin
    Dec 4, 2022 at 20:10
2

You could use the plugin vim-sneak with the following in your .vimrc (see the vim-sneak readme):

map f <Plug>Sneak_f
map F <Plug>Sneak_F
map t <Plug>Sneak_t
map T <Plug>Sneak_T

(Only the second line is strictly speaking relevant to your question, but the rest make the other search motions move across lines rather than working only in a single line.)

Then you can simply type F,. This will place the cursor on the , in your example. To jump further up to other commas in a larger file, you would simply repeat by pressing ;.

4
  • so that plugin brings the cursor to the closest comma? Dec 3, 2022 at 23:02
  • vim-sneak does a number of things. Its main feature is to offer a motion like f, mapped by default to s, which is followed by two characters instead of one and jumps to the next instance (or previous instance with S) of those two characters appearing in sequence, whether or not there is a line break in between. The plugin also defines versions of fFtT, left unmapped by default, that do what the default fFtT do except they move across lines (plus a couple visual enhancements). Map them with those four lines in your .vimrc and fFtT will move across lines. Dec 4, 2022 at 4:47
  • See <github.com/justinmk/vim-sneak>, and especially <github.com/justinmk/…>. I'll add the latter link to the answer. Dec 4, 2022 at 4:49
  • 1
    But to clarify, the plugin (and the default F command) has nothing to do with commas per se. To go back to your example, if you type F-, the cursor would jump to the - in your first line. For the default f and F commands, see :help f and :help F. Dec 4, 2022 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.