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

How do I edit a macro without re-recording it?

Answer

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

Explanation

Vim macros are stored as plain text in named registers, which means you can inspect and modify them directly using :let and :echo. This is far faster than re-recording a long macro just to fix a single keystroke.

How it works

  • :echo @q — print the raw contents of register q so you can see what was recorded
  • :let @q = '...' — overwrite register q with any string you write
  • The string is executed as if you had typed those keystrokes, so standard Vim notation applies (e.g., \r for Enter, \e for Escape)

A common pattern is to pull the register into the command line for editing:

:let @q = substitute(@q, 'foo', 'bar', '')

Or paste the macro contents into a buffer, edit it, then yank it back:

"qp          " paste register q into the buffer
" ... edit the pasted text ...
0"qy$        " yank the line back into register q

Example

Suppose you recorded @q to insert a line of dashes, but made a typo:

:echo @q
I---\r

You can fix it without re-recording:

:let @q = 'I====\r'

Now @q inserts ==== instead.

Tips

  • :reg q shows the register contents in a human-readable format with visible control characters
  • Registers az all work the same way — this technique applies to any named register
  • Uppercase append-register: using @Q (capital) appends to register q rather than replacing it

Next

How do I get just the filename without its path or extension to use in a command?