Piping buffer to external command in Vim

yasar picture yasar · Oct 23, 2011 · Viewed 28.8k times · Source

I am kind of a Vim novice. I would like to send contents of the current buffer to stdin of external command (lets say mail). My final purpose is to set a shortcut to quickly send email from current Vim buffer. I am guessing this should be a trivial stuff, but I couldn't find a way to send Vim buffer to an external command. Thanks in advance.

Answer

John Kugelman picture John Kugelman · Oct 23, 2011

You can use :w !cmd to write the current buffer to the stdin of an external command. From :help :w_c:

:[range]w[rite] [++opt] !{cmd}

Execute {cmd} with [range] lines as standard input (note the space in front of the '!'). {cmd} is executed like with ":!{cmd}", any '!' is replaced with the previous command |:!|.

A related command is :%!cmd which does the same thing and then replaces the current buffer with the output of the command. So :%!sort would invoke the external sort command to sort the current buffer in place.