Piping buffer to external command in Vim

℡╲_俬逩灬. 提交于 2019-12-17 10:13:14

问题


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.


回答1:


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.




回答2:


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:

  • How to write whole buffer to standard output from the command line?


来源:https://stackoverflow.com/questions/7867356/piping-buffer-to-external-command-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!