Reverse Search in Shell

I stumbled into this by accident. Somehow I pressed Ctrl+r and gotten this prompt in my Bash shell:

(reverse-i-search)`’:

That pushed me to research on what this Ctrl+r really does. Perhaps it was mentioned in some manual, but I just skipped as unimportant.

Ctrl+r allows one to search backwards in the recent history of the current shell. If the history is long it can save a lot of time going through the old commands. It is also a lot less cumbersome than history | grep “blah” mechanism. Just press this key combination and start typing. Multiple entries can be scrolled through by repeated Ctrl+r. Once you spot the target command, just press [enter] to execute it. Alternatively, using the right or left arrow keys will place the command on an actual command-line so you can edit it. Ctrl+g will abort the search and restore the original line.

Ctrl+r is supported by the GNU readline library. So any shell program, including your own creation, can have this history search functionality if this library is used.


Advertisements
This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s