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