Command Line Search Tools for Programmers

Published on by

Command Line Search Tools for Programmers image

Over the last several years, I’ve improved my command line searches through a few tools geared towards programmers. These tools help developers find phrases and patterns in text files in an unfamiliar codebase without the complexity of grep.

Searching for a unique string or keyword is an excellent way to find out where functionality is located without jumping into a text editor. Another use I have is finding previous commands that you ran via history and using a tool like grep to filter down lines that match a given pattern.

The following is a list of five command line search tools that will help you as a developer if you are interested in using the command line more for finding code, text, and files quickly without relying on an editor or an IDE.

Some of the tools are ‘nix only, but I’ve listed a few that are cross-platform and ridiculously fast!

Grep

The nice benefit of using grep is that it’s available on pretty much any ‘nix distribution you might use. Its utility is powerful in many different contexts, and I’ll show you a few of my favorite:

If you want to find a phrase in only PHP files and output the line number:

$ grep -RHn --include \*.php Controller .

Let’s say you typed a command into the console a few days ago but you only recall part of the command. You can pipe (|) the history command to search with grep:

$ history | grep "php artisan"

You can pipe all of CLI tools listed in this article, but I find I just need a simple grep which filtering history. A nice side-effect is that the filtered results will give you a number at the beginning and you can use it to re-run the command:

$ history | grep 'php artisan'
284 php artisan route:list
 
$ !284
$ php artisan route:list

Ack

Ack is “a tool like grep, optimized for programmers.” It searches recursively by default (i.e., your project) while ignoring VCS directories like .git and has convenient tools that help you explore code with fewer keystrokes.

Taking the same grep example, here’s how we would search for “Controller” in only PHP files:

# Ack
$ ack Controller --php
 
# Here's the grep example
$ grep -RHn --include \*.php Controller .

Let’s say that you wanted to search all other types of files except PHP. Each type has a “no” flag:

$ ack Controller --nophp

You can extend ack through an ~/.ackrc file to add custom types like --php. Let’s say that you commonly search in only blade files with something like this:

$ ack @auth --blade
Unknown option: blade
ack: Invalid option on command line

To register the “blade” type, you can add the following to an ~/.ackrc file and then search above will only look in files that end in blade.php:

--type-set=blade:match:.blade.php$

Here are a few other options you might want as a Laravel developer in the ~/.ackrc file:

# Always use color
--color
 
# Ignore PhpStorm and NPM
--ignore-dir=.idea/
--ignore-dir=node_modules/
 
# Add to existing types
--type-add=ruby:ext:haml,rake,rsel
 
# Add new types
--type-set=smarty:ext:tpl
--type-set=cakeview:ext:ctp,thtml
--type-set=markdown:ext:md,markdown
--type-set=json:ext:json
--type-set=blade:match:.blade.php$

Ack looks in various locations for an .ackrc file, but if you want to run ack without any .ackrc file, use the --noenv flag.

You can verify your custom types by running ack --help-types. Ack has a ton of documentation and probably does things I haven’t discovered yet. Check man ack for more information or check ack manual online.

The Silver Searcher

The Silver Searcher is another grep replacement that is similar to ack, but touts faster performance. It ignores files found in a project’s .gitignore file.

You can install Silver Searcher with Homebrew on OS X:

brew install the_silver_searcher

You run The Silver Searcher with the ag command:

$ ag Controller --php

I won’t cover ag in a ton of detail, but if I want to search a lot of files I sometimes reach for ag.

Sift

Sift is a grep alternative built with Golang which means that it’s widely available on Linux, Windows, OS X, and others. It’s ridiculously fast, and it has some cool use-cases that replace grep + awk combinations to extract data.

I suggest that you check out the samples to learn about the powerful features in sift.

Using our basic PHP search we’ve used for the other tools, here’s how you’d find “Controller” in PHP files:

# Only PHP
sift --ext php Controller
 
# Exclude PHP
sift --exclude-ext php Controller

RipGrep

RipGrep aligns itself as similar to The Silver Searcher, but with “the raw speed of GNU grep,” and it works on Mac, Linux, and Windows. The readme claims that RipGrep is generally faster than anything else, touting Rust’s regular expression engine, and honors the .gitignore file like The Silver Searcher.

Here’s how you would search PHP files for “Controller” with RipGrep:

rg --type=php Controller

What’s Next?

Ack is my trusty search tool of choice and I think you will get a lot of value in using it as a grep replacement. I would highly recommend learning how to use ack first, but these tools all have unique features that make them valuable in different ways.

If you need to search large amounts of files (I am looking at you recursive node_modules/ folder) then go for The Silver Searcher, Sift, or RipGrep. In large projects ack is still a decently performant tool but you will notice speed improvements in the other tools.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Asymmetric Property Visibility in PHP 8.4 image

Asymmetric Property Visibility in PHP 8.4

Read article
Access Laravel Pulse Data as a JSON API image

Access Laravel Pulse Data as a JSON API

Read article
Laravel Forge adds Statamic Integration image

Laravel Forge adds Statamic Integration

Read article
Transform Data into Type-safe DTOs with this PHP Package image

Transform Data into Type-safe DTOs with this PHP Package

Read article
PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell image

PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell

Read article
Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3 image

Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3

Read article