Hacker News new | ask | show | jobs
by ahipple 1434 days ago
> Do vim users know if there was anything like Select next occurrence in vim?

As noted in a sibling, you can press * (shift+8) in normal mode with your cursor over a word to find the next instance of the word. This also updates the search pattern so you can use n and shift+n to navigate forward and back between matches.

If you combine this with other bits of the vim "grammar" (which I do think encourages a kind of feature discovery, although I agree there's a large learning "hump" to get over to take advantage of it), you can use something like cgn to change the currently-focused search match, n to navigate to the next search match, and . to replay the command.

So while it's not exactly the same as multi-cursor, you can achieve something very similar (and just as fast) with the following sequence:

  1. In normal mode, place the cursor over the symbol you wish to edit
  2. Press key: \* (read: "find the next occurrence of the current symbol and update the search pattern to be that string")
  3. Press keys: cgn (read: "change the search match under the cursor")
  4. Type the new symbol
  5. Press ESC to return to normal mode
  6. Press keys: n. (read "find the next occurrence of the search pattern [set in step 2] and repeat the last command [the edit from steps 3-5]")
  7. Repeat 6 until all symbols are replaced
Or, in raw keystrokes, I might replace all six "foo"s in a document with "bar" by typing the following sequence of characters (having placed the cursor over a "foo"): *cgnbar<ESC>n.n.n.n.n.

It may seem complex compared to something like multi-cursor editing, but the nice thing here is that there are concepts here that are extensible beyond the scope of that specific edit, and can yield similar "speedy" workflows but on a much wider set of tasks.

- c[something] is a pattern that means "change [the given region]", so you could construct similar commands with different regions (`ciw` to change inside the current word, `ca{` to change the current curly-bracketed region (inclusive), and so on)

- [something]gn is a pattern that means "[do something] to the next search match", so you could delete it with `dgn`.

- . can replay many types of commands, so if I have a more complex sequence of edits to apply starting at a search match, I could do that too (not just simply replacing the symbol).

It's a very powerful paradigm.