History search backward/forward for bash

One way to make the up/down arrow more functional in BASH environment is to assign the history search binding to them. But in my  Gentoo box (w/ long usage for about 2 years), the configuration is just:

$ vim ~/.inputrc

And then add the settings:

"\e[A": history-search-backward
"\e[B": history-search-forward

After re-login, the function works.

But in my new install Ubuntu box (10.04, 10.10, 11.04), it is not work. For some references: bash 記憶指令 For Linux, the solution is to use the HEX code of the keys not he old binding method (maybe for some API changes?). The detail step lists:

1. Find out the HEX code of up/down arrow. With this command:

$ showkey -a

and then keystroke them, you will get the output like:

^[[A      27 0033 0x1b
          91 0133 0x5b
          65 0101 0x41
^[[B      27 0033 0x1b
          91 0133 0x5b
          66 0102 0x42

2. Set up the hex code with bindings:

$ vim ~/.inputrc

"\x1b\x5b\x41":history-search-backward
"\x1b\x5b\x42":history-search-forward

Leave a Reply

Your email address will not be published. Required fields are marked *