【发布时间】:2011-12-13 14:52:03
【问题描述】:
我想将当前缓冲区的内容发送到外部命令(如邮件)的标准输入。
如何将 Vim 缓冲区发送到外部命令?
【问题讨论】:
-
将选定文本作为
STDIN提供给shell 命令的相关问题:Pipe to shell and receive output on info line 和Replacing the selected original text with the output
我想将当前缓冲区的内容发送到外部命令(如邮件)的标准输入。
如何将 Vim 缓冲区发送到外部命令?
【问题讨论】:
STDIN 提供给shell 命令的相关问题:Pipe to shell and receive output on info line 和Replacing the selected original text with the output
您可以使用:w !cmd 将当前缓冲区写入外部命令的标准输入。来自: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
:%!gofmt 而不使用最后一个 %,因为这会导致它使用保存的版本(可能与当前缓冲区不同)
u 即可撤消
:%!my command 上查看过很多文档,但发现非常很少。这方面的文档在哪里?
以下是如何从命令行将当前缓冲区发送到外部标准输入的示例:
vim -es +"w >> /dev/stdout" -cq! /etc/hosts
它对脚本很有用。
更多命令行技巧,请查看:
【讨论】: