A Command Line Trick I Didn’t Know I Needed — Reverse Searching

This week I want to write about a trick I have been using in my terminal lately, reverse searching. I just learned about this, and I have been kicking myself for not learning about it sooner. Hopefully it will come in handy for you as much as it has for me if you haven’t seen this already!

First off, I was using bash as my shell for a long time, and just switched to zsh (and installed Oh My Zsh, it’s awesome!), let me tell you, I was living under a rock. I love it!

This reverse searching tip is the most useful command line tip I’ve been given lately, and will work for both bash or zsh as far as I understand!

Let’s say you have some crazy long Rails command that you used to create your models, migrations, controllers, etc. And you used rails g resource plus a bunch of other stuff:

ex. rails g resource color_form name email:text color_one:text color_two:text color_three:text color_four:text color_five:text percent_one:integer percent_two:integer percent_three:integer percent_four:integer percent_five:integer final_img

That’s a long command! But wait, maybe we screwed something up and now we want to do this over — but I used that command days ago! Oh no!

Reverse search to the rescue! On Mac (and I believe also on PC), ctrl + R will put us into this mode:

As soon as I begin typing, it starts showing me the most recent command which matches my search query. Let’s say I recently navigated all the way into a file which was 6 directories deep from my home directory. Instead of cding into each directory, or typing the long relative path out again, I can use reverse search!

If I was looking for this command:

cd Development/Code/side-projects/sub-directory/sub-sub-directory/sub-sub-sub-directory/

I might start by searching for a word in the last part of the path, in this case sub-sub-sub-directory. Depending on how many commands I have used since this one, I should probably be able to find it by the time I type the word sub:

There it is! That simple. The beauty is, it doesn’t matter how far back I did this command, if I search it correctly, I’ll always find it.

Now if I want to run this command immediately, I can just hit enter. If I wanted to exit the search, I can use either ctrl + C or ctrl + G, whatever works best for you.

However, if I want to finagle it a bit, I can hit escape and I can now do what I want with that command.

More to come on this article, stay tuned!

--

--

--

Full Stack Web Developer && Creative Thinker && Flatiron School Grad && Continuous Learner

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Computer Science students Must Knows

espeak-ng — Linux Command

How does spring security works internally?

Solving Project Euler Problem 25

What are Design Patterns?

Build Faster Apps with Concurrent API Requests

Fetching Git to GitHub

GSoC’20 Episode-1: Interactions with Supernovae!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tyler J Funk

Tyler J Funk

Full Stack Web Developer && Creative Thinker && Flatiron School Grad && Continuous Learner

More from Medium

Convert Common Access Field Defaults to SQL Server

20 Git Commands Every Developer Should Know

Dataclasses in Python (when, why, how)

Template Method — Python Design Patterns — SS Blog

Template Method — Python Design Patterns — SS Blog