How to repeat editing multiple lines in Vim?

I know that in Vim I often repeat the command, just adding the number in front of it. For example, you can delete 5 lines:

5dd 

You can also often specify a range of lines for applying a command, for example

 :10,20s:hello:goodbye:gc 

How can I do vertical editing? For example, I would like to insert a specific character, for example a comma, at the beginning (skipping spaces, that is, what you get if you enter a comma after Shift-I in command mode) of each line in a given assortment. How can this be achieved (without resorting to a period of down-down-down-down-down-down-period)?

+82
vim
Dec 10 '08 at
source share
8 answers

:10,20s/^/,/

Or use a macro recording with:

qai , ESC jhq

use with:

@ a

Explanation: qa starts recording a macro to register a , q ends recording. Registers a through z are available for this.

+78
Dec 10 '08 at 12:45
source share

Ctrl - v switches to visual mode by block. Then you can move ( h j k l -wise, as usual), and if you want to insert something onto several lines, use Shift - i .

So for the text:

 abc123abc def456def ghi789ghi 

if you press Ctrl - v with the mouse cursor at 1, press j twice to go down two columns, then Shift - i , ESC , your text will look like this:

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

(multi-line insert has a slight lag and will not be displayed until you press ESC ).

+86
Dec 10 '08 at 13:37
source share

What the command means: norm (al) for:

 : 10.20 normal I,
+44
Dec 18 '08 at 19:32
source share

If you are already using '.' to repeat your last command a lot, I found this the most convenient solution. This allows you to repeat the last command on each line of the visual block with

 " allow the . to execute once for each line of a visual selection vnoremap . :normal .<CR> 
+34
Nov 09 '11 at 11:53
source share

I find the easiest way to do this is

1) write a macro for one line, name it 'a'; in this case one type

qa I, ESC jq

2) select the line block that you want to apply to the macro

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

 :'<,'>norm@a 
+16
Nov 28 '09 at 11:21
source share

I think it’s easiest to record a macro and then repeat the macro as many times as you want. For example, to add a comma at the beginning of each line, type:

 qa I , ESC jq 

to repeat this 5 times, you enter

 5 @ a 
+10
Dec 10 '08 at 12:45
source share

I use block visual mode . This allows inserts / changes on multiple lines (for example, “vertical changes”).

+2
Jan 6 '09 at 23:33
source share

In addition to macros, as already mentioned, for the specific case of inserting a comma in a series of lines (for example, from line 10-20), you can do something like:

 :10,20s/\(.*\)/,\1 

That is, you can create a numbered group match with \ (and \) and use \ 1 in the replacement string to say "replace the contents of the match."

+1
Dec 10 '08 at 13:03
source share



All Articles