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?


1 Answer 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> 
  • 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, 2019 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.