Bind the bash history methods

Bash is an awesome shell and also very configurable. Some of bash’s build-in functions are not binded to keys, some of which REALLY should be!

Bash has two very useful functions used for searching your bash history:

  • history-search-backward
  • history-search-forward

So the question is how do we bind them. I bound them to Control-<up> and Control-<down>. To do this add the following lines to your ~/.inputrc file:
"\e[1;5A": history-search-backward
"\e[1;5B": history-search-forward

Note: To test it out you can use the bind command for your current shell:
bind "\e[1;5A": history-search-backward

If you want to use a different key combination then you can use the ‘read’ command to print it out, for example runnding the command read, then hitting control-<up>:

matt@wks1005847 ~ $ read

When we bind the key we replace the ‘^[‘ with a ‘\e’ as ‘^[‘ can match the <Escape> key.


#1 Chris on 01.04.11 at 7:05 pm

Cool. Fedora does this out of the box with Pg-Up/Down 🙂


#2 Kevin Brubeck Unhammer on 01.04.11 at 7:22 pm

What’s the difference between this and just the default C-s / C-r
history search?

#3 matt on 01.05.11 at 3:20 pm

Well the reverse look up allows you to do a reverse search, but if you match something you can’t go back to a further back match.
I know zsh allows this behavior with its reverse-lookup.

This allows you to continue matching the command/text you started writing, if that makes sense. Plus I think C-/ is useful as and are already history commands, so this is a good extension.
Further C-s on my machine suspends my terminal, so.. yeah.

@Chris: My Fedora machine doesn’t have this mapped, maybe I need to upgrade the 14 then check it out, thanks 🙂

Leave a Comment