Originally, "tty" had two definitions: the hardware (now the emulator) and the driver (interfaced through /dev/pty* or /dev/tty*).
The hardware/emulator was/is responsible for:
- Taking a stream of data and presenting it; this included interpreting control sequences like "move cursor left", "blinking cursor", "clear-screen" although these control sequences were often different among manufacturers.
- Sending keycodes for keys typed by the user; most of these were standard ASCII characters, but some terminals sent propriety keycodes for even standard keys.
The 'tty' driver was responsible for:
- Managing the buffering, in raw or canonical mode; for example, buffering a line of characters until Enter is pressed.
- Managing the control flow; e.g. being able to stop/continue with Cntl-s/Cntl-q.
- Translating propriety keycodes to standard ASCII, where applicable.
- Intercepting certain control characters (like Cntl-c and Backspace) and processing them appropriately (send SIGINT on a Cntl-c or signal an EOF on Cntl-d.
- Canonical display of characters, for example, if
echo
is turned off, then do not send feedback (character typed) back to the terminal.
The terminfo and termcap databases managed what terminal control characters should be sent for an operation (like 'clear-screen'). These control sequences were/are not interpreted by the driver, but by the hardware/emulator.
In its default configuration, PuTTY sends cursor keys in normal mode, e.g., ^[OA
while screen sets the cursor keys to application mode, e.g., ^[[A
.
That is done by screen's sending the escape sequence to turn on the keypad (in termcap ks
, terminfo smkx
). That happens to turn on the application mode for both the numeric keypad and the cursor keys, because both are present in the ks/smkx
capability. screen uses this capability if it is present, when initializing the terminal. screen also follows up by sending the string in the termcap CS
capability if it is set, hinting that the original design was to use terminal descriptions where only the keypad was initialized by ks/smkx
.
You notice this difference because your key bindings expect the strings sent by the cursor keys in normal mode. If you modified the terminal description for screen to omit the part which changes the cursor keys, you could continue to use those bindings.
For instance, you could do this:
infocmp screen >foo
vi foo
and change the assignment
smkx=\E[?1h\E=,
to
smkx=\E=,
then
tic foo
to compile the modified description. If you do this as your (non-root) user, tic will put the compiled description in your ~/.terminfo
directory. There are drawbacks to this approach, and using tput
to obtain key-binding information is preferable. But it is simple.
Further reading:
Best Answer
When you press a key or key combination in a terminal, it is transmitted to the application running in the terminal as a sequence of one or more characters. For example, when you press a, the application receives
a
. When you press Enter, the application receives the characterCR
(a.k.a.^M
(pronounced “control-emm”), a.k.a. character number 13, a.k.a.\r
or\015
). Key combinations involving Alt are typically transmitted as the characterESC
(a.ka.^[
a.k.a.\e
or\033
) followed by the sequence for the key or key combination without Alt. Function keys and other key combinations are transmitted as escape sequences beginning with\e[
or\eO
.The escapes sequences are not fully standardized, and terminals typically ignore certain attributes for certain keys. For example, Ctrl+Shift+letter is often transmitted exactly like Ctrl+letter by default.
You can see what your terminal sends for a key combination by pressing Ctrl+V followed by that key combination in a shell prompt, or
C-q
orC-h c
followed by the key combination in Emacs.With some terminal emulators, you can configure the escape sequences for each key. On Xterm, this is done through X resources. Most setups read resources from
~/.Xresources
when X starts, and you can load the file manually withxrdb -merge ~/.Xresources
.A common convention uses escape sequences of the form
ESC [ number1 ; number2 ~
for function keys with modifiers.number1
indicates the function key (15
to24
for F5 to F12 — for historical reasons, F1 through F4 have different escape sequences) andnumber2
indicates the modifier (2
for Shift,3
for Meta,5
for Ctrl,7
for Ctrl+Meta, and add 1 for Shift with at least one of Ctrl or Meta).Emacs translates escape sequences into its internal key representation through
input-decode-map
orlocal-function-key-map
(orfunction-key-map
before Emacs 23).