How do I search my command-line history for commands I used before?

Press Ctrl+R and type ssh. Ctrl+R will start search from most recent command to old one (reverse-search). If you have more than one command which starts with ssh, Press Ctrl+R again and again until you find the match.

Once you've found the match you can press Enter to execute the command or left / right cursor to just select the text of the command.

There is no default reverse option for Ctrl+R to invert the direction of the search but here you will find some suggestions about it.


If you just want to search your history, you can just use history | grep ssh, substituting ssh for whatever you want to search.


I do a slight variation of the above, works well for me (if you're referring to your bash history

In my home folder I create a file named

.inputrc

Inside goes this

"\e[5~": history-search-backward
"\e[6~": history-search-forward

Note: the above doesn't seem to work anymore in 14.04 so this does the same thing -

"\e[A":history-search-backward
"\e[B":history-search-forward

Then typing however much of a previous command I wish & using the page up/page dn buttons searchs the history, always starting with page up


Here's another method using classic commands (more likely to work across distros). Command history is stored in the file .bash_history in your home directory, so you can do this:

grep "ssh" ~/.bash_history

Don't forget the -i flag if you need case insensitive search.