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

How do I insert the output of any Vim Ex command directly into the current buffer?

Answer

:put =execute('{cmd}')

Explanation

The :put =execute('{cmd}') idiom inserts the output of any Vim Ex command as text in your buffer. It is a clean, modern alternative to the verbose :redir workflow and is invaluable for capturing :messages, :scriptnames, :highlight, or any other command output for inspection or editing.

How it works

  • :put reads from a register and pastes it below the current line
  • = is the expression register — instead of a named register, it evaluates a Vimscript expression
  • execute('{cmd}') runs the given Ex command and returns its output as a string
  • Together, the output string is placed into the buffer as new lines

Example

To dump all loaded scripts into the current buffer:

:put =execute('scriptnames')

Result — the buffer receives lines like:

  1: /usr/share/vim/vim90/syntax/syntax.vim
  2: /usr/share/vim/vim90/syntax/synload.vim
  3: ~/.vim/autoload/plug.vim
  ...

Other practical uses:

:put =execute('messages')       " paste recent error/info messages
:put =execute('highlight')      " paste all highlight group definitions
:put =execute('set all')        " paste all current option values

Tips

  • To insert above the current line instead of below, move to line 0 first: :0put =execute(...)
  • In Vimscript, use split(execute('cmd'), '\n') to get output as a list for programmatic processing
  • execute() was added in Vim 8.0 / Neovim. The older equivalent requires :redir @a | cmd | redir END followed by :put a
  • Combine with a range to replace selected lines: first d the lines, then :put =execute(...)

Next

How do I search for and highlight text that exceeds a specific column width like 80 or 120?