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
:putreads from a register and pastes it below the current line=is the expression register — instead of a named register, it evaluates a Vimscript expressionexecute('{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 ENDfollowed by:put a- Combine with a range to replace selected lines: first
dthe lines, then:put =execute(...)