How do I record and replay a macro in Vim?
Answer
qa ... q ... @a
Explanation
Macros let you record a sequence of commands and replay them. Press qa to start recording into register a, perform your edits, press q to stop, then @a to replay.
How it works
qastarts recording all keystrokes into registera- Perform any sequence of Vim commands
qstops recording@areplays the recorded macro@@repeats the last played macro
Example
To wrap each line in quotes:
qa— start recordingI"— insert"at the beginning of the lineA"— append"at the endj— move to the next lineq— stop recording5@a— replay 5 times for the next 5 lines
Tips
- Use any register
athroughz— you can have 26 macros stored at once - Prefix with a count:
100@areplays the macro 100 times - Edit a macro by pasting the register (
"ap), modifying the text, and yanking it back ("ayy) - Recursive macros:
qAappends to an existing macro in registera