Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

For example:

"\e[1;5C"
"\e[Z"
"\e-1\C-i"

I only know bits and pieces, like \e stands for escape and C- for Ctrl, but what are these numbers (1) and letters (Z)? What are the ;, [ and - signs for?

Is there only trial and error, or is there a complete list of bash key codes and an explanation of their syntax?

share|improve this question
1  
An additional tip for listing the actual readline bindings of the shell is bind -p. –  Herman Torjussen May 21 '13 at 11:27
    
Most such codes are interpreted by your terminal; read about ANSI escape codes on Wikipedia for a large listing. –  chepner May 21 '13 at 14:07
    
stty -a displays the terminal's line settings –  bug May 22 '13 at 14:04
add comment

3 Answers

up vote 5 down vote accepted

Those are sequences of characters sent by your terminal when you press a given key. Nothing to do with bash or readline per se, but you'll want to know what sequence of characters a given key or key combination sends if you want to configure readline to do something upon a given key press.

When you press the A key, generally terminals send the a (0x61) character. If you press <Ctrl-I> or <Tab>, then generally send the ^I character also known as TAB or \t (0x9). Most of the function and navigation keys generally send a sequence of characters that starts with the ^[ (control-[), also known as ESC or \e (0x1b, 033 octal), but the exact sequence varies from terminal to terminal.

The best way to find out what a key or key combination sends for your terminal, is run sed -n l and to type it followed by Enter on the keyboard. Then you'll see something like:

$ sed -n l
^[[1;5A
\033[1;5A$

The first line is caused by the local terminal echo done by the terminal device (it may not be reliable as terminal device settings would affect it).

The second line is output by sed. The $ is not to be included, it's only to show you where the end of the line is.

Above that means that Ctrl-Up (which I've pressed) send the 6 characters ESC, [, 1, ;, 5 and A (0x1b 0x5b 0x31 0x3b 0x35 0x41)

The terminfo database records a number of sequences for a number of common keys for a number of terminals (based on $TERM value).

For instance:

TERM=rxvt tput kdch1 | sed -n l

Would tell you what escape sequence is send by rxvt upon pressing the Delete key.

You can look up what key corresponds to a given sequence with your current terminal with infocmp (here assuming ncurses infocmp):

$ infocmp -L1 | grep -F '=\E[Z'
    back_tab=\E[Z,
    key_btab=\E[Z,

Key combinations like Ctrl-Up don't have corresponding entries in the terminfo database, so to find out what they send, either read the source or documentation for the corresponding terminal or try it out with the sed -n l method descrive above.

share|improve this answer
    
This is a great explanation, thanks! Now it all falls into place, \e-1\C-i is a backwards tab, because control and i inserts a tab and escape followed by -1 says bash to do it backwards once (I goggled this and found some stuff about digit-arguments). –  bug May 22 '13 at 10:03
add comment

It is provided via gnu readline library. you should look into man 3 readline to find out its description.

Looks like you also need information about what does escspe codes liks \[A mean. You can find this information in wikipedia ANSI esacape code article.

share|improve this answer
    
This does not explain my examples. –  bug May 21 '13 at 12:40
    
Looks like it was small misunderstood. Please look at the answer update. –  rush May 21 '13 at 13:39
    
There is no information about my specific examples in the article. A page-search for "[A" returned nothing as well. –  bug May 21 '13 at 13:42
1  
@bug please read carefully the article. \[A is in the table "CSI codes" within row: "CSI n A" –  rush May 21 '13 at 13:44
    
Thanks rush, but where do I find explanations on [Z, [1;5C, or -1 ? –  bug May 22 '13 at 9:28
add comment

Do these codes come from the same source? The last one looks like a GNU readline binding. That's what the user sends to a bash (see rush's answer). The first two, however, look more like terminal control sequences (even though the first one would be an ill-formed one). That's what bash or another program send back to the terminal emulator in order to control cursor movements, text colors, and the like.

share|improve this answer
    
I found "\e[1;5C" to be used for ctrl-arrow forward word movement, and "\e[Z" and "\e-1\C-i" for menu-complete-backwards in other tutorials. They have all been used inside the .inputrc file. –  bug May 21 '13 at 12:37
    
OK, if they are interpreted by readline then there must be some terminal emulator where these control sequences are bound to some combination of ctrl, alt, and/or shift with function or arrow keys. (I don't know which one.) –  Uwe May 21 '13 at 13:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.