How do I execute a Vimscript expression as a macro without recording it?
Answer
@=
Explanation
The @= command lets you type a Vimscript expression and execute the result as if it were a macro. Instead of recording keystrokes into a register, you dynamically build the keystroke sequence using any Vim expression. This is a powerful way to run one-off macros or computed keystroke sequences.
How it works
- In normal mode, press
@= - Vim opens the expression prompt (
=) - Type any expression that evaluates to a string of keystrokes
- Press
<CR>to execute those keystrokes as a macro
The expression can use string concatenation, variables, function calls, or any valid Vimscript.
Example
To delete the next 3 words by computing the command dynamically:
@='3dw'
To insert a line of dashes matching the length of the current line:
@='o' . repeat('-', col('$')-1) . "\<Esc>"
This opens a new line, inserts dashes equal to the current line's length, then escapes.
Tips
- Use
@@afterward to repeat the last expression macro - Unlike recorded macros,
@=expressions can use variables and functions for dynamic behavior - In insert mode,
<C-r>=evaluates an expression and inserts the result as text (different from@=which executes as keystrokes)