How can I set my BASH history scrolling to filter by what I've already typed?
Given a history of commands:
1 pwd
2 mysql -u root -p
3 ps -ef | more
4 top
5 mysql -h 192.168.1.101 -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.
Solution 1:
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
Solution 2:
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 192.168.1.101 -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)