Search Files at Ludicrous Speed on Your Macbook Pro

Kabir (ko-bir)
macOS.sh
Published in
3 min readDec 24, 2019

Software engineers often use tools that are a hundred times better than the stock application that consumers use. Searching for information in text files is very common in the software development world. After all, all code files are just plain text files. If you use text files to store information and want to search them fast, you need Silver Searcher for the command line. Silver Searcher uses threads, and therefore it can use your multi-core CPU more efficiently than traditional command-line search tools like grep.

Getting Silver Searcher (ag)

It is straightforward to install Silver Searcher (ag) for your macOS desktop or laptop if you have homebrew already installed. If you don’t have homebrew installed yet, visit the homebrew website.

Assuming you have homebrew installed, you can install Silver Searcher using a single command from the terminal:

brew install the_silver_searcher

Getting started with Silver Searcher

Once Silver Searcher is installed, you can search for information in text files using the ‘ag’ command. For example, to search for the keyword ‘evoknow,’ I can run:

ag evoknow

To search for a phrase such as “UC Berkeley,” I can run:

ag “uc berkeley”

Advanced Usage of Silver Searcher

Here I will show you a few advanced usages of Silver Searcher.

Finding matching line number and filenames

Sometimes you need the name of the file and the line number of the keyword or phrase that you are looking to find. For example:

ag -c “default password”

The following screen shows an example of the above command. Notice that ag shows the files that matched the phrase, and the line number within each file is shown after the filename separated by a colon.

Showing results with filename and line numbers

If you want the names of the files that match, you can use the -l option instead.

Using regular expressions

Using regular expressions in search is very common for software developers. For example, to find all the files that have either ‘Europe/London’ or ‘Europe/Athens’ in them, we can run:

ag ‘Europe/(London|Athens)’

Showing context when searching files

Say you want to find files that contain the word ‘London’ and see a few lines before and after to understand the context where this word appears. You can run:

ag -A4 -B4 ‘Europe/Berlin’

Here is an example output showing four lines before and after the matches.

Showing results of ag with four lines before and after the matching keyword

Siver Searching (ag) is an fantastic command-line tool for every Mac user. It is also available for Linux. Let me know how you like it in the comment section.

--

--