将 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 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
这是您需要的语法:
command! -range=% Foo execute <line1> . "," . <line2> . "!command"
注意 -range
参数。如果没有视觉选择或任意范围,上面的命令将适用于当前视觉选择、任意范围或整个缓冲区。
参见:help :command-nargs
。
我的 .vimrc
command! RemoveOutput :'<,'> ! grep -v 'OUT'
但这显然仅适用于视觉选择,前提是它之前被选中并再次取消选择。相反,如果没有这样的选择,我想将它应用于当前的视觉选择或整个文件。我将如何实现这一目标? 请注意,我知道我不需要在这里使用 grep。请将其视为更复杂 shell 命令的占位符。
您需要将范围传递给您的命令,以便它可以对一系列行进行操作。
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
这是您需要的语法:
command! -range=% Foo execute <line1> . "," . <line2> . "!command"
注意 -range
参数。如果没有视觉选择或任意范围,上面的命令将适用于当前视觉选择、任意范围或整个缓冲区。
参见:help :command-nargs
。