Updating my code search tools
dimanche 23 avril 2017 - un commentaire
Finding an information in a codebase is a very common task performed by software engineers. The quicker you can do it, the more chance you have of not losing your momentum. With proper tools, you can find things 10 times faster1.
In 2011, I’ve presented ack in a lightning talk at Paris Web. That was my tool of choice for a while. It was very convenient because it didn’t report files stored in the VCS folders. A bit later, I switched to ag that does the same thing and also tries to avoid files defined in .gitignore
. Recently, I’ve often ran into its limitations in .gitignore
file support. So I’m switching to two tools.
ripgrep
A recent player in the field. The introductory blog post got me very interested with the detailed explanation of the code and the benchmarks. It has a way better support for .gitignore
. And it’s pretty cool to be using a tool written in Rust.
I’d love if it supported searching through compressed files out of the box. In the mean time, this command will do the trick, although it can’t take advantage of parallelism:
find . -name "*.gz" -exec gzcat "{}" + | rg "whatever"
Update: ripgrep
now supports searching in compressed files with the -z/--search-zip
option.
git grep
This will only work in git repositories of course. Because it is a git tool, it only inspects files in the repository. To make it more convenient, I’ve tweaked it to have the same output than ripgrep.
--heading
prints the filename once before all matches in that file--break
puts an empty line before a new file
Here’s an extract of my global .gitconfig
:
[alias]
rg = "grep --heading --break -i"
[grep]
lineNumber = true
[color "grep"]
filename = "magenta"
linenumber = "green"
match = "red bold"
While looking into the documentation to tweak the output, I’ve noticed some interesting options. I don’t use them often but they are nice
-p
: This will try to display the function that the matches are part of. Not very reliable but gives a bit more context sometimes.-O
: This will open the matching files in the pager specified. If you set the pager to your code editor, it will open all matching files in your text editor.
Commentaires
Hi! Someone linked me to your post cause I was working on a grep feature recently. Ripgrep is awesome (and definitely the fastest regex CLI out there). I still mostly rely on git grep myself since its fast enough and the muscle memory of typing
git grep
. But yeah, I was recently wanting to solve being able togrep
on random repos/commits without needing to check them out. So we implemented grep on sourcegraph.com. EG click on the magnifying glass on https://sourcegraph.com/github.com/...The UI is gonna change very soon to be more useful and less cramped (current UI is based on what vscode provides). But given you have an interest in text search, I thought you may be interested in it / may have some feedback. Anyways thanks for great blog post :)