1

I have this shorcut in my vimrc that I use all the time (admittedly, I just copied it over from the web):

" bind K to grep word under cursor
nnoremap K :grep! "\b<C-R><C-W>\b"<CR>:cw<CR>

This works excellent, it displays the matched keywards in the window below in my vim session:

enter image description here

This is great, but I would like to create another keyboard shortcut that only searches for the keyword under the cursor for the current file only.

I searched how to do that online and found this command:

:vim /pattern/ % | cw

This works fine if I type it out manually on vim, but I'm trying to create a keyboard shortcut for it using this tutorial:

" bind L to grep word under cursor for current file only
nnoremap L :vim <C-R><C-W>% |<CR>:cw<CR> 

This isn't working, it's giving me a syntax error. My question is: how can I add the % option to the above vim command to the keyboard map?

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
abbood
  • 733
  • 9
  • 23

1 Answers1

1

I got it! Just by playing around, I got this to work:

" bind L to grep word under cursor for current file only
nnoremap L :vim <C-R><C-W> %<CR>:cw<CR> 
Pie
  • 175
  • 9
abbood
  • 733
  • 9
  • 23
  • As you noticed, the space between the pattern and percent is necessary for filename expansion. In the link i posted, there is an explanation for why the bar gave you trouble. – D. Ben Knoble Apr 29 '19 at 14:21