Vim <C-R><C-W>忽略当前行的内容

3
我有一个脚本,在当前项目中搜索光标下的单词:
nnoremap <leader>K :grep! "\b<C-R><C-W>\b"<CR>:cw<CR>

除了以b开头的单词外,它完全正常工作。

这是由于<C-R><C-W>只能完成单词的剩余部分。例如,如果我要搜索“branch”,则我的模式会得到类似这样的结果:

\branch\b

这相当于搜索单词“ranch”。

对此有什么想法吗?


1
你可以使用“-w”选项告诉“grep”仅搜索单词。这样,您就不需要使用“\b”,如果您想仅匹配整行,则有“-x”选项。 - Sundeep
1个回答

1

请尝试以下内容:nnoremap <leader>K :execute 'grep! "\b"'.expand("<cword>").'"\b"'<CR>:cw<CR>

<cword> 将扩展为光标下的当前单词,如 :help :<cword> 所解释的那样,还有其他选项:

<cword>    is replaced with the word under the cursor (like |star|)
<cWORD>    is replaced with the WORD under the cursor (see |WORD|)
<cfile>    is replaced with the path name under the cursor (like what|gf| uses)

查看更多信息请查阅帮助文档


1
这个对我起作用了,我只需要做一些更改:nnoremap <leader>K :execute 'grep! "\b"'.expand("<cword>").'"\b"'<CR>:cw<CR> - Henrique Barcelos
哦,是的,我搞错了。我当时有点着急,只是用 :echo 进行了测试。我会修改以包含你的那一行。 - rgoliveira

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接