Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat some action certain times on Vim?

Tags:

vim

macros

repeat

In Vim, I usually want to repeat some series of commands some times. Say, I want to comment 5 lines, I would use

I//<Esc>j .j.j.j.j 

Is there any way to repeat the last ".j" part several times?

like image 975
Martín Fixman Avatar asked Jul 13 '10 20:07

Martín Fixman


People also ask

How do you repeat an action in Vim?

vim Normal mode commands (Editing) Repeat the Last Change Your cursor will be at position 1 of line 1, and all you need to do to fix the next two lines is press j. twice - that is, j to move down a line and . to repeat the last change, which was the addition of the I .

How do you repeat a macro in vim?

To replay the macro once, move the cursor to the next line and press @h where h represents the register on which you saved the macro. Notice that the macro automatically moves the cursor to the next line as required. This allows you to repeat its execution. To repeat the macro execution, press @@.

Does vim do previous commands?

Hit the colon ( : ) and then use the up arrow to start going back through previous commands. You can use the up/down arrows too to move around the list.

Does vim have the same operation on multiple lines?

Use ↑ / ↓ / j / k to select multiple lines. Press Shift + i and start typing what you want. After you press Esc , the text will be inserted into all the lines you selected.


1 Answers

One way to do this is to assign your key sequence to a macro, then run the macro once followed by the @@ run-last-macro command. For example:

qa.jq@a@@ 

If you know how many times you want to repeat the macro, you can use 4@@ or whatever.

like image 90
Greg Hewgill Avatar answered Sep 19 '22 23:09

Greg Hewgill