Piping buffer to external command in Vim

2019-01-10 06:46发布

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.

2条回答
爷的心禁止访问
2楼-- · 2019-01-10 07:13

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.

查看更多
祖国的老花朵
3楼-- · 2019-01-10 07:16

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:

查看更多
登录 后发表回答