There are many options available for searching code. If you are using an IDE, there is a good chance it has something built in. Sadly it’s often not very good, or fast, and is never composable. What you want is something that is fast, able to handle complex queries, and that could be composed with other tools. If you are a unix geek, then you are likely already thinking “grep”. That is close to where I am headed, but there is more to the story.
Some time ago I was working on my first larger python codebase, it was pushing some half a million lines of code and lacked documentation. So I often found myself greping. I noticed that one of the other engineers was getting results back from his searches much faster than I was. While my searches would often take minutes to complete, his where returning in seconds. That sent me off on a search to understand why, and to find a better tool.
To cut the story short, I found that the main reason my searches were slow was
that they were searching everything. Every library and dependency in the
repository. I could get things faster by building up lengthy grep ignore lists,
but that felt like a kludge. Then, almost by accident I discovered a little gem:
git grep yes, git has a built in code
search tool! It behaves mostly like the good old grep you (may) be used to, but
this one knows what code to ignore because you have already told git that (in
.gitignore file for example), and it is multithreaded. It is also
However if your needs are more specialized, there is an entire mini-ecosystem of such tools:
- Ack! the original “better than grep” code search tool, written in perl, and portable across Windows, Mac, Linux etc.
- The Silver Searcher Started off as a clone of Ack! but with a focus on speed, it has since diverged somewhat, but retains the speed. Written in C with Windows builds available.
- The Platinum Searcher Intended to one up The Silver Searcher with better unicode support. Written in Go, with binaries available for Windows.
- sift Another take, also written in Go with support across all common OSes.
- ripgrep Interestingly this is written in Rust, and included binaries for the various OSes. The author appears to be thorough and working hard to go beyond the existing projects.
Also, for the true unix geeks, Awk can be a surprisingly powerful (and fast) search tool.