How to repeat an edit on multiple lines in Vim

vim

I'm aware that in Vim I can often repeat a command by simply adding a number in front of it. For example, one can delete 5 lines by:

5dd

It's also often possible to specify a range of lines to apply a command to, for example

:10,20s:hello:goodbye:gc

How can I perform a 'vertical edit'? I'd like to, for example, insert a particular symbol, say a comma, at the beggining (skipping whitespace, i.e. what you'd get if you type a comma after Shift-I in command mode) of every line in a given range. How can this be achieved (without resorting to down-period-down-period-down-period)?

Best Answer

Ctrl-v enters visual mode blockwise. You can then move (hjkl-wise, as normal), and if you want to insert something on multiple lines, use Shift-i.

So for the text:

abc123abc
def456def
ghi789ghi

if you hit Ctrl-v with your cursor over the 1, hit j twice to go down two columns, then Shift-i,ESC , your text would look like this:

abc,123abc
def,456def
ghi,789ghi

(the multi-line insert has a little lag, and won't render until AFTER you hit ESC).