Terminal Escape Characters – Meaning of ^ Character in Sequences Like ^X^I

escape-characterskeyboardterminal

I was reading this message from the zsh mailing list about key bindings and I'd like to know which key I need to press:

  1. ^X^I (I think Ctrl-X Ctrl-I, the capital X and I)
  2. ^[^@ (I think Ctrl-Esc-@ ??)
  3. ^X^[q(I think Ctrl-X Esc-q ??)
  4. ^XQ (I think Ctrl-X and Q ??)

From the Archlinux wiki page on zsh

  1. ^[[1;3A
  2. ^[[1;3D

From bindkey

  1. ^[[1;5C
  2. ^[[A

I know that ^[ means Esc, but I'm not sure how to find others.
Is there any official reference or website that lists these?

Best Answer

^c is a common notation for Ctrl+c where c is a (uppercase) letter or one of @[\]^_. It designates the corresponding control character. The correspondence is that the numeric code of the control character is the numeric code of the printable character (letter or punctuation symbol) minus 64, which corresponds to setting a bit to 0 in base 2. In addition, ^? often means character 127.

Some keys send a control character:

  • Escape = Ctrl+[
  • Tab = Ctrl+I
  • Return (or Enter or ) = Ctrl+M
  • Backspace = Ctrl+? or Ctrl+H (depending on the terminal configuration)

Alt (often called Meta because that was the name of the key at that position on historical Unix machines) plus a printable character sends ^[ (escape) followed by that character.

Most function and cursor keys send an escape sequence, i.e. the character ^[ followed by some printable characters. The details depend on the terminal and its configuration. For xterm, the defaults are documented in the manual. The manual is not beginner-friendly. Here are some tips to help:

  • CSI means ^[[, i.e. escape followed by open-bracket.
  • SS3 means ^[O, i.e. escape followed by uppercase-O.
  • "application mode" is something that full-screen programs usually turn on. Some keys send a different escape sequence in this mode, for historical reasons. (There are actually multiple modes but I won't go into a detailed discussion because in practice, if it matters, you can just bind the escape sequences of both modes, since there are no conflicts.)
  • Modifiers (Shift, Ctrl, Alt/Meta) are indicated by a numerical code. Insert a semicolon and that number just before the last character of the escape sequence. Taking the example in the documentation: F5 sends ^[[15~, and Shift+F5 sends ^[[15;2~. For cursor keys that send ^[[ and one letter X, to indicate a modifier M, the escape sequence is ^[[1;MX.

Xterm follows an ANSI standard which itself is based on historical usage dating back from physical terminals. Most modern terminal emulators follow that ANSI standard and implement some but not all of xterm's extensions. Do expect minor variations between terminals though.

Thus:

  • ^X^I = Ctrl+X Ctrl+I = Ctrl+X Tab
  • ^[^@ = Ctrl+Alt+@ = Escape Ctrl+@. On most terminals, Ctrl+Space also sends ^@ so ^[^@ = Ctrl+Alt+Space = Escape Ctrl+Space.
  • ^X^[q = Ctrl+X Alt+q = Ctrl+X Escape q
  • ^XQ = Ctrl+X Shift+q
  • ^[[A = Up
  • ^[[1;3A = Alt+Up (Up, with 1;M to indicate the modifier M). Note that many terminals don't actually send these escape sequences for Alt+cursor key.
  • ^[[1;3D = Alt+Left
  • ^[[1;5C = Ctrl+Right

There's no general, convenient way to look up the key corresponding to an escape sequence. The other way round, pressing Ctrl+V followed by a key chord at a shell prompt (or in many terminal-based editors) inserts the escape sequence literally.

See also How do keyboard input and text output work? and key bindings table?