Fast text search in files using The Silver Searcher

Text search inside files is one of the task we, as software engineers, do almost every day. Whether you use an IDE with this functionality built in or not, I believe it’s important to know how to be productive doing this operation from the command line, because in terms of flexibility the command line is really hard to beat. In this post I will show you how to win at file searching using a tool called The Silver Searcher.

grep is a command line utility for searching in files. It takes a pattern and returns the lines matching the pattern.

For example, suppose you are in the root of your project directory and you are looking for all files including the word password. Using grep you can simply run:

grep -R 'password' ./

The terminal will display all the lines with a match for the word password.

grep is a basic UNIX utility, it’s powerful and it has many options that allows for great flexibility, but it has the drawbacks of being quite uncomfortable to use in software projects.

For example, in 99% of the searches we want to ignore the files inside the .git/ directory, while we can use the --exclude-dir option, it still requires some keystrokes making us slow and less productive.

In addition:

  • grep looks for the provided pattern in backup files and binary files, leading to false positives.
  • searching only in files of a single type is complex, and more complex if by “file type” we don’t mean only “matching one specific extension”.
  • grep runs also quite slow compared to more optimised alternatives.

For all this reasons, you might want to improve your grep productivity using The Silver Searcher, or Ag, by Geoff Greer. It smarter and faster than grep, and easier to use in software projects. It’s also optimised to run on large code bases and you will be impressed by its speed.

If you want another excellent alternative to The Silver Searcher you can try Ack, which is mostly compatible with Ag but a little slower (for now).

In this post I will go through some of the basic commands for searching in files with Ag, so you can compare them with your grep workflow and see if it can be improved.

First of all, you can install Ag on a Mac using Homebrew:

brew install the_silver_searcher

Or if you are on Linux check here the package name for your platform.

Basic search

Searching for password in the project directory is easy:

ag password

This excludes the source control directories by default.

Filter by file type

Suppose you want to search only in javascript files:

ag --js password

This does not filter only by the js extension, but also for jsx, es6 and vue. You can check the list of file types and their match using

ag --list-file-type

Exclude directories

Excluding the node_modules directory:

ag --ignore-dir node_modules password

A smarter way to ignore patterns

By default Ag reads the patterns in your .gitignore (or other VCS ignore files) and ignores them (this works also for other source control systems). Beware that there is an open bug report about patterns including a double asterisk.

In addition you can create a .ignore file in your root directory to add more ignore patterns independently from the ones ignored in your .gitignore.

And if you don’t want Ag to ignore your .gitignore contents, you can pass a flag to include them:

ag password --skip-vcs-ignores

Filter by file name

Sometimes you want to run a search only in files having the name matching a pattern. For example, suppose you want to look for password only in files with name including the word database:

ag -G database password

Search by filename

If you want to search in the name of your files you can use find, but also Ag with the -g flag. To search for the word database in your filenames you can then run:

ag -g database

Case-insensitive search

To return case insensitive matches:

ag -i password

Print lines before and after the match

To print 5 lines before every match and 5 lines after:

ag -A 5 -B 5 password

Return only the filenames matching a search

Sometimes it’s useful to return only the filenames matching a search instead of the contents of those files. You can do that using the -l flag:

ag -l password

Piping

Like grep, Ag supports contents from the standard input as source for searching. For example, the following commands pipes the user processes to Ag, which in turn displays the active Neovim processes:

ps -a | ag nvim

Wrapping up

Searching in files is so common that using an underperformant tool is a real waste of time. Whether you are looking for variable names, functions definitions or simply looking for the active process on your machine, a better tool makes the difference in terms of productivity and time saved.

As demonstrated in the above examples, Ag is definitely an easier and faster replacement for grep, so don’t wait for another month before improving your workflow. The effort you will make today to learn it will pay for itself sooner than you expect.

Improve your skills week by week.

Subscribe to my free newsletter to receive original quality content weekly.

By opting in here, you agree to receive value-adding, non-spammy, exclusive content that will help improve your frontend skills, with very few offers, if any. You also agree to the privacy policy.

Comments