vimvimgrep

How to make vimgrep do word match search?


I have below statement in _vimrc file to map F3 to do vimgrep for word under current cursor.

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

Now, I want to make it vimgrep for exact word match for word under current cursor. I changed it as below but it doesn't work.

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

Anything is wrong? How can i achieve exact word match?


Solution

  • The problem is that you need to double up the backslashes - a single backslash will escape the next character, and if the character does not have a special meaning then the backslash is removed. e.g.

    echo "\<"
    

    will print

    <
    

    This seems to work OK:

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