Shift+Tab on several terminals sends an escape sequence like this
ESC [ Z
It was used in 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 (relying on a table of termcap strings in bash's lib/readline/terminal.c
), but the termcap "kB"
is not part of that.
As an additional complication, a configuration change was made beginning in 2007 which caused shift-tab on the Linux console to send a different escape sequence, i.e.,
ESC TAB
That was reported in 2021, and a fix made to the terminal database. Doing that caused some pain for Emacs developers (see mailing list thread), whose program does check for "kB". But as of May 2022, that appears to be resolved.
Either way, bash ignores that termcap string.
When bash has no termcap information, it relies upon the strings in .inputrc
. Failing to find a match in either, that ESC
(escape) character will as noted exit insert-mode (that's vi after all).
The escape sequence used for this happens to be the same as the ECMA-48 standard back-tab control sequence. This also is documented in terminfo(5)
:
back_tab cbt bt back tab (P)
Using the same escape sequence for a special key as for a control sequence is not entirely accidental. Hardware terminals used to often provide a local-editing mode in which cursor-movement keys would move the cursor around on the screen without relying on the host to echo the special keys. About half of the terminal descriptions in ncurses' terminal database show this association between special-keys and control sequences:
439 entries with cbt == kcbt
73 entries with clear == kclr
383 entries with cub1 == kcub1
505 entries with cud1 == kcud1
885 entries with cuf1 == kcuf1
868 entries with cuu1 == kcuu1
174 entries with dch1 == kdch1
186 entries with dl1 == kdl1
502 entries with home == khome
52 entries with ich1 == kich1
139 entries with il1 == kil1
The counts for cub1
and cud1
are lower than cuf1
and cuu1
because terminal descriptions often use a 1-character backspace or line-feed in preference to a 2-3 character control sequence. Still, that 439 for back-tab says that about a quarter of the terminal descriptions use this particular feature.
Using shift-tab to send a back-tab escape sequence was not done first by Linux console. There are older entries such as the Ann Arbor and the AT&T entries which use this feature. Manuals for the latter on bitsavers are scarce, but the AT&T 610 and AT&T 630 manuals are available.