Quiz Entry - updated: 2026.06.20
What are VIM macros and how do you use them?
A macro records a sequence of keystrokes (q + a letter to start, q to stop) so you can replay it with @letter on many lines.
Macros are vim's answer to "do this same edit 50 times." You record once into a named register, then @a replays it; 10@a runs it ten times. The trick to a reusable macro is making each repetition self-positioning — end the recording by moving to the next line (e.g. j), so replaying naturally marches down the file.
Recording a macro:
q+ letter (e.g.,qa) - Start recording to register 'a'- Perform your actions
q- Stop recording
Playing a macro:
@a- Play macro from register 'a'5@a- Play macro 5 times@@- Repeat last macro
Example workflow:
qa " Start recording to 'a'
I " Insert at line start
# TODO: " Type the prefix
Esc " Back to Normal
j " Move to next line
q " Stop recording
10@a " Apply to next 10 lines
Tip: Macros are powerful for bulk editing tasks like reformatting data or adding boilerplate to multiple lines.