I'm wondering why Shift + Arrow Key result in one of the A, B, C or D letters.

Why not other letters? Why any letter at all?

  • Intriguing :D My bet? Has to do with text based gaming :D – Rinzwind Apr 23 '13 at 17:29
  • 2
    Just an observation: in tty Shift + Arrow Keys doesn't do anything. – Radu Rădeanu Apr 23 '13 at 17:33
  • @Rinzwind: :))) – Radu Rădeanu Apr 23 '13 at 17:36
  • 1
    I asked this question on Unix/Linux Stack Exchange. There are some useful answers. Maybe you want to look at them. unix.stackexchange.com/questions/73669/… – yfklon Apr 30 '13 at 5:35

Terminal emulators such as gnome-terminal ("Terminal" in Ubuntu) but also xterm and urxvt are named "emulators" because they reproduce the behavior of older terminals which were the only display of a computer. Such terminals communicated with the computer with a text-based protocol and were initially designed to receive only text. Quickly, more control was needed: how to erase a line? Use colors? Or change the cursor position?

VT-100 terminal Source: The Art of Unix Usability - Command-Line Interfaces

Escape sequences were designed for every terminal to do such things. They are named escape because they start with the ASCII escape code: 33. There's no way to print such a character directly, which makes it a good fit for such sequences. When we still want to display this characted, ^[[ is used, and this is what I'll use in my explanations.

When the terminal received ^[[A, it did not mean "print ^[[A", but "the user pressed the up-arrow key". "A" here is totally arbitrary: it just happens to be the letter everybody agreed on, because that's what the DEC VT-52 and its successor the popular VT-100 terminal used.

DEC VT-52 Maintenance Manuel 1976

Source: DEC VT-52 Maintenance Manual 1976

This is still the way terminal emulators work today: depending on the $TERM variable, a database named terminfo is responsible for saying what codes should be sent to the shell (bash, sh, zsh...), which is then responsible for understanding them and react to them.

Now, the code for Shift + up happens to be ^[[1;2A: the terminal emulator sends this code to the shell, which tries to interpret ^[[1;2 but does not display anything because it doesn't know about that escape sequence. But A is left and displayed.

  • 5
    Wow, interesting. Now all those ^[[A make sense when I click the arrow keys while a process is running on the terminal. – Dan Apr 24 '13 at 7:25
  • 4
    You may hit CTRL-v just before you hit SHIFT-UpArrow to 'view' the escape sequences in raw form. Unless you have changed the defaults, ESC should appear on your terminal screen as ^[ – arielf - Reinstate Monica Apr 26 '13 at 21:36
  • @arielf You can also run cat >/dev/null then type, and press Ctrl+C (or Enter then Ctrl+D) when done. – wjandrea Dec 11 '19 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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