Reverse Search History in Bash

Published in category Productivity
on Christian Mayer's Weblog.

You can search the history of previous entered commands in the Bash shell by simply hitting Ctrl+r keys. Enter a string you want to search for. By hitting Ctrl+r again you can search the history for a given string backwards to the end.

(reverse-i-search)`blog': cd ~/work/dev/blog/

The i stands for incremental.

This is a trick I use often on long lines. Consider this was your previous executed command and you now want to vi the same file:

$ ls ~/this/is/a/very/long/path

You simply want to change the ls to vi. Of course you can hit the Arrow-Up-key and then go the whole way backwards to the left beginning of the line using the Arrow-Left-key. (Or on OS X with bigger steps by holding down the Alt-key and pressing the Arrow-Left-key.) But you can do it in a more simpler way than that. Just press Ctrl+r and search for ls. Now you are instantly at the first position of the line. Hit Esc to exit the search and replace ls at the beginning with vi. Done.

Recent Posts

About the Author

Christian is a professional software developer living in Vienna, Austria. He loves coffee and is strongly addicted to music. In his spare time he writes open source software. He is known for developing automatic data processing systems for Debian Linux.