How to get vimgrep to search for matching words?

I have an instruction in the _vimrc file to display F3, to make vimgrep for the word under the current cursor.

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

Now I want to make vimgrep to exactly match words for the word under the current cursor. I changed it as shown below, but it does not work.

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

Something is wrong? How can I achieve exact match words?

+3
source share
1 answer

The problem is that you need to double the backslash - one backslash will escape from the next character, and if the character has no special meaning, then the backslash will be removed. eg.

echo "\<"

will print

<

This seems to work fine:

map <leader>s :execute "noautocmd vimgrep /\\<" . expand("<cword>") . "\\>/gj **/*." .  expand("%:e") <Bar> cw<CR>
+4
source

Source: https://habr.com/ru/post/1724858/


All Articles