mhinz / vim-grepper

:space_invader: Helps you win at grep.
MIT License
1.21k stars 59 forks source link

-cword stopped working #257

Open slashmili opened 2 years ago

slashmili commented 2 years ago

NVIM version: v0.6.1 vim-grepper version: 2b93535752ffcb312f9fab73d90e80dc9f2e60fc OS: Mac 12.3.1 git: 2.33.0

I've been using this option for a while but I've noticed it's stopped working recently:

noremap <c-g> :Grepper -cword -noprompt<CR>

If run this command Grepper -cword, I get:

git grep -nGI> '\\bnoremap\\b'

When I press enter, it won't match with any record

If run it again and this time, remove one set of \ like this:

git grep -nGI> '\bnoremap\b'

it finds all the matches.

dennishostetler commented 1 year ago

Same issue here on my mac. It works on Windows though.

slashmili commented 1 year ago

just for the record I ended up using this for now:

noremap <c-g> :execute "Grepper -noprompt -query " . expand("<cword>")<CR>
LionelMartin commented 1 year ago

Same issue here

vext01 commented 1 year ago

Same here

vext01 commented 1 year ago

noremap :execute "Grepper -noprompt -query " . expand("")

Does anyone have a version of this I can drop in my init.lua?

I thought this might work, but no:

vim.api.nvim_set_keymap('n', 'gs', ':Grepper -noprompt -query ' .. vim.fn.expand('<cword>') .. '<CR>', { noremap = true, silent = true })

EDIT:

This works:

vim.api.nvim_set_keymap('n', 'gs', ':execute ":Grepper -noprompt -query " .. shellescape(expand("<cword>"))<cr>', { noremap = true, silent = true })
vext01 commented 1 year ago

I had a prod around in the plugin sources today. There's a call to shellescape that is double escaping the word anchors.

As @slashmili noted, this means we end up running commands like:

git grep -nGI> '\\bnoremap\\b'

instead of:

git grep -nGI> '\bnoremap\b'

This diff seems to work, but I can't vouch for its correctness. I'm not good at lua or vimscript:

diff --git a/plugin/grepper.vim b/plugin/grepper.vim
index 3552d51..9cd10e0 100644
--- a/plugin/grepper.vim
+++ b/plugin/grepper.vim
@@ -401,7 +401,7 @@ function! s:escape_cword(flags, cword)
   endif
   let a:flags.query_orig = a:cword
   let a:flags.query_escaped = 1
-  return shellescape(escaped_cword)
+  return "'" . escaped_cword . "'"
 endfunction

 " s:compute_working_directory() {{{2
lbonn commented 1 year ago

shellescape is here for a reason, so that the search string is correctly forwarded to the grep program if a special character is in the query.

It is working fine on linux with bash afaik, question would be why the \ is doubled on Mac in this case. Do you by any chance run fish shell?

vext01 commented 1 year ago

Do you by any chance run fish shell?

You gotta be kidding me! I'm using fish on OpenBSD and Linux!

lbonn commented 1 year ago

@vext01 there was this change in vim and neovim related to fish and escaping https://github.com/vim/vim/pull/8810, https://github.com/neovim/neovim/pull/15550

May be related to that

dennishostetler commented 1 year ago

I use the fish shell on my mac. Seems like you're on to something.

vext01 commented 1 year ago

I've just tried with my shell set to ksh and there is no issue there. Seems to be to do with fish...