Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I repeat an edit on multiple lines in Vim?

Tags:

vim

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).


:10,20s/^/,/

Or use a macro, record with:

q a i , ESC j h q

use with:

@ a

Explanation: q a starts recording a macro to register a, q ends recording. There are registers a to z available for this.


That's what the :norm(al) command is for:

:10,20 normal I,

If you are already using the '.' to repeat your last command a lot, then I found this to be the most convenient solution so far. It allows you to repeat your last command on each line of a visual block by using

" allow the . to execute once for each line of a visual selection
vnoremap . :normal .<CR>

I believe the easiest way to do this is

1) record a macro for one line, call it 'a'; in this case one types

q a I , ESC j q

2) select the block of lines that you want to apply the macro to

3) use the 'norm' function to execute macro 'a' over this block of lines, i.e.,

:'<,'>norm@a