1CONSOLE_CODES(4) Linux Programmer's Manual CONSOLE_CODES(4)
2
3
4
6 console_codes - Linux console escape and control sequences
7
9 The Linux console implements a large subset of the VT102 and
10 ECMA-48/ISO 6429/ANSI X3.64 terminal controls, plus certain private-
11 mode sequences for changing the color palette, character-set mapping,
12 etc. In the tabular descriptions below, the second column gives
13 ECMA-48 or DEC mnemonics (the latter if prefixed with DEC) for the
14 given function. Sequences without a mnemonic are neither ECMA-48 nor
15 VT102.
16
17 After all the normal output processing has been done, and a stream of
18 characters arrives at the console driver for actual printing, the first
19 thing that happens is a translation from the code used for processing
20 to the code used for printing.
21
22 If the console is in UTF-8 mode, then the incoming bytes are first
23 assembled into 16-bit Unicode codes. Otherwise each byte is trans‐
24 formed according to the current mapping table (which translates it to a
25 Unicode value). See the CHARACTER SETS section below for discussion.
26
27 In the normal case, the Unicode value is converted to a font index, and
28 this is stored in video memory, so that the corresponding glyph (as
29 found in video ROM) appears on the screen. Note that the use of Uni‐
30 code (and the design of the PC hardware) allows us to use 512 different
31 glyphs simultaneously.
32
33 If the current Unicode value is a control character, or we are cur‐
34 rently processing an escape sequence, the value will treated specially.
35 Instead of being turned into a font index and rendered as a glyph, it
36 may trigger cursor movement or other control functions. See the LINUX
37 CONSOLE CONTROLS section below for discussion.
38
39 It is generally not good practice to hard-wire terminal controls into
40 programs. Linux supports a terminfo(5) database of terminal capabili‐
41 ties. Rather than emitting console escape sequences by hand, you will
42 almost always want to use a terminfo-aware screen library or utility
43 such as ncurses(3), tput(1), or reset(1).
44
46 This section describes all the control characters and escape sequences
47 that invoke special functions (i.e. anything other than writing a glyph
48 at the current cursor location) on the Linux console.
49
50 Control characters
51 A character is a control character if (before transformation according
52 to the mapping table) it has one of the 14 codes 00 (NUL), 07 (BEL), 08
53 (BS), 09 (HT), 0a (LF), 0b (VT), 0c (FF), 0d (CR), 0e (SO), 0f (SI), 18
54 (CAN), 1a (SUB), 1b (ESC), 7f (DEL). One can set a `display control
55 characters' mode (see below), and allow 07, 09, 0b, 18, 1a, 7f to be
56 displayed as glyphs. On the other hand, in UTF-8 mode all codes 00-1f
57 are regarded as control characters, regardless of any `display control
58 characters' mode.
59
60 If we have a control character, it is acted upon immediately and then
61 discarded (even in the middle of an escape sequence) and the escape
62 sequence continues with the next character. (However, ESC starts a new
63 escape sequence, possibly aborting a previous unfinished one, and CAN
64 and SUB abort any escape sequence.) The recognized control characters
65 are BEL, BS, HT, LF, VT, FF, CR, SO, SI, CAN, SUB, ESC, DEL, CSI. They
66 do what one would expect:
67
68 BEL (0x07, ^G) beeps;
69
70 BS (0x08, ^H) backspaces one column (but not past the beginning of the
71 line);
72
73 HT (0x09, ^I) goes to the next tab stop or to the end of the line if
74 there is no earlier tab stop;
75
76 LF (0x0A, ^J), VT (0x0B, ^K) and FF (0x0C, ^L) all give a linefeed;
77
78 CR (0x0D, ^M) gives a carriage return;
79
80 SO (0x0E, ^N) activates the G1 character set, and if LF/NL (new line
81 mode) is set also a carriage return;
82
83 SI (0x0F, ^O) activates the G0 character set;
84
85 CAN (0x18, ^X) and SUB (0x1A, ^Z) interrupt escape sequences;
86
87 ESC (0x1B, ^[) starts an escape sequence;
88
89 DEL (0x7F) is ignored;
90
91 CSI (0x9B) is equivalent to ESC [.
92
93 ESC- but not CSI-sequences
94 ESC c RIS Reset.
95 ESC D IND Linefeed.
96 ESC E NEL Newline.
97 ESC H HTS Set tab stop at current column.
98 ESC M RI Reverse linefeed.
99 ESC Z DECID DEC private identification. The kernel returns the
100 string ESC [ ? 6 c, claiming that it is a VT102.
101 ESC 7 DECSC Save current state (cursor coordinates,
102 attributes, character sets pointed at by G0, G1).
103 ESC 8 DECRC Restore state most recently saved by ESC 7.
104 ESC [ CSI Control sequence introducer
105 ESC % Start sequence selecting character set
106 ESC % @ Select default (ISO 646 / ISO 8859-1)
107 ESC % G Select UTF-8
108 ESC % 8 Select UTF-8 (obsolete)
109 ESC # 8 DECALN DEC screen alignment test - fill screen with E's.
110 ESC ( Start sequence defining G0 character set
111 ESC ( B Select default (ISO 8859-1 mapping)
112 ESC ( 0 Select VT100 graphics mapping
113 ESC ( U Select null mapping - straight to character ROM
114 ESC ( K Select user mapping - the map that is loaded by
115 the utility mapscrn(8).
116 ESC ) Start sequence defining G1
117 (followed by one of B, 0, U, K, as above).
118 ESC > DECPNM Set numeric keypad mode
119 ESC = DECPAM Set application keypad mode
120 ESC ] OSC (Should be: Operating system command) ESC ] P
121 nrrggbb: set palette, with parameter given in 7
122 hexadecimal digits after the final P :-(. Here n
123 is the color (0-15), and rrggbb indicates the
124 red/green/blue values (0-255). ESC ] R: reset
125 palette
126
127 ECMA-48 CSI sequences
128 CSI (or ESC [) is followed by a sequence of parameters, at most NPAR
129 (16), that are decimal numbers separated by semicolons. An empty or
130 absent parameter is taken to be 0. The sequence of parameters may be
131 preceded by a single question mark.
132
133 However, after CSI [ (or ESC [ [) a single character is read and this
134 entire sequence is ignored. (The idea is to ignore an echoed function
135 key.)
136
137 The action of a CSI sequence is determined by its final character.
138
139 @ ICH Insert the indicated # of blank characters.
140 A CUU Move cursor up the indicated # of rows.
141 B CUD Move cursor down the indicated # of rows.
142 C CUF Move cursor right the indicated # of columns.
143 D CUB Move cursor left the indicated # of columns.
144 E CNL Move cursor down the indicated # of rows, to column 1.
145 F CPL Move cursor up the indicated # of rows, to column 1.
146 G CHA Move cursor to indicated column in current row.
147 H CUP Move cursor to the indicated row, column (origin at 1,1).
148 J ED Erase display (default: from cursor to end of display).
149 ESC [ 1 J: erase from start to cursor.
150 ESC [ 2 J: erase whole display.
151 K EL Erase line (default: from cursor to end of line).
152 ESC [ 1 K: erase from start of line to cursor.
153 ESC [ 2 K: erase whole line.
154 L IL Insert the indicated # of blank lines.
155 M DL Delete the indicated # of lines.
156 P DCH Delete the indicated # of characters on the current line.
157 X ECH Erase the indicated # of characters on the current line.
158 a HPR Move cursor right the indicated # of columns.
159 c DA Answer ESC [ ? 6 c: `I am a VT102'.
160 d VPA Move cursor to the indicated row, current column.
161 e VPR Move cursor down the indicated # of rows.
162 f HVP Move cursor to the indicated row, column.
163 g TBC Without parameter: clear tab stop at the current position.
164 ESC [ 3 g: delete all tab stops.
165 h SM Set Mode (see below).
166 l RM Reset Mode (see below).
167 m SGR Set attributes (see below).
168 n DSR Status report (see below).
169 q DECLL Set keyboard LEDs.
170 ESC [ 0 q: clear all LEDs
171 ESC [ 1 q: set Scroll Lock LED
172 ESC [ 2 q: set Num Lock LED
173 ESC [ 3 q: set Caps Lock LED
174 r DECSTBM Set scrolling region; parameters are top and bottom row.
175 s ? Save cursor location.
176 u ? Restore cursor location.
177 ` HPA Move cursor to indicated column in current row.
178
179 ECMA-48 Set Graphics Rendition
180 The ECMA-48 SGR sequence ESC [ parameters m sets display attributes.
181 Several attributes can be set in the same sequence, separated by semi‐
182 colons. An empty parameter (between semicolons or string initiator or
183 terminator) is interpreted as a zero.
184
185 param result
186 0 reset all attributes to their defaults
187 1 set bold
188 2 set half-bright (simulated with color on a color display)
189 4 set underscore (simulated with color on a color display)
190 (the colors used to simulate dim or underline are set
191 using ESC ] ...)
192 5 set blink
193 7 set reverse video
194 10 reset selected mapping, display control flag, and toggle
195 meta flag (ECMA-48 says "primary font").
196 11 select null mapping, set display control flag, reset tog‐
197 gle meta flag (ECMA-48 says "first alternate font").
198
199 12 select null mapping, set display control flag, set toggle
200 meta flag (ECMA-48 says "second alternate font"). The
201 toggle meta flag causes the high bit of a byte to be tog‐
202 gled before the mapping table translation is done.
203 21 set normal intensity (ECMA-48 says "doubly underlined")
204 22 set normal intensity
205 24 underline off
206 25 blink off
207 27 reverse video off
208 30 set black foreground
209 31 set red foreground
210 32 set green foreground
211 33 set brown foreground
212 34 set blue foreground
213 35 set magenta foreground
214 36 set cyan foreground
215 37 set white foreground
216 38 set underscore on, set default foreground color
217 39 set underscore off, set default foreground color
218 40 set black background
219 41 set red background
220 42 set green background
221 43 set brown background
222 44 set blue background
223 45 set magenta background
224 46 set cyan background
225 47 set white background
226 49 set default background color
227
228 ECMA-48 Mode Switches
229 ESC [ 3 h
230 DECCRM (default off): Display control chars.
231
232 ESC [ 4 h
233 DECIM (default off): Set insert mode.
234
235 ESC [ 20 h
236 LF/NL (default off): Automatically follow echo of LF, VT or FF
237 with CR.
238
239 ECMA-48 Status Report Commands
240 ESC [ 5 n
241 Device status report (DSR): Answer is ESC [ 0 n (Terminal OK).
242
243 ESC [ 6 n
244 Cursor position report (CPR): Answer is ESC [ y ; x R, where x,y
245 is the cursor location.
246
247 DEC Private Mode (DECSET/DECRST) sequences.
248 These are not described in ECMA-48. We list the Set Mode sequences;
249 the Reset Mode sequences are obtained by replacing the final `h' by
250 `l'.
251
252 ESC [ ? 1 h
253 DECCKM (default off): When set, the cursor keys send an ESC O
254 prefix, rather than ESC [.
255
256 ESC [ ? 3 h
257 DECCOLM (default off = 80 columns): 80/132 col mode switch. The
258 driver sources note that this alone does not suffice; some user-
259 mode utility such as resizecons(8) has to change the hardware
260 registers on the console video card.
261
262 ESC [ ? 5 h
263 DECSCNM (default off): Set reverse-video mode.
264
265 ESC [ ? 6 h
266 DECOM (default off): When set, cursor addressing is relative to
267 the upper left corner of the scrolling region.
268
269 ESC [ ? 7 h
270 DECAWM (default on): Set autowrap on. In this mode, a graphic
271 character emitted after column 80 (or column 132 of DECCOLM is
272 on) forces a wrap to the beginning of the following line first.
273
274 ESC [ ? 8 h
275 DECARM (default on): Set keyboard autorepreat on.
276
277 ESC [ ? 9 h
278 X10 Mouse Reporting (default off): Set reporting mode to 1 (or
279 reset to 0) — see below.
280
281 ESC [ ? 25 h
282 DECTECM (default on): Make cursor visible.
283
284 ESC [ ? 1000 h
285 X11 Mouse Reporting (default off): Set reporting mode to 2 (or
286 reset to 0) — see below.
287
288 Linux Console Private CSI Sequences
289 The following sequences are neither ECMA-48 nor native VT102. They are
290 native to the Linux console driver. Colors are in SGR parameters: 0 =
291 black, 1 = red, 2 = green, 3 = brown, 4 = blue, 5 = magenta, 6 = cyan,
292 7 = white.
293
294 ESC [ 1 ; n ] Set color n as the underline color
295 ESC [ 2 ; n ] Set color n as the dim color
296 ESC [ 8 ] Make the current color pair the default attributes.
297 ESC [ 9 ; n ] Set screen blank timeout to n minutes.
298 ESC [ 10 ; n ] Set bell frequency in Hz.
299 ESC [ 11 ; n ] Set bell duration in msec.
300 ESC [ 12 ; n ] Bring specified console to the front.
301 ESC [ 13 ] Unblank the screen.
302 ESC [ 14 ; n ] Set the VESA powerdown interval in minutes.
303
305 The kernel knows about 4 translations of bytes into console-screen sym‐
306 bols. The four tables are: a) Latin1 -> PC, b) VT100 graphics -> PC,
307 c) PC -> PC, d) user-defined.
308
309 There are two character sets, called G0 and G1, and one of them is the
310 current character set. (Initially G0.) Typing ^N causes G1 to become
311 current, ^O causes G0 to become current.
312
313 These variables G0 and G1 point at a translation table, and can be
314 changed by the user. Initially they point at tables a) and b), respec‐
315 tively. The sequences ESC ( B and ESC ( 0 and ESC ( U and ESC ( K
316 cause G0 to point at translation table a), b), c) and d), respectively.
317 The sequences ESC ) B and ESC ) 0 and ESC ) U and ESC ) K cause G1 to
318 point at translation table a), b), c) and d), respectively.
319
320 The sequence ESC c causes a terminal reset, which is what you want if
321 the screen is all garbled. The oft-advised "echo ^V^O" will only make
322 G0 current, but there is no guarantee that G0 points at table a). In
323 some distributions there is a program reset(1) that just does "echo
324 ^[c". If your terminfo entry for the console is correct (and has an
325 entry rs1=\Ec), then "tput reset" will also work.
326
327 The user-defined mapping table can be set using mapscrn(8). The result
328 of the mapping is that if a symbol c is printed, the symbol s = map[c]
329 is sent to the video memory. The bitmap that corresponds to s is found
330 in the character ROM, and can be changed using setfont(8).
331
333 The mouse tracking facility is intended to return xterm-compatible
334 mouse status reports. Because the console driver has no way to know
335 the device or type of the mouse, these reports are returned in the con‐
336 sole input stream only when the virtual terminal driver receives a
337 mouse update ioctl. These ioctls must be generated by a mouse-aware
338 user-mode application such as the gpm(8) daemon.
339
340 The mouse tracking escape sequences generated by xterm encode numeric
341 parameters in a single character as value+040. For example, `!' is 1.
342 The screen coordinate system is 1-based.
343
344 The X10 compatibility mode sends an escape sequence on button press
345 encoding the location and the mouse button pressed. It is enabled by
346 sending ESC [ ? 9 h and disabled with ESC [ ? 9 l. On button press,
347 xterm sends ESC [ M bxy (6 characters). Here b is button-1, and x and
348 y are the x and y coordinates of the mouse when the button was pressed.
349 This is the same code the kernel also produces.
350
351 Normal tracking mode (not implemented in Linux 2.0.24) sends an escape
352 sequence on both button press and release. Modifier information is
353 also sent. It is enabled by sending ESC [ ? 1000 h and disabled with
354 ESC [ 1000 l. On button press or release, xterm sends ESC [ M bxy.
355 The low two bits of b encode button information: 0=MB1 pressed, 1=MB2
356 pressed, 2=MB3 pressed, 3=release. The upper bits encode what modi‐
357 fiers were down when the button was pressed and are added together:
358 4=Shift, 8=Meta, 16=Control. Again x and y are the x and y coordinates
359 of the mouse event. The upper left corner is (1,1).
360
362 Many different terminal types are described, like the Linux console, as
363 being `VT100-compatible'. Here we discuss differences between the
364 Linux console and the two most important others, the DEC VT102 and
365 xterm(1).
366
367 Control-character handling
368 The VT102 also recognized the following control characters:
369
370 NUL (0x00) was ignored;
371
372 ENQ (0x05) triggered an answerback message;
373
374 DC1 (0x11, ^Q, XON) resumed transmission;
375
376 DC3 (0x13, ^S, XOFF) caused VT100 to ignore (and stop transmitting) all
377 codes except XOFF and XON.
378
379 VT100-like DC1/DC3 processing may be enabled by the tty driver.
380
381 The xterm program (in VT100 mode) recognizes the control characters
382 BEL, BS, HT, LF, VT, FF, CR, SO, SI, ESC.
383
384 Escape sequences
385 VT100 console sequences not implemented on the Linux console:
386
387 ESC N SS2 Single shift 2. (Select G2 character set for the next
388 character only.)
389 ESC O SS3 Single shift 3. (Select G3 character set for the next
390 character only.)
391 ESC P DCS Device control string (ended by ESC \)
392 ESC X SOS Start of string.
393 ESC ^ PM Privacy message (ended by ESC \)
394 ESC \ ST String terminator
395 ESC * ... Designate G2 character set
396
397 ESC + ... Designate G3 character set
398
399 The program xterm (in VT100 mode) recognizes ESC c, ESC # 8, ESC >, ESC
400 =, ESC D, ESC E, ESC H, ESC M, ESC N, ESC O, ESC P ... ESC ESC Z (it
401 answers ESC [ ? 1 ; 2 c, `I am a VT100 with advanced video option') and
402 ESC ^ ... ESC with the same meanings as indicated above. It accepts
403 ESC (, ESC ), ESC *, ESC + followed by 0, A, B for the DEC special
404 character and line drawing set, UK, and US-ASCII, respectively.
405
406 The user can configure xterm to respond to VT220-specific control
407 sequences, and it will identify itself as a VT52, VT100, and up depend‐
408 ing on the way it is configured and initialized.
409
410 It accepts ESC ] (OSC) for the setting of certain resources. In addi‐
411 tion to the ECMA-48 string terminator (ST), xterm accepts a BEL to ter‐
412 minate an OSC string. These are a few of the OSC control sequences
413 recognized by xterm:
414
415 ESC ] 0 ; txt ST Set icon name and window title to txt.
416 ESC ] 1 ; txt ST Set icon name to txt.
417 ESC ] 2 ; txt ST Set window title to txt.
418 ESC ] 4 ; num; txt ST Set ANSI color num to txt.
419 ESC ] 10 ; txt ST Set dynamic text color to txt.
420 ESC ] 4 6 ; name ST Change log file to name (normally disabled
421 by a compile-time option)
422 ESC ] 5 0 ; fn ST Set font to fn.
423
424 It recognizes the following with slightly modified meaning (saving more
425 state, behaving closer to VT100/VT220):
426
427 ESC 7 DECSC Save cursor
428 ESC 8 DECRC Restore cursor
429
430 It also recognizes
431
432 ESC F Cursor to lower left corner of screen (if enabled by
433 xterm's hpLowerleftBugCompat resource)
434 ESC l Memory lock (per HP terminals).
435 Locks memory above the cursor.
436 ESC m Memory unlock (per HP terminals).
437 ESC n LS2 Invoke the G2 character set.
438 ESC o LS3 Invoke the G3 character set.
439 ESC | LS3R Invoke the G3 character set as GR.
440 ESC } LS2R Invoke the G2 character set as GR.
441 ESC ~ LS1R Invoke the G1 character set as GR.
442
443 It also recognizes ESC % and provides a more complete UTF-8 implementa‐
444 tion than Linux console.
445
446 CSI Sequences
447 Old versions of xterm, e.g., from X11R5, interpret the blink SGR as a
448 bold SGR. Later versions which implemented ANSI colors, e.g., XFree86
449 3.1.2A in 1995, improved this by allowing the blink attribute to be
450 displayed as a color. Modern versions of xterm implement blink SGR as
451 blinking text and still allow colored text as an alternate rendering of
452 SGRs. Stock X11R6 versions did not recognize the color-setting SGRs
453 until the X11R6.8 release, which incorporated XFree86 xterm. All
454 ECMA-48 CSI sequences recognized by Linux are also recognized by xterm,
455 however xterm implements several ECMA-48 and DEC control sequences not
456 recognized by Linux.
457
458 The xterm program recognizes all of the DEC Private Mode sequences
459 listed above, but none of the Linux private-mode sequences. For dis‐
460 cussion of xterm's own private-mode sequences, refer to the Xterm Con‐
461 trol Sequences document by Edward Moy, Stephen Gildea, and Thomas E.
462 Dickey available with the X distribution. That document, though terse,
463 is much longer than this manual page. For a chronological overview,
464
465 http://invisible-island.net/xterm/xterm.log.html
466
467 details changes to xterm.
468
469 The vttest program
470
471 http://invisible-island.net/vttest/
472
473 demonstrates many of these control sequences. The xterm source distri‐
474 bution also contains sample scripts which exercise other features.
475
477 ESC 8 (DECRC) is not able to restore the character set changed with ESC
478 %.
479
481 In 2.0.23, CSI is broken, and NUL is not ignored inside escape
482 sequences.
483
484 Some older kernel versions (after 2.0) interpret 8-bit control
485 sequences. These "C1 controls" use codes between 128 and 159 to
486 replace ESC [, ESC ] and similar two-byte control sequence initiators.
487 There are fragments of that in modern kernels (either overlooked or
488 broken by changes to support UTF-8), but the implementation is incom‐
489 plete and should be regarded as unreliable.
490
491 Linux "private mode" sequences do not follow the rules in ECMA-48 for
492 private mode control sequences. In particular, those ending with ] do
493 not use a standard terminating character. The OSC (set palette)
494 sequence is a greater problem, since xterm may interpret this as a con‐
495 trol sequence which requires a string terminator (ST). Unlike the set‐
496 term sequences which will be ignored (since they are invalid control
497 sequences), the palette sequence will make xterm appear to hang (though
498 pressing the return-key will fix that). To accommodate applications
499 which have been hardcoded to use Linux control sequences, set the xterm
500 resource brokenLinuxOSC to true.
501
502 An older version of this document implied that Linux recognizes the
503 ECMA-48 control sequence for invisible text. It is ignored.
504
506 console(4), console_ioctl(4), charsets(7)
507
508
509
510Linux 2006-05-29 CONSOLE_CODES(4)