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

How do I capture command output into a register?

Answer

:redir @a | command | redir END

Explanation

The :redir command redirects Vim's command output to a register, file, or variable. This lets you capture and manipulate output programmatically.

How it works

  • :redir @a starts redirecting output to register a
  • Run any Ex command that produces output
  • :redir END stops redirecting
  • "ap pastes the captured output

Example

Capture the output of :marks into register a:

:redir @a
:marks
:redir END
"ap

Tips

  • :redir >> file.txt appends to a file
  • :redir => variable captures to a Vimscript variable
  • Clear the register first: :let @a = ''
  • :redir @A appends to register a (uppercase appends)
  • Useful for scripting and debugging

Next

How do you yank a single word into a named register?