Vim 全词搜索 - 但速度更快
我知道要在 vim 中搜索整个单词,您需要输入:
/\<word\><CR>
现在,我想做的是将这种行为映射到 ? (因为我从不向后搜索,如果需要,我可以向前搜索,然后 NN)。即我想输入:
?word<CR>
并得到与上面相同的结果(vim 搜索整个单词)。我已经摆弄 vim 命令和映射几个星期了,但我不确定如何完成这一任务。感谢您的帮助。
更新:(而不是?我现在使用\)。
I know that to search for a whole word in vim you need to type:
/\<word\><CR>
Now, what I would like to do is to map this behaviour to ? (as I never search backwards, and if needed I could search forward and then NN). I.e. I'd like to type:
?word<CR>
and have the same result as above (vim searches the whole word). I've been fiddling around with vim commands and mappings for some weeks now, but I'm not sure about how to accomplish this one. Thank you for any help.
Update: (insead of ? I use \ now).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我倾向于使用
*
和#
(按照 Brian Agnew 的建议),但如果你想要一个涉及打字的方法,你可以这样做:
注意有一个空格最后一行的
SearchWord
之后。说明:
映射将使
?
打开命令提示符并键入SearchWord
(包括空格)。该命令使SearchWord myword
执行与call SearchWord('myword')
等效的操作(即,它在参数周围加上引号,以便将其转换为字符串)。该函数将搜索寄存器@/
设置为等于由\<
和\>
包围的单词,然后执行正常模式 < code>n 查找搜索寄存器内容的下一个实例。当然,如果您这样做,您就会失去增量搜索的好处,但无论如何希望它有用。
I tend to use
*
and#
(as suggested by Brian Agnew), but if you want a method that involves typingyou could do something like this:
Note there is a space after
SearchWord
on the last line.Explanation:
The mapping will make
?
open up a command prompt and typeSearchWord
(including the space). The command makesSearchWord myword
do the equivalent ofcall SearchWord('myword')
(i.e. it puts the quotes round the argument in order to make it into a string). The function sets the search register@/
equal to your word surrounded by\<
and\>
and then does a normal-moden
to find the next instance of the contents of the search register.Of course you lose the benefits of incremental searching if you do this, but hopefully it's useful anyway.
您可以使用
*
和#
(向前和向后)搜索光标下的单词。g*
和g#
会执行相同的操作,但会查找包含最初位于光标下方的单词。显然(!)您需要找到第一个实例才能使其工作,但它对于连续搜索非常有效。
You can search for words under the cursor using
*
and#
(forwards and back).g*
andg#
will do the same but finding words containing what's under your cursor initially.Obviously (!) you need to have found the first instance already for this to work, but it's very effective for successive searches.
Al 的解决方案非常好,但不是吗?是否更简单,只需输入
\<\>
模式,然后将光标向后移动两次?这对我有用:这样你仍然可以获得增量搜索(有点)。
Al's solution is very nice, but wouldn't it be simpler to just enter the
\<\>
pattern, then move the cursor back twice? This works for me:This way you still get incremental search (kinda).