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

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

Answer

:let @a = "content"

Explanation

When a recorded macro has a typo or needs a small tweak, you don't have to re-record it entirely. You can read the macro into a buffer, edit it as plain text, and write it back — or directly assign a new value with :let.

How it works

The most surgical approach is to use :let to assign a new string directly to the register:

:let @a = "new macro content"

To edit an existing macro, use the "paste and re-yank" workflow:

  1. Open a blank line (e.g., o then <Esc>)
  2. Paste the macro content with "ap
  3. Edit the text on that line
  4. Yank the line back into register a with "ayy or V"ay
  5. Delete the scratch line with dd

Alternatively, for simple single-line replacements:

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

Example

You recorded a macro that was supposed to add a semicolon and move down, but you forgot the j:

:let @a
" Shows: 'A;<Esc>' (missing the j)
:let @a = 'A;<Esc>j'

Now @a appends a semicolon and moves to the next line as intended.

Tips

  • Use :reg a to inspect the current contents of any register before editing
  • The :let @a = @a trick reads the register into vimscript notation, letting you see special characters like <Esc> as literal escape sequences
  • You can copy one macro into another: :let @b = @a
  • This technique works for any register, not just macro registers

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?