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

How do I edit a recorded macro to fix a mistake without re-recording it?

Answer

"ap, edit, 0"ay$

Explanation

When a macro has a small mistake, re-recording the entire thing is tedious. Instead, you can paste the macro contents into a buffer, edit the text directly, and yank it back into the register. This gives you full editing power over your macros.

How it works

  1. "ap — paste the contents of register a into the buffer as text
  2. Edit the pasted text to fix the macro (the text represents the keystrokes)
  3. 0"ay$ — re-yank the corrected line back into register a
  4. Now @a executes the fixed macro

Example

Suppose register a contains a macro that does Iprefix: ^[A;^[j (where ^[ is Escape). After pasting with "ap, you see:

Iprefix: ^[A;^[j

You notice the prefix should be "tag: " instead. Edit the line:

Itag: ^[A;^[j

Then 0"ay$ yanks the fixed version back into register a.

Tips

  • Control characters like <Esc> appear as ^[ in the buffer — do not delete these
  • To type a literal <Esc> in insert mode, press <C-v><Esc>
  • Use :let @a = 'your macro text' as an alternative way to set register contents
  • "qp and 0"qy$ work the same way for any register letter

Next

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