Why I Search with Grep

Even though Sublime Text has Find in Files, Goto Definition, and Goto Anything, I still use grep and its friends on a regular basis. In some cases, it’s the only viable option, while in others, it’s faster and more convenient.

What is Grep?

Grep is an ancient command line utility created in 1974 by Ken Thompson, first appearing in Version 4 of Unix. It’s short for “globally search a regular expression and print”. Regular expressions are used to specify your search string, but ordinary strings are often good enough. In the following example, grep will return all lines containing the “search string” inside files in the specified folder, any level deep (the recursive-Rswitch), case-insensitive (-i).

grep "search string" folder -i -R

Everyone Has It

Not everyone uses grep, but it’s installed by default on Unix-like OSes. When I pair-program on someone else’s computer, I will use grep rather than learn to search with their editor. ack is a slightly more convenient alternative to grep, but I stick with grep because it’s always available. Grep is often your only choice in a production environment.

Long or Wide Files

Grep is great for filtering server logs to focus on key lines. I’ll often insert debug info into the Rails log, prefixed with an arbitrary string//// which only I would use. Then I can filter the log for my special string. In the following example, the tail command outputs the bottom of the log file in real-time as it’s being added to, thanks to the -f (follow) switch. Then the output is piped into the grep command using |, so I only see what I’m interested in.

tail -f log/development.log | grep '////'

Sometimes, I’ll search a file containing very long lines of code, or even just one huge line. A Sublime search will return the entire line, which doesn’t help. By specifying the . regular expression wildcard, I can truncate the result to a reasonable number of characters:

grep -o .....search..... wide-file.txt

Chained Searches

Sometimes I won’t remember how something is named. Was it EvidenceLinkedExternally,ExternallyLinkedEvidence, or EvidenceExternallyLinked? Or a search will return too many results, and I want to narrow it down further, but I’m too lazy to search with my eyes. These cases call for chaining multiple greps together, again with the pipe operator|.

grep -iR evidence app | grep -i external

The above command returns any line of code in the app folder which contains both the ‘evidence’ and ‘external’ words, no matter what order they’re in.

Inverted Searches

Grep can also be used to return results NOT containing a search string. Recently I was working on updating the way we retrieve application config values. We read those values with code similar to configatron.some_setting, but we realized configatron.some_setting!with a bang would be better. That way, if the setting was missing or misspelled, it would raise an error immediately while still in development, rather than confounding the end user in production. So I did this:

grep configatron app lib config -R | grep -v "\!" | grep -v "="

The above line searches for strings that:

  • contain the word configatron
  • are in any of the app, lib and config folders and their subfolders (-R )
  • do NOT contain the bang, because those are OK (using -v)
  • do NOT contain the assignment operator (because that’s where settings are being assigned rather than retrieved)

More Keyboard-Friendly

In Sublime, I can save my favourite search filters. But I have to navigate the drop-downs to find what I want, using my mouse. I hate using my mouse. I type faster than I can aim. So I prefer to setup bash aliases with my favourite searches. For example:

alias gspec="grep 'describe\|shared_example\|context\|scenario\| it ' -R --color"

This allows me to get the outline of a specified rspecfile like this:

More Responsive

If I perform a grep search which is too wide in scope, returning an endless stream of results, I can simply press Ctrl-C to cancel it immediately. With Sublime, there is no way to cancel a long search. My computer slows until the cows come home.

Conclusion

There’s a new code editor every year, but 40-year-old grep is still my favourite search tool. It’s installed everywhere, works with long and wide files, allows for sophisticated searches, and always feels snappy and convenient.

Bonus Tip: git grep is a thing! Use it to quickly search just your source code!