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 compassable. What you want is something that is fast, able to handle complex queries, and 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. 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 your .gitignore file for example), and it is multithreaded. It is also universally available.
However if your needs are more specialized, there is an entire mini-ecosystem of such tools:
- The original “better than grep” code search tool, written in perl, and portable across Windows, Mac, Linux etc.
- 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.
- Intended to one up The Silver Searcher with better unicode support. Written in Go, with binaries available for Windows.
- Another take, also written in Go with support across all common OSes.
- 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.
- For the true unix geeks, can be a surprisingly powerful (and fast) search tool.
About the Author
Jordan Schatz is a Gun.io software engineer and consultant helping companies deliver results through; recruiting talent, training & leading engineering teams, designing software architecture, user experience, and running systems at scale. He believes that we can do better as an industry, producing software that is a greater benefit to humankind, and do so with a lower total cost of ownership.
My focus is on making software that is:
Besides a long history of software consulting he has also lead teams in startups, founded one of his own and has been the CTO for a mid sized ($50MM/yr) business, built a software development company and have written software in enterprise & government environments.
He currently lives as a digital nomad, traveling around the US (and contemplating expanding that to the world) with his wife and four kids.