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

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 @a starts redirecting command output into register a
  • | chains commands on one Ex line
  • messages prints the message history
  • | redir END stops 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 messages for other commands that print useful output and keep the same redirection pattern.

Next

How do I enable matchit so % jumps between if/else/end style pairs?