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

How do I fix a typo in a recorded macro without re-recording it?

Answer

:let @q = substitute(@q, 'old', 'new', 'g')

Explanation

When a recorded macro has a small mistake, you don't have to re-record the entire thing. Because macros are stored as plain text in registers, you can edit them programmatically with :let and substitute() — the same way you'd do a find-and-replace on a string in Vimscript.

How it works

  • @q refers to the contents of register q (the macro storage)
  • substitute(@q, 'old', 'new', 'g') replaces all occurrences of the old keystrokes with new ones
  • :let @q = ... writes the modified string back into register q
  • The macro is immediately available for playback with @q
:let @q = substitute(@q, 'old', 'new', 'g')

Example

You recorded a macro in @q that runs :%s/funciton/function/g<CR> but you misspelled the replacement. Instead of re-recording, run:

:let @q = substitute(@q, 'funciton', 'function', 'g')

Or, if you want to inspect and hand-edit the macro:

1. "qp        ← paste macro contents below cursor
2. edit the line manually
3. "qyy       ← yank the line back into register q

Both approaches leave you with an updated macro ready to run with @q.

Tips

  • Use :echo @q first to inspect the raw macro content before editing
  • Special keys like Enter are stored as \r and Escape as \x1b or ^[ — you can substitute those too
  • Append to a macro with uppercase register: "Qq resumes recording and appends to register q
  • Use :reg q to view the macro content in a readable format

Next

How do I complete identifiers using ctags from within insert mode?