Recording and Playing Macros

qa records into register a. q stops. @a plays.

Keys: qa, q, @a, @@

qa starts recording into register a; press q again to stop. @a plays back. @@ replays the most recent macro.

Macros are Vim's loop. Record a sequence of keystrokes once; replay it with one key. They're how you turn 100 lines of repetitive editing into 4 keystrokes.

Start recording into register a
KeyNote
q
a
Stop recording
KeyNote
q
Play macro a
KeyNote
@
a
Repeat last macro
KeyNote
@
@
qa โ€” start recording
q โ€” stop recording
@a โ€” play macro
@@ โ€” repeat last macro

Worked example โ€” qa ... q then @a

Record once, play forever.

Step 1 ยท qa ยท qa โ€” start recording into a.
qa โ€” start recording into a.
Step 2 ยท I1. Esc ยท Prefix with '1. '.
Prefix with '1. '.
Step 3 ยท j0 ยท Move to next line (recorded).
Move to next line (recorded).
Step 4 ยท q ยท q โ€” stop recording.
q โ€” stop recording.
Step 5 ยท @a @a ยท @a twice โ€” replays on the next two lines.
@a twice โ€” replays on the next two lines.

Macros are just register contents. Open the register, edit it, paste it โ€” they're text.

โ–ถ Try this in the simulator

See also: Counted Macros, Editing a Macro, Macros Are Just Register Contents