将vim用户定义的命令应用于视觉选择或全部

时间:2015-04-07 15:27:34

标签: vim

我的.vimrc

中有一个用户定义的命令
command! RemoveOutput :'<,'> ! grep -v 'OUT'

但是,只有在之前选择并再次取消选择时,这显然适用于视觉选择。相反,如果没有这样的选择,我想将它应用于当前的视觉选择或整个文件。我怎么做到这一点? 请注意,我知道我不需要在这里使用grep。请将其视为更复杂的shell命令的占位符。

2 个答案:

答案 0 :(得分:6)

您需要将范围传递给您的命令,以便它可以在一系列行上运行。

command! -range=% RemoveOutput :<line1>,<line2> ! grep -v 'OUT'

vim documentation for commands了解详情。

LINE RANGE

Some commands take a range as their argument.  To tell Vim that you are
defining such a command, you need to specify a -range option.  The values for
this option are as follows:

    -range      Range is allowed; default is the current line.
    -range=%    Range is allowed; default is the whole file.
    -range={count}  Range is allowed; the last number in it is used as a
            single number whose default is {count}.

When a range is specified, the keywords <line1> and <line2> get the values of
the first and last line in the range.  For example, the following command
defines the SaveIt command, which writes out the specified range to the file
"save_file":

    :command -range=% SaveIt :<line1>,<line2>write! save_file

答案 1 :(得分:1)

这是您需要的语法:

command! -range=% Foo execute <line1> . "," . <line2> . "!command"

注意-range参数。如果没有视觉选择或任意范围,上面的命令将适用于当前的视觉选择,任意范围或整个缓冲区。

请参阅:help :command-nargs