Developer's Daily | Unix by Example |
main | java | perl | unix | dev directory | web log |
console_codes ? Linux console escape and control sequences |
The Linux console implements a large subset of the VT102 and ECMA-48/ISO 6429/ANSI X3.64 terminal controls, plus certain private-mode sequences for changing the color palette, character-set mapping, etc. In the tabular descriptions below, the second column gives ECMA-48 or DEC mnemonics (the latter if prefixed with DEC) for the given function. Sequences without a mnemonic are neither ECMA-48 nor VT102. After all the normal output processing has been done, and a stream of characters arrives at the console driver for actual printing, the first thing that happens is a translation from the code used for processing to the code used for printing. If the console is in UTF-8 mode, then the incoming bytes are first assembled into 16-bit Unicode codes. Otherwise each byte is transformed according to the current mapping table (which translates it to a Unicode value). See the CHARACTER SETS section below for discussion. In the normal case, the Unicode value is converted to a font index, and this is stored in video memory, so that the corresponding glyph (as found in video ROM) appears on the screen. Note that the use of Unicode (and the design of the PC hardware) allows us to use 512 different glyphs simultaneously. If the current Unicode value is a control character, or we are currently processing an escape sequence, the value will treated specially. Instead of being turned into a font index and rendered as a glyph, it may trigger cursor movement or other control functions. See the LINUX CONSOLE CONTROLS section below for discussion. It is generally not good practice to hard-wire terminal controls into programs. Linux supports a terminfo(5) database of terminal capabilities. Rather than emitting console escape sequences by hand, you will almost always want to use a terminfo-aware screen library or utility such as ncurses(3), tput(1), or reset(1). |
This section describes all the control characters and escape sequences that invoke special functions (i.e. anything other than writing a glyph at the current cursor location) on the Linux console. |
Control characters |
A character is a control character if (before transformation according to the mapping table) it has one of the 14 codes 00 (NUL), 07 (BEL), 08 (BS), 09 (HT), 0a (LF), 0b (VT), 0c (FF), 0d (CR), 0e (SO), 0f (SI), 18 (CAN), 1a (SUB), 1b (ESC), 7f (DEL). One can set a ‘display control characters’ mode (see below), and allow 07, 09, 0b, 18, 1a, 7f to be displayed as glyphs. On the other hand, in UTF-8 mode all codes 00-1f are regarded as control characters, regardless of any ‘display control characters’ mode. If we have a control character, it is acted upon immediately and then discarded (even in the middle of an escape sequence) and the escape sequence continues with the next character. (However, ESC starts a new escape sequence, possibly aborting a previous unfinished one, and CAN and SUB abort any escape sequence.) The recognized control characters are BEL, BS, HT, LF, VT, FF, CR, SO, SI, CAN, SUB, ESC, DEL, CSI. They do what one would expect: |
BEL (0x07, ^G) beeps; |
BS (0x08, ^H) backspaces one column (but not past the beginning of the line); |
HT (0x09, ^I) goes to the next tab stop or to the end of the line if there is no earlier tab stop; |
LF (0x0A, ^J), VT (0x0B, ^K) and FF (0x0C, ^L) all give a linefeed; |
CR (0x0D, ^M) gives a carriage return; |
SO (0x0E, ^N) activates the G1 character set, and if LF/NL (new line mode) is set also a carriage return; |
SI (0x0F, ^O) activates the G0 character set; |
CAN (0x18, ^X) and SUB (0x1A, ^Z) interrupt escape sequences; |
ESC (0x1B, ^[) starts an escape sequence; |
DEL (0x7F) is ignored; |
CSI (0x9B) is equivalent to ESC [. |
ESC- but not CSI-sequences |
|
|
|
|
|
|
BUGS
SEE ALSO
devdaily.com, an alvin j. alexander production.
|