Searching bash history with prefix

6 Jul

On the console, arrow up and down usually allow to browse through the bash history.
The following entry to ~/.bashrc allows a similar behaviour with page up and down, but only browsing through the part of the history that starts with the string left of the cursor position.

if [[ $- == *i* ]]
then
    bind '"\e[5~": history-search-backward'
    bind '"\e[6~": history-search-forward'
fi

We can alternatively get the same functionality for all programs using the readline library (including bash) by extending ~/.inputrc with

"\e[5~": history-search-backward
"\e[6~": history-search-forward

These changes only have an effect after restarting the bash/console.