Efficiently search and select commands from bash history

Some time ago, during the Devoxx conference, I noticed a very interesting tool used by the speaker. When he wanted to type in the console a command, which he had already typed before, terminal was shown a list of prompts. This makes working in the console more efficient.

By default, when we want to invoke a command from history in the console, we can use the up and down arrows.

1git --version
2java --version
3uname -a

And next we click ⇧⇧⇧ and ENTER

Using the arrows to select a command from history

Shortcut CTRL+R

We can also use the shortcut CTRL+R and start typing a fragment of a command. The last command in the history containing the fragment of text you typed will be shown. Unfortunately, only one command is shown.

Let's assume that we typed the following commands into the console

1java -version
2git --version
3docker --version

When you type version after using the CTRL+R shortcut, you will only see the last command containing this fragment, i.e. docker --version.

Using CTRL+R shortcut

CTRL+R shortcut on steroids

With the hstr tool, we can see a list of commands from history that match the typed text fragment.
Additional advantages of the tool:

  • the list is sorted from the most frequently called commands, not by chronology,
  • the matching fragment of the command with the text you typed is highlighted in red,
  • you can enter several fragments of a command, separating them by a space.

Below is a demonstration of how the CTRL+R shortcut works after typing version.
Once the list of prompts is displayed, we can navigate with the up/down arrows.

Using CTRL+R shortcut after installing hstr tool


As of Ubuntu 21.10, this tool is available in the repositories by default:

1sudo apt install hstr

For older versions, run the commands:

1sudo add-apt-repository ppa:ultradvorka/ppa
2sudo apt-get update
3sudo apt-get install hstr
4hstr --show-configuration >> ~/.bashrc
5source ~/.bashrc