Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim <C-R><C-W> ignore current content of the line

Tags:

vim

I have this script to search for the word under cursor in the current project:

nnoremap <leader>K :grep! "\b<C-R><C-W>\b"<CR>:cw<CR>

It works just fine except when the word starts with b.

This is due to <C-R><C-W> only completing the remaining of the word. For example, if I'm searching for "branch", my pattern gets something like this:

\branch\b

Which is equivalent to search for the work "ranch".

Any thoughts on how to figure this out?

like image 505
Henrique Barcelos Avatar asked Dec 07 '25 06:12

Henrique Barcelos


1 Answers

Try this: nnoremap <leader>K :execute 'grep! "\b"'.expand("<cword>").'"\b"'<CR>:cw<CR>.

<cword> will expand to the current word under the cursor, as :help :<cword> explains, along with others:

<cword>    is replaced with the word under the cursor (like |star|)
<cWORD>    is replaced with the WORD under the cursor (see |WORD|)
<cfile>    is replaced with the path name under the cursor (like what|gf| uses)

Check the help for more info.

like image 184
rgoliveira Avatar answered Dec 09 '25 18:12

rgoliveira



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!