Learn Practice Daily golf Reference

How to get multiple cursors in Vim

Vim has no multiple cursors built in. To change every copy of a word, put the cursor on it, press *, then cgn, type the new word and Esc; each . changes the next one.

Multiple cursors let you place a cursor on each thing you want to change and type once. Vim and Neovim don't have them, and most long-time users don't miss them, because Vim's answer is to make the change once and repeat it: with . at each place, with a block selection, with a command over a range, or with a macro. Each is shown below, starting with the one closest to "select the next match and edit them all".

Try it here

Rename a variable with cgn

The cursor is on count. Press *: it searches for the word and jumps to the next copy. cgn changes that match; type total and Esc. Then . . does the next two, wrapping round to the top.

1 of 2* then cgn, type total, Esc
1let count = 0;
1for (const item of items) {
2 count += item.qty;
3}
4console.log(count);
NORMALLoading the editor…1/20.0s1:5

This editor needs a keyboard. Read on, and play it at your computer.

You want toIn Vim
change every copy of a word, choosing each one* then cgn, type, Esc, then . to change the next or n to skip it
type the same thing at the start of several linesCtrl-v, move down to select a column, I, type, Esc
type at the end of several lines of different lengthsCtrl-v, select down, $, A, type, Esc
change every copy of a word in the file at once:%s/\<count\>/total/g ↵ (add c to confirm each)
run the same keys on each selected lineV to select, then :normal A; ↵ (Vim types the range for you)
make a longer edit on many linesrecord it once with qa ... q, then @a or :normal @a on a range

Block selection: a column of cursors

The nearest thing to a cursor on every line is Visual Block mode. Press Ctrl-v, move down with j (or a count, 3j), and you have a column selected. I inserts in front of it: type the text and, when you press Esc, it appears on every line. A appends after the block, and $ before it makes the block reach each line's end, however long. c replaces the selected column on every line, and d deletes it.

It only works on the same column of neighbouring lines. For places scattered around a file, use cgn and . or a substitute.

Plugins

If you want real multiple cursors in Vim or Neovim, the vim-visual-multi plugin (mg979/vim-visual-multi on GitHub) adds them, with its own keys; its README covers setup. Learn cgn and Ctrl-v first, though: they work everywhere Vim keys do, with nothing to install.