Given a history of commands:

1 pwd
2 mysql -u root -p
3 ps -ef | more
4 top
5 mysql -h -u root -p

When you press the Up arrow, you scroll through all those commands. I've read it somewhere before (and done it in my work PC) that you can set-up BASH such that when I first type p then press the Up arrow, it would only scroll through all commands in history that starts with "p" (pwd and ps -ef | more). When I type mysql then press Up, then it would only scroll through all commands starting with "mysql".

I'd like to set my laptop to use this, but I can't find the instructions again.


You can configure incremental searching in /etc/inputrc (for machine-wide changes), or ~/.inputrc (just for your user account).

Add these two lines:

"\e[A": history-search-backward
"\e[B": history-search-forward
| improve this answer | |
  • 5
    /etc/inputrc requires root to edit and makes machine-wide changes, ~/.inputrc, is editable by ordinary users and makes changes just for that user. – James Polley Jan 8 '10 at 4:18

Alternately, you could press ctrl+R to enter reverse-i-search mode. You can then start typing and it will find the first prior line that matches the string you type (ie, type m and it will jump back to mysql -h -u root -p; then type o and it will jump back to ps -ef | more as that's the first line with mo in.

You can hit ctrl+r again to search for the match before that (ie, ctrl+r,my,ctrl+r would take you to the second-last command starting with mysql)

You can read more about searching Readline's history search, commands for interacting with the history, and how you can modify the default keybindings and behavious in man bash (or online)

| improve this answer | |
  • works in zsh as well – akira Jan 8 '10 at 7:45
  • 1
    Yup, I already use Ctrl+R. but sometimes, after I begin typing a long command, I suddenly need to check all similar commands I've used before. This happens when I type in a mysql command and have to scroll through commands in history with different parameters (database names, output file, input file, etc). – Nikki Erwin Ramirez Jan 8 '10 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.