Searching Shell History

You may end up in a situation where you’d like to run a command you’ve run before, but you aren’t quite sure the last time you ran it. You could hit the up key until you find it, but you can search your shell history instead. To do this, press Control+R. You should see the prompt like this:


At this point, you can start typing in the beginning of the command you’d like to find. For example, if I’m looking for the last gcc command I ran, I could type in “gcc”. The search will show the current matching result:

(reverse-i-search)`gcc': gcc -o main main.c

If that’s the command I want, I can just hit Enter to accept it. If not, I can cycle through matches by hitting Control+R repeatedly. If you want to accept the current match without running it, you can press esc, , or . If you don’t like any of the matches, you can exit search by hitting ctrl+G.