Repetition with . is limited to the last change. Complex multi-step sequences need more powerful automation.
Macros record and replay sequences of Normal-mode commands using registers (a-z).
Recording workflow:
qa— start recording to registera(showsrecording @a)- Perform the sequence of commands
q— stop recording@a— replay the macro@@— replay last executed macro
Example: Numbered list
- Place cursor on line with
1 qaYp<C-a>q— record: yank line, paste, increment number, stop@a— writes 2 below 1@@— writes 3 below 2100@@— creates 1 through 103
- Registers are named a-z (26 total)
- Uppercase registers append:
qAappends to registera - Macros persist until overwritten
- Works across files if registers are preserved
- Macros execute at Normal mode — recording in Insert mode includes literal keystrokes
<C-a>increments if cursor is on/on after a number:regshows all register contents
- Repetition — Simpler than macros for single changes
- Vim Modes — Recording works in Normal mode
- Visual Selection — Can record visual selections
- Block Selection — Can be combined with macros