Reverse searching Bash history with Ctrl-R

In the default GNU Bash shell (and many others), pressing Ctrl-R triggers reverse incremental history search. It lets you search your command history interactively, as you type.

Try it:

(reverse-i-search)`':

Now start typing any part of a previous command, for example ssh. Bash will live-search backward in your history and show the most recent match:

(reverse-i-search)`ssh': ssh root@192.168.1.1

Things you can do:

  • Press Ctrl-R again to go further back in history with the same query.
  • Press Enter to run the shown command.
  • Press the right arrow to place the shown command on your prompt to edit it before running.
  • Press Ctrl-G to cancel the search and return to your original prompt.

Note that commands are matched anywhere in the line (not just from the beginning).

Technically you can do a forward search with Ctrl-S, but it’s usually intercepted by terminal flow control (XON/XOFF). To disable that:

stty -ixon

That frees up Ctrl-S for forward search.