vimtricks.wiki Concise Vim tricks, one at a time.

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

  1. Open a scratch line (or a new buffer).
  2. "qp — paste the contents of register q onto the page as plain text. You will see the raw keystroke sequence (e.g., 0cwHello<Esc>j).
  3. Edit the text normally with any Vim commands.
  4. Visually select the corrected text with V and yank it back into register q: "qy (or "qd if you want to remove the scratch line).
  5. The macro in register q now 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 q shows the current contents of register q without pasting.
  • Non-printable keys appear as ^[ (Escape), ^M (Enter), etc. — you can type them with <C-v><Esc> in insert mode.

Next

How do I visually select a double-quoted string including the quotes themselves?