How do I capture command-line output directly into a register?
Answer
:redir @a | messages | redir END<CR>
Explanation
When debugging a session or building repeatable edits, it is useful to turn command output into editable text. Vim's :redir can redirect Ex command output into a register, which gives you a fast bridge from transient command-line information to normal-mode operations. This is powerful for advanced workflows where you want to inspect, transform, or paste diagnostic output without leaving Vim.
How it works
:redir @astarts redirecting command output into registera|chains commands on one Ex linemessagesprints the message history| redir ENDstops redirection and finalizes register contents
After this runs, register a contains the same text you would normally only see in the command area. You can inspect it with :reg a, paste it with "ap, or process it further.
Example
Run:
:redir @a | messages | redir END
Then paste the captured output into a scratch buffer:
"ap
Resulting text might look like:
Error detected while processing ...
line 12: E121: Undefined variable
Tips
- Use uppercase register names (for example
@A) if you want to append multiple captures into one log register. - Swap
messagesfor other commands that print useful output and keep the same redirection pattern.