Piping buffer to external command in Vim
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.
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.
Here is example how to send the current buffer to external stdin from the command line:
vim -es +"w >> /dev/stdout" -cq! /etc/hosts
It's useful for scripting purposes.
For more command-line tricks, check:
上一篇: VIM语法着色错误列表
下一篇: 在Vim中将缓冲区配管到外部命令