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

How do I run a one-off macro without recording by executing keystrokes from an expression register?

Answer

@='A;<Esc>'<CR>

Explanation

Recorded macros are powerful, but sometimes you need a quick ephemeral sequence and do not want to occupy a register. @= lets you evaluate an expression and execute the resulting keystrokes immediately as a macro. This is a high-leverage tool for ad hoc edits, especially when combined with repeat() or string building in Vimscript expressions.

How it works

  • @= enters expression-register execution mode for macros
  • 'A;<Esc>' returns a Normal-mode key sequence string
  • <CR> confirms and executes the produced sequence
  • In this example, A;<Esc> appends ; at end of line and returns to Normal mode

Example

Given:

const x = 1
const y = 2

On the first line, run:

@='A;<Esc>'<CR>

Result:

const x = 1;
const y = 2

Then move down and repeat with @@ to apply the same generated macro again.

Tips

  • Build dynamic one-offs by concatenating pieces, for example @='A' . ';' . '<Esc>'<CR>
  • Use this for one-shot edits when recording (qa...q) would be overkill
  • If mappings interfere, include only raw Normal keys you intend to execute

Next

How do I show the current buffer in a new tab without re-reading it from disk?