我想将当前缓冲区的内容发送到外部命令(比如邮件)的标准输入中。
如何将Vim缓冲区发送到外部命令?
你可以使用 :w !cmd
将当前缓冲区写入外部命令的标准输入(stdin)。参考文档 :help :w_c
:
:w_c :write_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 :!.
另一个相关的命令是:%!cmd
,它执行相同的操作,然后用命令的输出替换当前缓冲区。因此,:%!sort
会调用外部排序命令来原地对当前缓冲区进行排序。从:help :range!
中得到:
:{range}![!]{filter} [!][arg] :range!
Filter {range} lines through the external program
{filter}. Vim replaces the optional bangs with the
latest given command and appends the optional [arg].
Vim saves the output of the filter command in a
temporary file and then reads the file into the buffer
tempfile. Vim uses the 'shellredir' option to
redirect the filter output to the temporary file.
However, if the 'shelltemp' option is off then pipes
are used when possible (on Unix).
When the 'R' flag is included in 'cpoptions' marks in
the filtered lines are deleted, unless the
:keepmarks command is used. Example:
:keepmarks '<,'>!sort
When the number of lines after filtering is less than
before, marks in the missing lines are deleted anyway.
:'<,'>!python -mjson.tool
或:%!python -mjson.tool
。 - TrinitronX:%!gofmt
命令,去掉最后一个 %
,因为这会导致其使用已保存的版本(可能与当前缓冲区不同)。 - thomasrutteru
撤消即可。 - törzsmókusvim -es +"w >> /dev/stdout" -cq! /etc/hosts
这对编写脚本非常有用。
更多的命令行技巧,请查看: