Piping buffer to external command in Vim

心不动则不痛 提交于 2019-11-27 10:35:46

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.

kenorb

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:

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