Use reverse-i-search to quickly navigate through your history

Anthony Ng
codeburst
Published in
4 min readApr 29, 2018

This article is intended for Mac users

Long terminal command that we’d rather not retype

You type a long command into your terminal to start up your program. Now you want to do it again. The easiest option is to retype the entire command. Your typing speed might improve. Or, you will get tired of programming 😪.

Let’s take a look at some alternatives.

Using the arrow keys

You can press the up and down arrows in your terminal to navigate through your history. This navigates through your history chronologically. Pressing the up arrow will go one entry earlier in your history. Pressing the down arrow will go one entry later in your history.

This is convenient for commands that you ran 5 minutes ago. But it can get unwieldy when you are searching for an obscure command you used 5 days ago.

Side note: Where is this history kept?

Your machine saves the history of your commands into a file. Think of it like your web browser’s history. You can run history to view the history of all your recently executed commands.

history

You can also open the ~/.bash_history file to view the same results.

reverse-i-search, a better way of searching

You ran an AWS command in your terminal 5 days ago. Unluckily for you, you don’t remember the obscure StackOverflow question that contained it. Of course, you also forgot to document this command. You can use the arrow keys, but this will go through all the commands you executed in your terminal since last week.

Instead, we can use reverse-i-search.

reverse-i-search: Search backward starting at the current line and moving ‘up’ through the history as necessary. This is an incremental search. (source)

Press the ctrl key and the r key simultaneously. A prompt will appear like so:

reverse-i-search prompt

Start typing what you remember of your command. You will see the latest command from your history that matches your search term.

reverse-i-search search results

Press ctrl + r again to navigate through earlier entries in your history that match your search term.

Helpful tips and tricks

Using HISTIGNORE

The up and down arrows are useful to grab recently used commands. However, it navigates through ALL your commands, including commonly used ones that you probably were not looking for. You’re not looking for an ls or pwd command to execute again.

Our history with junk

You can update your ~/.bashrc file with a HISTIGNORE entry to filter your history.

~/.bashrc with HISTIGNORE

Now, you will no longer have a history filled with useless pwd and ls commands that you don’t intend to reuse. Beautiful.

Our history with less junk

Note that HISTIGNORE does not affect what is currently saved in your ~/.bash_history file. It will only prevent FUTURE entries from getting saved into your ~/.bash_history file.

Update .inputrc file with a shortcut

Start typing git into your terminal. Then, press the up arrow. Your terminal is now populated with your most recently executed command.

It might be convenient to only see history that matches what you have typed in your terminal (doing a reverse-i-search using the term that is currently present).

We can update the ~/.inputrc file to get this desired effect.

~/.inputrc file

\e is an escape character.

[A and [B are escape codes for the up and down arrows. (see here for more escape codes)

Using comments in your commands

Do you work on different projects that use different commands? Or do you want a convenient way to filter your searches by category (rather than search term)?

You can do that with comments.

When running commands in your terminal, you can add comments with #.

Using comments

After doing this, you can use reverse-i-search to filter by these commands.

Using comments in our reverse-i-search

As usual, you can also use ctrl + r again to navigate through earlier entries in your history that match your search term.

Resources

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Responses (1)

Write a response