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

How do I inspect what key sequences are stored in a macro register?

Answer

:echo strtrans(@q)

Explanation

When a macro behaves unexpectedly, :echo strtrans(@q) reveals exactly what is stored in register q—including invisible control characters—as human-readable escape sequences. This is the fastest way to debug a misbehaving macro without pulling it into a buffer.

How it works

  • @q — the contents of register q (works for any register az)
  • strtrans() — a built-in Vimscript function that converts control characters to printable form, e.g. <Esc> shows as ^[, <CR> as ^M, <C-a> as ^A
  • :echo — prints the result on the status line

Example

After recording qqciwHello<Esc>wq, inspect it:

:echo strtrans(@q)

Output:

ciwHello^[w

This immediately shows that <Esc> was stored as ^[ and confirms the motion w is at the end.

If you see unexpected characters such as an extra ^M (carriage return) or missing ^[ (escape), you know where the recording went wrong.

Tips

  • Use :reg q to see the raw stored value (Vim renders it slightly differently, e.g. ^[ vs <Esc>)
  • To print all registers at once: :reg — but strtrans() is better for seeing control characters in a specific register
  • After diagnosing the issue, pull the macro into a buffer with :put q, fix it, then yank it back: "qyy

Next

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