Better bindings for command line history search
Do you find it confusing to use Ctrl+r
for searching a command from shell history? I have the following mappings in the ~/.inputrc
file, so that I can use the ↑
and ↓
arrow keys instead.
"\e[A": history-search-backward
"\e[B": history-search-forward
Normally, when you use the ↑
arrow key, you'll get the previous command from the history. You can repeatedly press the key to get more entries. With the above settings active, this behavior will change if you have some characters already typed before pressing the arrow key — you'll only get entries from the history that match these characters from the start of the command. If there are multiple matches, you can use the ↑
and ↓
keys repeatedly to move backwards and forwards through the list.
If you want the matching to happen anywhere in command (same behavior as Ctrl+r
and Ctrl+s
), use the following lines instead:
"\e[A": history-substring-search-backward
"\e[B": history-substring-search-forward
The ~/.inputrc
file affects any shell using the readline
library (for example, programming language REPLs). You can use the bind
command and put them in, ~/.bashrc
for example, to affect only that particular shell.
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
See wiki.archlinux: readline for more details and examples.