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

How do I edit a recorded macro by modifying it as text in a buffer?

Answer

:put a ... edit ... "ayy

Explanation

Recorded macros are stored as plain text in registers, but editing them by re-recording is tedious for complex sequences. A much faster approach is to paste the macro into a buffer as editable text, modify it, and yank it back into the register.

How it works

  1. Paste the register: :put a dumps the contents of register a onto a new line
  2. Edit the text: Modify the keystroke sequence using normal editing commands
  3. Yank it back: Move to the line and type 0"ay$ to yank the contents (without the trailing newline) back into register a
  4. Clean up: Delete the scratch line with dd

The macro is now updated with your edits.

Example

You recorded a macro in register q that surrounds a word with parentheses:

Register q contains: bi(<Esc>ea)<Esc>

You want to change it to use square brackets instead. Paste it:

:put q

This gives you a line like bi(<Esc>ea)<Esc>. Edit the ( to [ and ) to ], then yank it back:

0"qy$
dd

Now @q surrounds words with square brackets.

Tips

  • Use 0"ay$ (not "ayy) to avoid capturing the trailing newline, which would add an unwanted <CR> to your macro
  • Special keys appear as literal characters (e.g., ^[ for <Esc>) — do not type the two characters ^ and [, keep the single special character
  • You can also use :let @a = '...' for simple edits, but the buffer method is better for complex macros with special characters

Next

How do I ignore whitespace changes when using Vim's diff mode?