Bash – Why does “shift-tab” result in “Escape” in the terminal

bashescape-charactersreadlineterminal

I just noticed this by accident.

I use the vi readline mode (run set -o vi in bash) so it is particularly noticeable; in emacs readline mode I don't think Esc does anything, but in vi mode it exits insert mode (enters normal mode).

I don't see ShiftTab documented in man bash, and it would seem that its interpretation as an Esc key is at a more basic level than bash. Possibly in readline?

It works on Mac OS X as well as Linux.

Where can I find this documented?

Best Answer

Shift+Tab on several terminals sends an escape sequence like this

ESC [ Z

It has been part of the Linux console terminal since 1995, part of xterm since 2002, and used in terminals emulating one or the other of those since then.

In the terminfo description, this would be expressed as

kcbt=\E[Z,

and kcbt is documented in terminfo(5):

key_btab                  kcbt   kB   back-tab key

From the standpoint of parsing, there is no difference between this and pressing some function-key. readline does have special cases for several editing keys (seen after the call to tgetent in bash's lib/readline/terminal.c), but the termcap "kB" is not part of that.

Related Question