How do I edit a recorded macro without re-recording it from scratch?
Answer
"qp
Explanation
Macros are stored as plain text in named registers. If you record a long macro and then make a mistake, you don't need to start over — you can paste the macro into a buffer, edit it directly as text, and then yank it back into the register. This technique treats macros as first-class, editable data.
How it works
- Open a scratch line (or a new buffer).
"qp— paste the contents of registerqonto the page as plain text. You will see the raw keystroke sequence (e.g.,0cwHello<Esc>j).- Edit the text normally with any Vim commands.
- Visually select the corrected text with
Vand yank it back into registerq:"qy(or"qdif you want to remove the scratch line). - The macro in register
qnow contains your edited version.
Example
" Step 1: Paste macro q into the buffer
"qp
→ 0cwfoo<Esc>j
" Step 2: Fix a typo — change "foo" to "bar"
/foo<CR>cwbar<Esc>
→ 0cwbar<Esc>j
" Step 3: Select the line and yank back into register q
V"qy
" Step 4: Run the corrected macro
@q
Tips
- Use
:let @q = '...'to set a register from the command line directly. :reg qshows the current contents of registerqwithout pasting.- Non-printable keys appear as
^[(Escape),^M(Enter), etc. — you can type them with<C-v><Esc>in insert mode.