gpt4 book ai didi

vim - 将 vim 用户定义的命令应用于视觉选择或所有

转载 作者:行者123 更新时间:2023-12-02 15:27:46 24 4
gpt4 key购买 nike

我的 .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

关于vim - 将 vim 用户定义的命令应用于视觉选择或所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com