Vim 全词搜索 - 但速度更快

发布于 2024-10-13 16:35:32 字数 290 浏览 7 评论 0原文

我知道要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦亦如幻 2024-10-20 16:35:32

我倾向于使用 *# (按照 Brian Agnew 的建议),但如果你想要一个涉及打字的方法,

?word<CR>

你可以这样做:

function! SearchWord(word)
    let @/ = '\<' . a:word . '\>'
    normal n
endfunction
command! -nargs=1 SearchWord call SearchWord(<f-args>)
nmap ? :SearchWord 

注意有一个空格最后一行的 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 typing

?word<CR>

you could do something like this:

function! SearchWord(word)
    let @/ = '\<' . a:word . '\>'
    normal n
endfunction
command! -nargs=1 SearchWord call SearchWord(<f-args>)
nmap ? :SearchWord 

Note there is a space after SearchWord on the last line.

Explanation:

The mapping will make ? open up a command prompt and type SearchWord (including the space). The command makes SearchWord myword do the equivalent of call 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-mode n 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.

时光倒影 2024-10-20 16:35:32

您可以使用 *#(向前和向后)搜索光标下的单词。 g*g# 会执行相同的操作,但会查找包含最初位于光标下方的单词。

显然(!)您需要找到第一个实例才能使其工作,但它对于连续搜索非常有效。

You can search for words under the cursor using * and # (forwards and back). g* and g# 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.

陌伤ぢ 2024-10-20 16:35:32

Al 的解决方案非常好,但不是吗?是否更简单,只需输入 \<\> 模式,然后将光标向后移动两次?这对我有用:

nmap ? /\<\><Left><Left>

这样你仍然可以获得增量搜索(有点)。

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:

nmap ? /\<\><Left><Left>

This way you still get incremental search (kinda).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文