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

How do I inspect a macro register with escaped control characters so I can debug it safely?

Answer

:echo string(getreg('q'))

Explanation

Macros can fail for subtle reasons: hidden control keys, extra whitespace, or unexpected register contents. :echo string(getreg('q')) gives you a debug-friendly representation of register q, including escaped control characters, instead of executing anything. This is much safer than trial-running a broken macro across real code.

How it works

  • getreg('q') returns the raw contents of register q
  • string(...) renders that value with escapes, making non-printing bytes visible
  • :echo prints the result to command output for inspection
  • You can then patch the macro with :let @q = ... instead of re-recording from scratch

Example

Suppose a macro unexpectedly exits insert mode too early. Inspect it:

:echo string(getreg('q'))

You might see escaped fragments that reveal why, such as an extra control sequence or an unwanted carriage-return token. After identifying the issue, update only the broken piece and retest on a scratch buffer.

Tips

  • Pair this with :echo getregtype('q') to confirm characterwise/linewise/blockwise register type
  • Use :let @q = substitute(@q, ... ) for targeted repairs
  • Keep a known-good macro snapshot in another register before making edits

Next

How do I save only real file buffers when running bufdo across many open buffers?