How do I edit a macro without re-recording it?
Answer
:let @q = substitute(@q, 'old', 'new', '')
Explanation
Vim macros are stored as plain text in named registers, which means you can inspect and modify them directly using :let and :echo. This is far faster than re-recording a long macro just to fix a single keystroke.
How it works
:echo @q— print the raw contents of registerqso you can see what was recorded:let @q = '...'— overwrite registerqwith any string you write- The string is executed as if you had typed those keystrokes, so standard Vim notation applies (e.g.,
\rfor Enter,\efor Escape)
A common pattern is to pull the register into the command line for editing:
:let @q = substitute(@q, 'foo', 'bar', '')
Or paste the macro contents into a buffer, edit it, then yank it back:
"qp " paste register q into the buffer
" ... edit the pasted text ...
0"qy$ " yank the line back into register q
Example
Suppose you recorded @q to insert a line of dashes, but made a typo:
:echo @q
I---\r
You can fix it without re-recording:
:let @q = 'I====\r'
Now @q inserts ==== instead.
Tips
:reg qshows the register contents in a human-readable format with visible control characters- Registers
a–zall work the same way — this technique applies to any named register - Uppercase append-register: using
@Q(capital) appends to registerqrather than replacing it