How do I execute Ex commands stored in a register instead of replaying it as normal-mode keys?
Answer
:@q
Explanation
Most macro workflows focus on @q, which replays register q as normal-mode keystrokes. But Vim also supports :@q, which executes the register contents as Ex commands. This is powerful when your automation is mostly command-line operations such as substitutions, quickfix actions, writes, or command chaining.
Using :@q lets you keep reusable command pipelines in registers and invoke them on demand, without wrapping everything in user commands or functions. It is especially helpful for one-off maintenance jobs where you need script-like behavior but want to stay in an interactive editing session.
How it works
:@q
:enters Ex command mode@qtells Vim to read registerqand execute it as Ex commands- Register
qshould contain valid Ex command text (often multiple commands separated by newlines)
Example
Suppose register q contains:
%s/\s\+$//e
update
Running :@q will:
1) remove trailing whitespace in the current buffer
2) write the file only if it changed
Tips
- Populate the register with
:let @q = "cmd1\ncmd2"for repeatable command scripts - Use
:reg qto inspect what will run before executing it - Prefer
@q(normal macro) when you need cursor-motion editing, and:@qwhen you need Ex-command orchestration