Grepping Git Repositories and Other Collections

My current workflow has two ways of grepping collections of files. For
grepping Git repositories, I use counsel-git-grep. It uses the fast
grep built into Git as a backend. For other collections, I use
counsel-ag, a recursive grep that uses The Silver Searcher for a
backend. I usually prefer to use counsel-git-grep because I can be
in any directory of the repository and still search everything; I
don’t have to worry about starting in the top directory.

Then I read this recent post from abo-abo. In it, abo-abo says that he
now uses counsel-rg, which uses ripgrep as a backend, for everything
because it is the fastest of the generally available greps. That
sounded pretty good but I was confused. As far as I could tell from
the documentation, ripgrep doesn’t treat Git repositories in a special
way. It will simply do a recursive search from the current directory
(or the one you explicitly specify, of course). So I queried abo-abo
about that and it turns out that counsel-rg (as well as
counsel-ag) now explicitly check for a Git repository and will start
from the repository’s top directory unless you specify some other
starting point.

What that means to me is that I don’t need the two ways of grepping. I
can simply use counsel-ag and it will do the right thing if I’m in a
Git repository. That leaves the question of whether I should
install ripgrep and use counsel-rg to do my greps. Ripgrep does
appear to be considerably faster than the other methods but unlike
abo-abo, I’m not dealing with 2G repositories and counsel-ag seems
more than fast enough. Still, it’s always nice to be faster so I installed
ripgrep so that I could try out counsel-rg anyway.

Be sure to read abo-abo’s post. It has some other information on his
workflow and handling such things as ignore files. You might also
enjoy this abo-abo post on ripgrep: he explains why he switched to it
and some of its advantages. And if you aren’t already using the
Ivy/Counsel/Swiper suite, stop what you’re doing and install them
right now. You will, I promise you, be glad you did.

This entry was posted in General and tagged . Bookmark the permalink.