vim - 如何让 vimgrep 进行单词匹配搜索?

标签 vim vimgrep

我在 _vimrc 文件中有以下语句来映射 F3 以对当前光标下的单词执行 vimgrep。

map <F3> :execute "noautocmd vimgrep /" . expand("<cword>") . "/gj **/*." .  expand("%:e") <Bar> cw<CR>

现在,我想让它 vimgrep 用于当前光标下单词的精确单词匹配。我如下更改了它,但它不起作用。
map <leader>s :execute "noautocmd vimgrep /\<" . expand("<cword>") . "\>/gj **/*." .  expand("%:e") <Bar> cw<CR>

有什么不对吗?我怎样才能实现精确的单词匹配?

最佳答案

问题是您需要将反斜杠加倍 - 单个反斜杠将转义下一个字符,如果该字符没有特殊含义,则删除反斜杠。例如

echo "\<"

将打印
<

这似乎工作正常:
map <leader>s :execute "noautocmd vimgrep /\\<" . expand("<cword>") . "\\>/gj **/*." .  expand("%:e") <Bar> cw<CR>

关于vim - 如何让 vimgrep 进行单词匹配搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1854956/

相关文章:

VIM 缩写停止工作 : how to debug

vim - Vim 文本对象 ip 和 ap 有什么区别?

bash - 如何阻止 bash 在 vi​​m 中的每个字母前添加 ^@

vim - 如何在 vimgrep 中排除文件模式?

vimgrep 不区分大小写的文件模式

vim - 使用ssh/Putty和netrw在Windows上透明地编辑远程文件

vim - 什么是正确的 Vim/Ag g :ctrlp_user_command value for Windows?

regex - VIM - 当前缓冲区中视觉选择的 VIMGREP 热键

vim - 在 Vimgrep 中的按键映射中使用多个模式

vim - 有什么方法可以使用 vimgrep,限制为 >1 但不是所有类型的文件?