How do I edit a recorded macro by treating it as plain text?
Answer
"qp {edit} 0"qy$ dd
Explanation
When you record a macro and realize it has a mistake, the easiest fix is to paste the macro's keystrokes as text, edit them, and yank the corrected version back into the register. This is far more practical than re-recording from scratch, especially for long macros.
How it works
"qp— paste the contents of registerqas text on the current line{edit}— make your corrections using normal editing commands (fix typos, insert missing keystrokes, delete extras)0"qy$— move to the start of the line, then yank from there to end of line back into registerqdd— delete the temporary helper line
The macro is now updated in register q and ready to run with @q.
Example
You recorded a macro in q that does Iprefix: <Esc>j. You pasted it and see:
Iprefix: <Esc>j
You realize it should be Iprefix: <Esc>0j (go to column 0 before moving down). Edit accordingly, then yank back.
Tips
- Use
:reg qto inspect the register before pasting so you know what you're working with - Non-printable keys appear as control characters (e.g.,
^[for<Esc>,^Mfor<CR>) — you can insert them with<C-v><Esc>,<C-v><CR>, etc. - This technique works with any named register, not just
q - For simple find-and-replace edits,
:let @q = substitute(@q, 'old', 'new', 'g')may be quicker