搜索命令`//`如何在Vim中工作?

Che*_*tan 12 vim search keyboard-shortcuts

搜索//完东西后,如果你点击,你似乎得到了下一个结果.这有什么不同n?你应该如何使用它?什么//e匹配,还有什么其他选择//

pax*_*blo 16

搜索命令具有以下格式:

/pattern/offset<cr>
Run Code Online (Sandbox Code Playgroud)

如果pattern省略该部分,搜索将查找搜索到的最后一个模式.如果省略偏移量,则不应用偏移量.一旦找到您的pattern项目,偏移量基本上是对光标的操作.

大多数vi用户都熟悉没有偏移的变化,/pax<cr>重复上次搜索,/<cr>相当于n.

在您的具体示例中,//<cr>它是相同的/<cr>,它意味着重复上一次搜索并且不应用任何偏移量.

另一方面,//e<cr>意味着重复上一次搜索并将光标移动到找到的项目的末尾.补偿是:

[num]         [num] lines downwards, in column 1
+[num]        [num] lines downwards, in column 1
-[num]        [num] lines upwards, in column 1
e[+num]       [num] characters to the right of the end of the match
e[-num]       [num] characters to the left of the end of the match
s[+num]       [num] characters to the right of the start of the match
s[-num]       [num] characters to the left of the start of the match
b[+num]       [num] identical to s[+num] above (mnemonic: begin)
b[-num]       [num] identical to s[-num] above (mnemonic: begin)
;{pattern}    perform another search, see |//;|
Run Code Online (Sandbox Code Playgroud)

没有num用途的加号或减号1.


Bri*_*sen 9

其中一个很好的功能//是你可以在s命令中使用它.因此,如果您最初搜索/Foo然后决定替换它Bar,则可以在不重复模式的情况下执行此操作.做就是了:%s//Bar/g

显然,如果模式稍微复杂一点,这会更有用.


Jos*_*Lee 6

//<CR>意味着重复搜索最后一个模式,没有偏移。

//e<CR>意味着重复搜索最后一个模式,但落在匹配的末尾。

n/<CR>与它使用最后一个模式最后一个偏移量相同,但是n保留最后一个方向,同时/始终找到一个匹配。

有关这些命令及其选项的详细说明,请参阅:h last-pattern和。:h search-offset

  • 偏移量(通常)是您在文件中的位置,从顶部开始。`//` 将从您所在的位置重复搜索,而 `n` 将从最后一个匹配的位置重复搜索。顺便说一句,您也可以在替代命令和全局命令中使用“//”。如果您需要进行特别棘手的匹配,并且想要事先通过常规搜索对其进行测试,则该功能非常有用。 (2认同)