Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitGrep command #36

Closed
wants to merge 1 commit into from
Closed

Add GitGrep command #36

wants to merge 1 commit into from

Conversation

icholy
Copy link
Contributor

@icholy icholy commented Nov 2, 2015

Is there a way to do this without adding a command?

@junegunn
Copy link
Owner

junegunn commented Nov 3, 2015

That looks almost identical to Ag, right? Maybe we could generalize the concept into a versatile command that can be used with grep, ag, git grep, or whatever, like vim does with grepprg and grepformat. I'll think about it. Until then, you can have it on your vimrc, though you'll have to replace s: references appropriately.

@icholy
Copy link
Contributor Author

icholy commented Nov 3, 2015

Yeah, I just copied the Ag command. Generalizing it sounds like the right idea.

@rainerborene
Copy link

What do you think about renaming Ag to Grep and use grepprg and grepformat options?

@rainerborene
Copy link

That would allow users to temporarily change grepprg and grepformat on a function if they want to.

function! s:Ag()
  " store original grepprg and grepformat
  " change grepprg....
  " call grep
  " restore original options
endfunction

@junegunn
Copy link
Owner

That's a good point, adding a generalized version of Ag. But I'll keep Ag just for convenience. You don't have to know how to set up grepprg and grepformat, and it's tad shorter to type :) I'll review the code if you send me a PR.

Incidentally, I have Grep command defined in my vimrc that does not use fzf and is just a shortcut to the builtin :grep command. Sometimes in a small repo when you simply want to build a quickfix list of matches, fzf can be an overkill.

@LK4D4
Copy link

LK4D4 commented Sep 19, 2016

@junegunn Sorry to bother you, but would you be opposed to at least option to replace ag command there? I've tested three tools: ack, ag and pt and all three have same options.
https://gist.github.com/anonymous/8eaa4eff4dc0e248b1d6e9e222088c62 here is the patch which I used for tests.

@junegunn
Copy link
Owner

junegunn commented Sep 25, 2016

@LK4D4 That is based on the assumption that the other commands 1. take the same set of options (which is not true for the new rg), 2. and print the output in the same format as ag. I'll see if it's viable to add a generalized grep function that takes the full command. The command is also responsible for transforming the output so the same sink function can be applied.

@junegunn junegunn mentioned this pull request Sep 25, 2016
9 tasks
@chopfitzroy
Copy link

Hey @junegunn in regards to the above comment and a generalised grep function will a new issue be opened for this? Asking simply in regards to tracking progress.

Cheers.

@junegunn
Copy link
Owner

@CrashyBang No need. I'm working on it, coming soon.

@junegunn
Copy link
Owner

I'm almost done but there's a few issues.

  • Different format. git grep or gnu grep do not print the column number unlike ag, pt, or awk
  • rg seems unstable at the moment.
    • This does not work on Vim: echo system('rg ".*"') | echo v:shell_error

@chopfitzroy
Copy link

Hey @junegunn,

Cheers for letting me know, should I post an issue against rg?

Cheers.

@icholy
Copy link
Contributor Author

icholy commented Sep 25, 2016

Lol that's dope

@junegunn junegunn closed this in 7707746 Sep 25, 2016
@junegunn
Copy link
Owner

No idea why rg doesn't work well in this context, but it's a separate issue anyway.

This is how the new fzf#vim#grep(command, with_column, bang_or_extra_opts) function can be used:

" git grep and gnu grep do not print column number so with_column is 0
command! -bang -nargs=* GGrep call fzf#vim#grep('git grep --line-number '.shellescape(<q-args>), 0, <bang>0)
command! -bang -nargs=* Grep call fzf#vim#grep('grep -r --line-number '.shellescape(<q-args>).' *', 0, <bang>0)

@chopfitzroy
Copy link

Hey @junegunn got this working with rg 👍

I am using the following command which works perfectly except for one thing:

command! -bang -nargs=* Find call fzf#vim#grep('rg --vimgrep --fixed-strings --ignore-case --no-ignore --hidden --follow --glob "!.git/*" '.shellescape(<q-args>), 1, <bang>0)

It does not highlight the search term like :Ag used to, should it highlight it or was that custom to the :Ag command?

Cheers,
Otis.

@junegunn
Copy link
Owner

junegunn commented Sep 26, 2016

rg seems to ignore --color=always if --vimgrep is set, which is a sensible thing to do I guess.

So without --vimgrep, we can use something like rg --column --line-number --no-heading --color=always KEYWORD. It works fine on Neovim (with the latest fzf 0.15.2). But for some reason it doesn't work correctly on Vim running on tmux. I can see some extra ^O characters in the output, not sure why.

# Compare these
echo foobar | ag --color foo | more
# ESC[30;43mfooESC[0mESC[Kbar

echo foobar | rg -N --color=always foo | more
# ESC[91mESC[1mfooESC[m^Obar (on tmux)
# ESC[91mESC[1mfooESC(BESC[mbar (not on tmux)

If that extra ^O turns out to be a valid character, it's trivial to fix the sink function to ignore that.

@junegunn junegunn mentioned this pull request Sep 26, 2016
9 tasks
@chopfitzroy
Copy link

Hey @junegunn looks good to me, would you like me to open an issue with rg? Or would you be better suited?

@junegunn
Copy link
Owner

Do as you please. FYI, we can make it work without changing rg or fzf.vim by filtering out the character with tr.

command! -bang -nargs=* Find
  \ call fzf#vim#grep('rg --column --line-number --no-heading --color=always '.shellescape(<q-args>).'| tr -d "\017"', 1, <bang>0)

@rawaludin
Copy link

Trying to make it work. But no, this implementation is not faster than @junegunn implementation with Ag in fzf.vim. Just want to make sure, there won't be native :Rg command in this plugin right?

Thanks.

@rawaludin
Copy link

Using sample Rg implementation in Readme, when I'm using :Rg its count total files longer than using :Ag command. The result is come late too.

@rawaludin
Copy link

@junegunn
Copy link
Owner

junegunn commented Jan 3, 2017

I don't know, I just use :Ag KEYWORD :) The performance of ag has never been a problem for me, so I haven't really looked into rg.

@rawaludin
Copy link

Yeah, better stick with Ag :)

@stephenprater
Copy link

@rawaludin I'm having this same problem - did you ever figure it out? Running rg is twice as fast as ag just running it from bash - but running within FZF.vim and rg is way slower than ag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants