Search code examples
selectvimhighlightvimgrep

How to use vimgrep to grep the word that's highlighted by Vim?


Using :vimgrep, inside Vim, in normal mode I can type:

:vimgrep mywords %

to search for "mywords" in the documents below the current directory.

But I wish that in normal mode, when I highlight a word using gd, or in visual mode use viw to select a word, I use a hot key F8 to vimgrep. So I add in my vimrc and restart Vim:

vnoremap <F8> :vimgrep expand('<cword>') %<CR>

This doesn't work for me. I put focus on one word and select it, I press F8, no response in Vim. How can I make it work?

Thanks!


Solution

  • Try

    vnoremap <F8> y:vimgrep "<c-r>"" %<CR> 
    

    Recommended reading: Mapping keys in Vim - Tutorial (Part 1)