Keys and key codes
What the arrow, function, Home, End and Ctrl keys send, and why Backspace and Delete confuse people.
In a VT session the keys are sent as bytes. Special keys send escape sequences.
Typing characters
- A printing key sends its character (as UTF-8 today).
Ctrl+ a letter sends a control code:Ctrl+Cis03(interrupt),Ctrl+Dis04(end of input),Ctrl+Zis1A(suspend),Ctrl+Lis0C(redraw),Ctrl+[isESC.Ctrl+SpaceandCtrl+@send00(NUL); if that key does nothing on the host, see Ctrl+Space not working over SSH or in tmux: the NUL byte and how to test it.Altheld with a key sendsESCbefore the character. Programs such as Emacs and readline call this Meta.Tabis09.Enteris usually0D(carriage return). Shift+Enter and Ctrl+Enter send the same byte; see Shift+Enter or Ctrl+Enter acts like Enter in a terminal: why, and how to fix it.
Cursor keys
Normal mode Application mode
Up ESC [ A ESC O A
Down ESC [ B ESC O B
Right ESC [ C ESC O C
Left ESC [ D ESC O D
Full-screen programs such as vim and less switch to application mode. Both are handled by a correct terminal. If arrow keys print ^[[A in a program, the terminal type is probably wrong.
Navigation and editing keys
Home ESC [ H (or ESC [ 1 ~)
End ESC [ F (or ESC [ 4 ~)
Insert ESC [ 2 ~
Delete ESC [ 3 ~
Page Up ESC [ 5 ~
Page Down ESC [ 6 ~
Function keys (xterm)
F1 ESC O P F2 ESC O Q F3 ESC O R F4 ESC O S
F5 ESC [ 15 ~ F6 ESC [ 17 ~ F7 ESC [ 18 ~ F8 ESC [ 19 ~
F9 ESC [ 20 ~ F10 ESC [ 21 ~ F11 ESC [ 23 ~ F12 ESC [ 24 ~
Linux console and some other terminal types use different sequences for the same keys. That is why the terminal type has to match what you tell the host.
Backspace and Delete
Hosts disagree about which byte erases a character:
- DEL (
7F) is the common choice today. - Backspace (
08, alsoCtrl+H) was the traditional one.
If Backspace prints ^H, or Delete prints ~, the terminal and the host disagree. Check the Backspace sends setting of your client. On the host, stty erase ^? selects DEL and stty erase ^H selects BS.
Hardware keyboards and F-keys
A phone or tablet keyboard may lack Esc, F1 to F12 or Ctrl. Use the client's key bar or on-screen keys for them. Esc matters in vim, and Ctrl+C interrupts a stuck command.
Related
- Escape sequences
- TERM and terminfo
- Terminal frozen after Ctrl+S: XON/XOFF flow control and Ctrl+Q
- Ctrl+D logs you out of the shell: IGNOREEOF and ignoreeof
- App guide: Utility key bar
- Shift+Enter or Ctrl+Enter acts like Enter in a terminal: why, and how to fix it
- Ctrl+Space not working over SSH or in tmux: the NUL byte and how to test it
- VT220 keypad on OpenVMS: PF1 (GOLD) to PF4 and application keypad from a PC or tablet
- Ctrl+Left and Ctrl+Right don't move by word over SSH: key codes and ~/.inputrc