Kevin
Kevin

Reputation: 3431

How do I dump output of an external command to a new buffer in Vim?

:enew lets me create a new buffer and :.!<command> lets me dump the output of an external command to that buffer. Can I combine the two into a one liner?

Thanks.

Upvotes: 9

Views: 4252

Answers (3)

tkolleh
tkolleh

Reputation: 423

:vnew | read !<command>

Execute the command and place the output in a new vertically split buffer. Use # to pass the path of the current buffer to the command. e.g.

:vnew | read !python -m json.tool #

Using python's JSON module to format the working JSON file.

Alternatively, the AsyncCommand plugin can be used to "run any program and load results in a split" buffer asynchronously.

Additional reading:

  • :h new
  • :h read

Upvotes: 0

bobbogo
bobbogo

Reputation: 15483

:.! actually pipes the current line through the external command, possibly losing the line. You may wish to use :r !<command>—Note the space before the !. I often use :0r !cmd so that the output is inserted at the start of the buffer.

Upvotes: 3

Matt Briggs
Matt Briggs

Reputation: 42158

'|' is used to chain commands together in vim. so

:enew | .! <command>

should do what you want

Upvotes: 10

Related Questions