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

How do I preview a macro register with escaped keycodes before executing it?

Answer

:put =strtrans(@q)<CR>

Explanation

Macro failures are often caused by hidden control keys like <Esc>, <CR>, or tabs that are hard to see in raw register output. :put =strtrans(@q)<CR> prints register q into the buffer using escaped notation, so those invisible characters become readable.

This is safer than replaying a suspicious macro blindly. You can inspect exactly what will run, fix it, and only then execute with @q.

How it works

  • @q references the raw contents of register q
  • strtrans(...) converts non-printable bytes to visible escape sequences
  • :put =... inserts the evaluated expression result as a new line below the cursor

Because the output is plain text, you can yank it, edit it, compare versions, or stash it in notes while debugging a complex workflow.

Example

Suppose register q contains a macro with hidden keys:

qaI// <Esc>jA;<Esc>q

Run:

:put =strtrans(@q)<CR>

You might see:

I// ^[jA;^[

Now it is obvious where Escape appears and whether the macro has extra keystrokes.

Tips

  • Use :echo strtrans(@q) for a quick one-line preview in the command area
  • Pair with :let @q = '...' to rebuild a corrected macro deterministically
  • Inspect any register the same way, e.g. strtrans(@a)

Next

How do I run one substitution in nomagic mode so most regex characters are literal?