Home / PageUp and PageDown history search auto completion on the BASH shell

PageUp and PageDown history search auto completion on the BASH shell

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

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

Update May 3rd 2010: I previously noted that "I haven’t managed to work out how you reload these particular settings without logging out and logging back in again." Thanks to Carl CH posting this on my Facebook page:

Exec replaces the current running process (bash in this case) with a new process. So, simply type

exec bash

and the currently running bash will be replaced by a new bash. The new bash reads the new /etc/inputrc file and now history-search works.