Replaying macros
Once you’ve recorded a macro, you can replay it by typing @, followed by the name of the macro, in normal mode. For example — if you record a macro called a with qa, then you can replay that macro with @a in normal mode.
It’s easier to see the magic of macros in practice, so let’s try this in the editor. Work through the checklist below: record a macro called a that puts an X at the start and the end of the cursor line, then move to the second line and replay the whole edit with @a.
Wow! Did you see that? The macro did all the hard work for us!
Now that we know the basics of how to use macros, we’ll delve into them in a bit more detail in the coming sections.
1 2 3 4 5 6 7
Put your cursor on the first line Jump to the top of the buffer — press gg.
Start recording a macro called a Press qa — Vim will show recording @a in the bottom-left.
Prefix the line with an X Press IX, then Esc to return to normal mode.
Append an X as well Press AX, then Esc again.
Stop recording Press q — the recording @a flag will disappear.
Move to the second line Press j.
Replay the macro Press @a and watch both edits happen in one go.
Next: Start recording a macro called a
Next: Prefix the line with an X
Next: Append an X as well
Next: Stop recording
Next: Move to the second line
Next: Replay the macro
Last one — goal check runs when it's done
Nice work!
Up next: What's in a name?.