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"

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.

  1. Yes, 10 times