Edit multiple lines at once in Vim
Vim has a built-in way to visually edit multiple lines at once: visual block mode. And the vim-visual-multi plugin can give you an even more powerful way to edit multiple lines at once. Here’s the difference between them and some basics:
First, a quick refresher on the built in. If I press ctrl-v
, I can then select some lines with j
. Movements from here, such as w
to go to the next word, are now operating only based on the last line. Practically speaking, this limits the usefulness of such movements unless all your text is neatly aligned vertically. But one useful edit I routinely make with this mode is appending something to end of every line with $A
. Watch the demo:
With vim-visual-multi, movements operate independently on each line. This is what other editors call “multi cursor” and it’s a popular feature of VS Code. After installing the plugin, I can use ctrl-down
or ctrl-up
— the arrow keys. (Note that on macOS I needed to disable a default system mapping under System Preferences > Keyboard Shortcuts.)
This time, word movement with w
works like we would expect. And when I use Vim commands they will operate distinctly with each cursor. Like magic, everything we do works on every line at once.
In the demo below, I delete a character with x
and then use csw
from vim-surround to change the surrounding characters around the word.
This is really just scratching the surface of what the plugin can do. There is extensive documentation and a robust system of mappings. There is even mouse support — not something I will ever use but it could be a useful crutch for those coming to Vim from VS Code.