Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to set a key binding in bash for "history-search-backward" readline command to a combination of Control+some other key (I'm using 2 as an example), but I'm unable to do so.

(edit: it seems the problem was my choice of 2 as the example key. I tried with \C-l and it's working. I'll still accept an answer if someone explains why 2 doesn't work)

After several tries my ~/.inputrc now looks like this

set bind-tty-special-chars off
"\C-2": history-search-backward

but it doesn't work and bind -p | grep "-2" gives nothing. If I try something without the control key, it works:

"C-2": history-search-backward

I can search in the history by prssing the sequence C + - + 2.

bind -p gives control in \C form, for example:

"\C-w": unix-word-rubout

I've tried different formats in my inputrc:

Control-2: history-search-backward
Ctrl-2: history-search-backward
"Control-2": history-search-backward

but nothing works.

"\e2": history-search-backward

works if I press Escape followed by 2.

Can anyone help?

Fedora 11:
Bash version 4.0.23(1)
GNU Readline 5.2 (according to the man page)

share|improve this question
Perhaps you have found a bug? I'd try checking the Fedora user forums. My answer incorrectly assumed that inputrc was a bash-thing and not part of readline. – J. Polfer Aug 19 '09 at 18:28
You might want to add that "\c2" doesn't work to your list, as I deleted my incorrect answer. It's kinda an Edison-making-lightbulb-thing, but at least you know it doesn't work. – J. Polfer Aug 19 '09 at 18:30
up vote 5 down vote accepted

There's no ASCII code for Control-2. Control-@ through Control-_ correspond to control codes 0x00 (NUL) through 0x1F (Unit Separator). For example, the code for Control-I is the code for 'I' (0x49) minus 0x40 = 0x09 (HT, aka tab). There's no set definition for Control+(some other character not in the @ to _ block).

Programs that do their own keyboard handling can interpret Control any way they like in combination with any other keys. But programs like bash, which read their input through a terminal, don't have any way of even seeing Control-2.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .