How do I add more commands to a macro I already recorded?
Answer
qA
Explanation
If you finish recording a macro and realize you forgot a step, you don't need to re-record the whole thing. Using an uppercase register letter with q appends new keystrokes to an existing macro instead of overwriting it. So qA appends to the macro in register a.
How it works
qastarts recording a new macro into registera, replacing any previous contentsqA(uppercase) starts recording and appends new keystrokes to whatever is already in registera- Press
qagain to stop recording - When you replay with
@a, Vim executes the original keystrokes followed by the appended ones as a single seamless macro
Example
You record a macro that wraps a word in parentheses:
qa bi(<Esc> ea)<Esc> q
Register a now contains bi(<Esc>ea)<Esc>. But you realize you also want to move to the next line afterward. Instead of re-recording everything:
qA j q
Register a now contains bi(<Esc>ea)<Esc>j. The j was appended seamlessly.
Given the text:
alpha
beta
gamma
Running 3@a from the first word produces:
(alpha)
(beta)
(gamma)
Tips
- This works for all 26 named registers:
qBappends to registerb,qCto registerc, and so on - Use
:reg ato inspect the current contents of registerabefore and after appending - If the appended portion has a mistake, you can edit the register directly: paste it with
"ap, fix the text, then yank it back with"ayy - Appending is especially useful for building up complex macros incrementally — record the basic operation first, test it, then append refinements
- Remember that macro registers and yank registers are the same registers — appending with
qAalso appends to the yank registera, and vice versa with"Ayy - Combine with recursive macros: record
qa ... q, test it, then append the recursive call withqA @a q