How do I fix a typo in a recorded macro without re-recording it?
Answer
:let @q = substitute(@q, 'old', 'new', 'g')
Explanation
When a recorded macro has a small mistake, you don't have to re-record the entire thing. Because macros are stored as plain text in registers, you can edit them programmatically with :let and substitute() — the same way you'd do a find-and-replace on a string in Vimscript.
How it works
@qrefers to the contents of registerq(the macro storage)substitute(@q, 'old', 'new', 'g')replaces all occurrences of the old keystrokes with new ones:let @q = ...writes the modified string back into registerq- The macro is immediately available for playback with
@q
:let @q = substitute(@q, 'old', 'new', 'g')
Example
You recorded a macro in @q that runs :%s/funciton/function/g<CR> but you misspelled the replacement. Instead of re-recording, run:
:let @q = substitute(@q, 'funciton', 'function', 'g')
Or, if you want to inspect and hand-edit the macro:
1. "qp ← paste macro contents below cursor
2. edit the line manually
3. "qyy ← yank the line back into register q
Both approaches leave you with an updated macro ready to run with @q.
Tips
- Use
:echo @qfirst to inspect the raw macro content before editing - Special keys like Enter are stored as
\rand Escape as\x1bor^[— you can substitute those too - Append to a macro with uppercase register:
"Qqresumes recording and appends to registerq - Use
:reg qto view the macro content in a readable format