How can I pipe the output of a shell command into a new buffer in Vim? The following obviously wouldn't work, but you can see what I'm getting at:
:!echo % | :newtab
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
You can't pipe the output of a shell command into a command that creates a new buffer, but you can create a new buffer and read the output of a shell command into that buffer with one entry on Vim's command line. A working version of your example would be
:tabnew | r !echo <c-r>=bufname("#")<cr>
Note that the pipe symbol in this case is a separator between Vim ex commands, not the shell's pipe. See also
:help :tabnew :help :r! :help :bar
Here's what I do. It's alluded to in comments in the above answers.
:new | r ! <cmd> :new | r ! <cmd> # (# is replaced with filename)
Example 1: Find all text files in
:new | r ! find /tmp -name '*.txt'
Example 2: You're editing file foo.txt and you want to run
ls -la foo.txt and get the output in a buffer:
:new | r ! ls -la #
# is replaced with the filename of the original buffer you're editing. This is particularly useful for ad-hoc source control commands e.g.
:new | r ! hg annotate -un #
:new creates a horizontal split, use
:vnew if you want a vertical split instead and
:tabnew for a new tab.
:tabnew | enew | r ! <your shell cmd>
works for me.
If you really require to store the result in a new buffer, but require info from the old current buffer, then you can either use
:let res = system('echo '.expand('%')) :tabnew :put=res
or store the current buffer name for later:
:let bn = expand('%') :tabnew | :r!echo <c-r>=bn<cr>