As far as keyboard shortcuts go, we believe we have saved the best for last. Out of all the time-savers we have so far introduced, this is by far the coolest as far as we're concerned: reverse searching.
A reverse search allows you to go back through your history, and search for a string within your executed commands. You can think of this as similar to history | grep cat, but much more interactive and much faster. To enter the reverse search prompt, the keys CTRL+r are used:
reader@ubuntu:~$ # CTRL+r
(reverse-i-search)'': # Start typing now.
(reverse-i-search)'cat': cat /var/log/dpkg.log # Press CTRL+r again for next match.
(reverse-i-search)'cat': sudo cat /etc/shadow # When match is found, press ENTER to execute.
reader@ubuntu:~$ sudo cat /etc/shadow
root:*:17647:0:99999:7:::
daemon:*:17647:0:99999:7:::
bin:*:17647:0:99999:7:::
<SNIPPED>
Please go ahead and give it a try. It is hard to get these interactive prompts down on paper, so we hope the comments above give a good indication of how a reverse search works. You can reverse search all the way up to the beginning of your history. If, at that point, you press CTRL+r again, you'll see something like the following:
(failed reverse-i-search)'cat': cat base-crontab.txt
This signifies to you that there are no more matches for reverse search to find. At this point, or before if you think you're taking too long, you can always press CTRL+c to stop reverse searching.
By contrast with the !ls syntax, reverse search will not start looking for the keywords only from the beginning of the line:
reverse-i-search)'ls': cat grep-then-else.sh
This means that it is both more powerful (it just matches anywhere in the command) and more complicated to use (it does not match just the command). However, if you're smart about this and you just want commands, you can always use a well-placed whitespace to make sure situations like the example above do not happen:
(reverse-i-search)'ls ': ls -al /tmp/new # Note the whitespace after ls.
While we'd love to talk more about reverse searching, the only true way for you to learnĀ itĀ properly is to start using it. Rest assured, if you get proficient in its use (and also know when to stop searching and just type in the command you're looking for), you'll be sure to impress your peers with your efficient Terminal work!