How do I edit a recorded macro without re-recording it from scratch?
Answer
:let @a = "content"
Explanation
When a recorded macro has a typo or needs a small tweak, you don't have to re-record it entirely. You can read the macro into a buffer, edit it as plain text, and write it back — or directly assign a new value with :let.
How it works
The most surgical approach is to use :let to assign a new string directly to the register:
:let @a = "new macro content"
To edit an existing macro, use the "paste and re-yank" workflow:
- Open a blank line (e.g.,
othen<Esc>) - Paste the macro content with
"ap - Edit the text on that line
- Yank the line back into register
awith"ayyorV"ay - Delete the scratch line with
dd
Alternatively, for simple single-line replacements:
:let @a = substitute(@a, 'old', 'new', 'g')
Example
You recorded a macro that was supposed to add a semicolon and move down, but you forgot the j:
:let @a
" Shows: 'A;<Esc>' (missing the j)
:let @a = 'A;<Esc>j'
Now @a appends a semicolon and moves to the next line as intended.
Tips
- Use
:reg ato inspect the current contents of any register before editing - The
:let @a = @atrick reads the register into vimscript notation, letting you see special characters like<Esc>as literal escape sequences - You can copy one macro into another:
:let @b = @a - This technique works for any register, not just macro registers