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

How do I debug a macro by executing it step by step?

Answer

Paste with "ap and execute keys manually

Explanation

To debug a macro, paste its contents into the buffer, read each keystroke, and execute them one at a time to find where the macro goes wrong.

How it works

  1. "ap — paste the macro register content
  2. Read each character/key sequence
  3. Manually execute each step on the target text
  4. Identify where the behavior diverges from expectations
  5. Fix and "add to store back

Example

Macro a contains I// ^[j (shown when pasted). You can see:

  • I enters insert mode at line start
  • // types the comment prefix
  • ^[ is Escape
  • j moves down

Tips

  • ^[ in the pasted output represents <Esc>
  • ^M represents <CR> (Enter)
  • :reg a shows the register contents without pasting
  • Consider rebuilding the macro from scratch if debugging is too complex
  • Test on a copy of your data before running macros on important files

Next

How do you yank a single word into a named register?