PageUp and PageDown history search auto completion on the BASH shell
Posted November 14th, 2007 in Linux/Unix/BSD (Updated January 25th, 2008)
From the BASH shell it's possible to have auto-completion where you start to type in part of a command, and then use a keystroke sequence, such as PageUp or PageDown, to then cycle through the history for commands which started with the first text you have entered.
For example, if the last few commands you had entered at the BASH shell were as follows:
ls -l / ls -l /home ls -l /var ls -l /home/chris ls -l /var/spool/mail/
The entering ls -l and then PageUp would show all of the previously entered commands starting with ls -l in reverse order, ie the reverse of the items above.
If you entered ls -l /h and PageUp then you'd get ls -l /home/chris followed by ls -l /home.
It's then also possible to cycle forward through the commands again using the PageDown key. This is useful if you'd hit the PageUp key a few too many times and need to go back to one of the more recent commands in the history.
The key that is bound to this search forward and search backward history does not necesssarily have to be PageUp and PageDown, but I personally always set it to this as it's what I am used to.
The file that contains the settings for these BASH key bindings is located at /etc/inputrc, and by default on CentOS 5 looks like this:
# do not bell on tab-completion #set bell-style none set meta-flag on set input-meta on set convert-meta off set output-meta on # Completed names which are symbolic links to # directories have a slash appended. set mark-symlinked-directories on $if mode=emacs # for linux console and RH/Debian xterm "\e[1~": beginning-of-line "\e[4~": end-of-line "\e[5~": beginning-of-history "\e[6~": end-of-history "\e[3~": delete-char "\e[2~": quoted-insert "\e[5C": forward-word "\e[5D": backward-word "\e[1;5C": forward-word "\e[1;5D": backward-word # for rxvt "\e[8~": end-of-line # for non RH/Debian xterm, can't hurt for RH/DEbian xterm "\eOH": beginning-of-line "\eOF": end-of-line # for freebsd console "\e[H": beginning-of-line "\e[F": end-of-line $endif
The PageUp and PageDown keys are set with the "\e[5~": & "\e[6~": lines, so you'd need to comment them out and add new commands as follows:
#"\e[5~": beginning-of-history #"\e[6~": end-of-history "\e[5~": history-search-backward "\e[6~": history-search-forward
I haven't managed to work out how you reload these particular settings without logging out and logging back in again. Suggestions for how to do this are welcome.
Update November 28th 2007: I've also posted instructions for this on Ubuntu. The process is the same but the new commands are already in the file so its just a matter of commenting out and un-commenting some lines in the /etc/inputrc file.
Update January 25th 2007: The above modifies /etc/inputrc and makes the changes global in scope and therefore affects all users. To just change your own user profile you would create/edit a file in your home directory called .inputrc and put just the following lines in it (thanks to Isaac Emesowum for letting me know about this):
"\e[5~": history-search-backward "\e[6~": history-search-forward
Subscribe!
If you found this post interesting and would like to be notified the next time something is posted, please subscribe to my RSS Feed. Thanks for visiting!
