Editing a vim macro
Sometimes you will make a mistake with a lengthy macro, but would rather edit it than re-record it entirely. You can do this using the following process:
Put the macro on an empty line with
If your macro is saved in register
a, the command is
Edit the macro as needed.
Yank the macro into the correct register by moving the cursor to the beginning of the line and using
You can re-use the original register or use another one. If you want to use register
b, the command is
"by$. or by using
"<register>d$(deletes the unused line)
Record and replay action (macros)
q command we could simplify a lot of tedious work in vim.
example 1. generate array sequence (1 to 20).
i to enter insert mode, input
STEP 2. Record following action: "append the last number to the next line, and increment the number"
escto exit input mode
qato enter record mode, using buffer
pto copy current line and paste it as the next line
ato increment number
qagain to finish record
STEP 3. Replay action 18 times.
18@a to replay action 3 and action 4 in step 2.
Recording a macro
One way to create a macro is to record it.
Start recording a macro and save it to a register (in this example, we'll use
a, but it can be any register you could normally yank text to):
Then run the commands you want to record in the macro (here, we'll surround the contents of a line with
When we're finished with the commands we want to record in the macro, stop the recording:
Now, any time we want to execute the recorded sequence of commands stored in
and vim will repeat the recorded sequence.
Next time you would like to repeat the last macro that was used you can double type
And as a extra bonus it is good to remember that if you put a number before a command it will repeat it that many times. So, you repeat the macro saved in register
a 20 times with:
Vim macros can also be recursive. This is useful for when you need to act on every line (or other text object) till the end of the file.
To record a recursive macro, start with an empty register. (A register can be emptied using
Choose a consistent starting point on each line to start and finish.
Before finishing recording, invoke the macro itself as the last command. (This is why the register must be empty: so it'll do nothing, as the macro doesn't exist yet).
Example, given the text:
In normal mode, with the cursor on the first line and a empty register
a, one could record this macro:
Then with a single invocation of
@a, all the lines of the file would be now inside double quotes.
What is a macro?
A macro is a series of keystrokes meant to be "played back" by Vim without any delay. Macros can be stored in registers or variables, bound to keys, or executed on the command line.
Here is a simple macro that uppercases the third
word on a line:
That macro could be recorded into register
or saved directly into register
to be played back with:
But it could also be typed directly in the command-line:
for instant playback via the
Or put into a variable:
to be played back with:
Or saved as a mapping:
to be played back by pressing
If you want to store a macro for later reuse you can type in insert mode:
This inserts the macro in register
q (in this example:
0wwgUiw). You can use this
output e.g. to define the macro in your
Doing so the register
q is initialized with this macro every time you start vim.