Following my History and Aliases post, Brian Ashe emailed this:
bash has a built-in history search command. Just hit control-r and start typing. Once you have a match, pressing control-r again steps backwards through the stack. (Just like switching apps with command-tab, tab, tab.) For example, if the last five commands I entered were
I [can] type ‘control-r, ssh’ and ‘ssh 3’ would be active. If I then press control-r again, it would make ‘ssh 2’ active, and one more press would activate ‘ssh 1’. Note that it is case-sensitive.
I did not know this. My purpose in writing History and Aliases was introduce the concept of Bash’s
history and show how the
alias command can be used. If I had known about
control-r, I would’ve written pretty much the same post because
greping through history is a useful thing.
But Brian’s tip is very cool and I did not know about it.
Just hit control-r. When you do, your command prompt is replaced with:
Then start typing what you’re looking for. As Brian notes, the search is case-sensitive, so mail and Mail are two different things:
(reverse-i-search)`Mail’: /Applications/MailSteward.app/Contents/MacOS/MailSteward 1 >& /dev/null
I entered Mail, which appears after the closing parenthesis. The first match from history is presented to me. At this point, I can continue typing or hit
control-r to begin cycling through my history file. Only matches will be presented. The search starts at the most recent command and, as you hit
control-r, it searched upward through your history file. Once you hit the last match, it stops cycling.
Hit enter once you’ve found your match. Hit
control-c to cancel out and return to your command prompt.
That is very cool.
Thanks, Brian! This is something I’ll make good use of.
- Related Posts
- History And Aliases