0

I have this alias to grep under the cursor (for the entire project):

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

How can I change this to only grep the contents of the currently opened file?

note: My grep is linked to the silver searcher like so:

if executable('ag')
  " Use ag over grep
  let &grepprg = "ag -w"
endif

Update: How to avoid the hotkey window?

mas suggested I use fzf Blines command, which worked using this keyboard shortcut:

nnoremap L :BLines "\b<C-R><C-W>\b"<CR>

when I put the cursor on a word and type L, I get the results in a hotkey window like so:

enter image description here

However If I simply type the following into the command line

:BLines 

I have a much more pleasant window that appears in the middle of the screen

enter image description here

What is it in my keyboard shortcut that's preventing the middle screen window from appearing?

6
  • If you have fzf and fzf.vim installed, you can try Blines from the command mode.
    – mas
    Commented Jan 29, 2023 at 1:37
  • thanks mas! It worked, but I updated my question with a little tweak
    – abbood
    Commented Jan 29, 2023 at 5:44
  • As vim provides * for searching a word under the cursor, I tried :nnoremap * yiw:BLines <C-R><C-W><CR> and it works quite nicely. You don't have to remember a new command to search for a word under the cursor.
    – mas
    Commented Jan 29, 2023 at 6:27
  • That's odd.. isn't <C-R><C-W> for copying the word under the cursor in command notation? Why do you still have it in your command if you are already using *?
    – abbood
    Commented Jan 29, 2023 at 13:20
  • 1
    <C-r><C-w> pastes the contents of the register. First we have to yank the word under the cursor to the register via yiw and then paste back the same for the query in the command mode. Hope that helps.
    – mas
    Commented Jan 30, 2023 at 2:04

1 Answer 1

1

I would do:

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

Remark: grep is looking into the corresponding file. If your buffer is not saved on disk the result will be puzzling. To limit the problem, at the expense of saving the file, you could add the save operation into the mapping.

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

If you want not to change the file on disk but accept to temporarily change it for the sake of grep you could do:

nnoremap K :w<CR>:grep! "\b<C-R><C-W>\b" %<CR>:earlier 1f<CR>:w<CR>:later 1f<CR>:cw<CR><ENTER>

Explanation:

  • :w save the file on disk to let grep analyze it
  • :earlier 1f restore the buffer to the content previously saved on disk
  • :w restore the content of the file
  • :later 1f restore the buffer
4
  • 2
    That would work on the current file, though, not the current buffer. Maybe that's what OP want.
    – romainl
    Commented Jan 28, 2023 at 10:23
  • You are right, I have tried to improve the answer to take your remark into account (not a perfect solution but better than nothing) Commented Jan 28, 2023 at 10:36
  • My bad I was actually interested in searching the current file (not the current buffer - I corrected the question), so I'm using your initial script: nnoremap K :grep! "\b<C-R><C-W>\b" %<CR>:cw<CR><ENTER> However when I use it and the list of the matched searches appear, nothing happens when I press enter, which wasn't the original behavior. Can you please amend it so that when I press enter it jumps to the location of that search result?
    – abbood
    Commented Jan 28, 2023 at 18:24
  • See :help :grep.
    – romainl
    Commented Jan 28, 2023 at 19:02

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.