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

How do I save my last Ex command into a register so I can replay it as a macro?

Answer

:let @q = @:

Explanation

The : register always holds the last Ex command you ran. Copying it into a named register with :let @q = @: promotes that command into a replayable macro. You can then fire it on demand with @q, run it across a range with :'<,'>norm @q, or chain it with other macros — all without re-typing the original command.

How it works

  • @: is the read-only register that stores the last command-line entry (the text after the :, without the leading colon)
  • :let @q = @: copies that text into register q
  • @q then executes the contents of q as an Ex command (because the stored text starts at the command level)
  • @@ re-runs whichever named register was last executed

Example

Say you just ran a targeted substitute:

:s/TODO: /DONE: /g

Rather than typing it again on another buffer, capture it:

:let @q = @:

Now switch to the next buffer and replay with:

@q

Or apply it to every line in a visual selection:

:'<,'>norm @q

Tips

  • @: alone also replays the last command directly — use :let @q = @: when you want to save it for later or apply it to a range
  • After saving to q, you can paste it with "qp, tweak the text, and pull it back with 0"qy$ to refine the command before replaying
  • Combine with @@ to re-run the most recently executed register without specifying its name each time

Next

How do I insert the entire current line into the command line without typing it?