Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make vimgrep do word match search?

Tags:

vim

vimgrep

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?

like image 544
Morgan Cheng Avatar asked Oct 15 '25 20:10

Morgan Cheng


1 Answers

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>
like image 52
Dave Kirby Avatar answered Oct 19 '25 00:10

Dave Kirby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!