Bruno Arine

How to insert shell commands output in Vim

Today I learned that you can grab the output of any shell command and insert it directly into Vim.

To do that, type:

:r! <command>

The command above is a shorthand for :read !<command>.

I was interested in this feature after reading about how to interact with LLMs using the command line using Simon Willison’s CLI tools.

The reverse path is also possible (sending selected text in visual mode to an external command). For this, use the following (ugly and unpractical) command:

:'<, '> w !<command>

Breaking it down:

  • '<, '> tells Vim to use the entire selection while in visual mode.
  • w sends the text somewhere else—in this case, it will pipe the visual content to !<command>. It could be a filename though.