繁体   English   中英

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

[英]Apply vim user defined command to visual selection or all

我的.vimrc有一个用户定义的命令

command! RemoveOutput :'<,'> ! grep -v 'OUT'

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

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

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

有关命令 ,请参阅vim文档

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

这是您需要的语法:

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

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

请参阅:help :command-nargs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM