1 Control Functions
liquibyte edited this page 2026-11-19 16:23:30 -06:00

Control Functions

The reference of what the terminal does with what a program writes: the C0 controls, the escape sequences, the control sequences, the modes, and the OSC, DCS and APC strings, with the answers the terminal gives. The functions and their meaning come from ECMA-48, the DEC VT manuals and xterm's control sequences; where they differ, Hosho does what xterm does, because programs are tested against it.

1. How the bytes are read

  • The input is UTF-8. A byte sequence that is not correct UTF-8 gives U+FFFD. A C1 control written as UTF-8 (U+0080 to U+009F) is read as ESC and the matching byte (U+009B is CSI).
  • The parser is the state machine of Paul Flo Williams' "A parser for DEC's ANSI-compatible video terminals", with xterm's changes: : separates sub-parameters (SGR 38:2::r:g:b), BEL also ends an OSC string, and CAN, SUB and an ESC that does not start ESC \ cancel a string.
  • A sequence keeps at most 32 parameters: the parameters after the 32nd are dropped, and the sequence is still done. A sequence with more than 2 intermediate bytes, or with two private markers, is dropped. A parameter larger than 65535 counts as 65535.
  • An OSC, DCS or APC string holds at most 64 MiB: a longer one is dropped whole, not done in part. In an OSC or APC string the C0 controls are dropped; a DCS string keeps them and drops only DEL. SOS and PM strings, and a DCS string whose parameters have a : or a private marker that is not the first byte, are read to their end and dropped.
  • A count of 0 is 1, as the standards say. A sequence the terminal does not know is ignored.
  • A character of width 0, such as a combining mark, joins the character before it in that character's cell. U+FE0F after a character of one cell that has an emoji form makes that character two cells wide.
  • What a reader sees as one character, a grapheme cluster (Unicode's UAX #29), stays in one cell, or two when it is wide: the two letters of a flag, an emoji with a skin tone, emoji joined by a zero width joiner (U+200D), a letter with a spacing mark, the parts of a Hangul syllable. A flag, and a letter of one cell with a spacing mark, are two cells wide; an emoji with a skin tone is as wide as the emoji. The conjuncts of Indic scripts (UAX #29's rule GB9c) are not joined. A program that counts each character's width for itself, as wcwidth() does, resets mode 2027: then each character with a width has cells of its own, and only a character after a zero width joiner joins the cell of the one before.
  • An erase gives cells the pen's background ("background color erase"), as xterm-256color promises.

In the tables, n is a number the program gives, CSI is ESC [, OSC is ESC ], DCS is ESC P, APC is ESC _ and ST is ESC \.

2. C0 controls

Control What it does
ENQ (0x05) Sends the answerback message, when the host set one.
BEL (0x07) The host's bell.
BS (0x08) One column left, not past the left margin. With reverse wrap (mode 45), at the first column it goes to the right margin of the line above when that line continued on this one.
HT (0x09) To the next tab stop, or the right margin, or the last column. Tab stops start at every eighth column.
LF, VT, FF (0x0A to 0x0C) One line down; at the bottom of the scroll region the region scrolls. With newline mode (LNM) also to the first column.
CR (0x0D) To the left margin (to the first column when the cursor is left of the margin).
SO, SI (0x0E, 0x0F) The characters that follow use G1, or G0.
DEL (0x7F) Nothing, but in a set of 96 characters, where it is the last character.

3. Escape sequences

Sequence Name What it does
ESC 7, ESC 8 DECSC, DECRC Saves and restores the cursor: its place, the colors and attributes, the character sets, origin mode and the last column flag. Each screen has its own.
ESC D, ESC E, ESC M IND, NEL, RI One line down; one line down to the left margin; one line up (at the top of the scroll region the region scrolls down).
ESC H HTS Sets a tab stop at the cursor's column.
ESC N, ESC O SS2, SS3 The next character uses G2, or G3.
ESC n, ESC o LS2, LS3 The characters that follow use G2, or G3.
ESC V, ESC W SPA, EPA The characters that follow are protected, also from ED, EL and ECH; the protection ends.
ESC Z DECID Answers as the primary device attributes.
ESC c RIS The full reset: the modes, the colors, the palette, the character sets, the tab stops, the keys a program defined and the pictures it kept are as after hosho_terminal_create(), and the two screens are empty with the cursor at home. The scrollback, the title and the title stack stay.
ESC =, ESC > DECKPAM, DECKPNM Application keypad on, off.
ESC 6, ESC 9 DECBI, DECFI One column back or forward; at the margin of the scroll region the region's columns move.
ESC # 3, ESC # 4 DECDHL The line is the top or bottom half of double-width, double-height characters.
ESC # 5, ESC # 6 DECSWL, DECDWL The line is single width, or double width.
ESC # 8 DECALN Fills the screen with E; the scroll region and the margins become the whole screen, and the cursor goes home.
ESC SP F, ESC SP G S7C1T, S8C1T The terminal's answers and the keys start with ESC and a second byte, or with one C1 control (U+0080 to U+009F), which is sent in UTF-8 as two bytes (C2 9B for CSI), as xterm does in UTF-8 mode. S8C1T does nothing after VT52 mode (section 8).
ESC ( ) * + set SCS Designates a set of 94 characters to G0, G1, G2 or G3 (section 9).
ESC - . / A SCS Designates the right half of ISO Latin-1 (96 characters) to G1, G2 or G3. Another set of 96 shows as ASCII.

4. Control sequences

Sequence Name What it does
CSI n @ ICH Inserts n blank cells at the cursor, inside the margins.
CSI n A, B (or e), C (or a), D CUU, CUD (VPR), CUF (HPR), CUB Moves the cursor; it stops at the scroll region or the margins when it started inside them.
CSI n E, CSI n F CNL, CPL Down or up n lines, to the left margin.
CSI n G (or `), CSI n d CHA (HPA), VPA To column n, or row n.
CSI row ; column H (or f) CUP, HVP To that place; with origin mode, counted from the scroll region and the margins.
CSI n I, CSI n Z CHT, CBT n tab stops forward, or back.
CSI n J ED Erases from the cursor to the end (0), from the start to the cursor (1), the screen (2), or the scrollback (3).
CSI n K EL Erases in the line: to the end (0), from the start (1), all (2).
CSI n L, CSI n M IL, DL Inserts or deletes n lines at the cursor, in the scroll region, and the cursor goes to the left margin; nothing when the cursor is outside the region or the margins. Deleted lines do not go to the scrollback.
CSI n P, CSI n X DCH, ECH Deletes n cells (the rest moves left), or erases n cells.
CSI n S, CSI n T SU, SD Scrolls the region up or down. Lines go to the scrollback only when they leave the top of the screen: the scroll region starts at the first line, there are no left and right margins, and the primary screen is in use. This is true of a line feed at the bottom of the region too.
CSI n b REP Shows the last character n more times, when it came directly before; at most as many times as the screen has cells.
CSI c DA Answers CSI ? 64 ; 4 ; 6 ; 9 ; 15 ; 21 ; 22 ; 28 c: a VT420 with sixel, selective erase, national sets, the technical set, horizontal scrolling, color and rectangle functions.
CSI > c DA2 Answers CSI > 41 ; 10 ; 0 c.
CSI > q XTVERSION Answers `DCS >
CSI n g TBC Clears the tab stop at the cursor (0), or all (3).
CSI n h, CSI n l SM, RM Sets or resets a standard mode (section 5).
CSI ? n h, CSI ? n l DECSET, DECRST Sets or resets a DEC private mode (section 5).
CSI ... m SGR Colors and attributes (section 6).
CSI 5 n, CSI 6 n DSR, CPR Answers CSI 0 n (no fault), or the cursor's place CSI row ; column R.
CSI ? n n DSR 6: the cursor with its page, CSI ? row ; column ; 1 R. 15: the printer (CSI ? 10 n ready when the host has one, CSI ? 13 n none). 25: user-defined keys (20 unlocked, 21 locked). 26: the keyboard (CSI ? 27 ; 1 ; 0 ; 0 n). 55, 56: the locator (ready and a mouse once the host told a pointer position). 62: no room for macros. 63: a checksum of 0. 75: data integrity good. 85: one session.
CSI n q DECLL Keyboard lights: 0 all off, 1 to 3 Num Lock, Caps Lock, Scroll Lock on, 21 to 23 off. The host's leds.
CSI top ; bottom r DECSTBM The scroll region, two lines or more; the cursor goes home.
CSI left ; right s DECSLRM The left and right margins, with mode 69 set; the cursor goes home. Without mode 69, CSI s saves the cursor.
CSI u SCORC Restores the cursor.
CSI ... t XTWINOPS The window functions and reports (The Host Functions). These work always: 14, 16, 18 and 19 answer the text area in pixels (CSI 4 ; height ; width t), the cell in pixels (CSI 6 ; height ; width t) and the screen in characters (CSI 8 ; rows ; columns t and CSI 9 ; rows ; columns t); 22 and 23 save and take back the title, on a stack of 10 (with a second number of 1, the icon's name alone, they do nothing). Only when the host permits window functions, as they change the window: 8 asks the host's resize for a size in characters; a first number of 24 or more is DECSLPP, the lines of the page; 1 to 7, 9 and 10 go to the host's window; 11, 13 and 15 ask the host's window_report and answer CSI 1 t or CSI 2 t, CSI 3 ; x ; y t and CSI 5 ; height ; width t; 20 and 21 answer the icon's name as OSC L name ST (the title, when no program named the icon) and the title as OSC l title ST, with their control characters left out; and a 0 for the size of 8 means what fits the display.
CSI n i, CSI ? n i MC Printing: 0 prints the screen (the scroll region; all of it with mode 19; a form feed after it with mode 18), 5 and 4 start and end printer controller mode (the bytes go to the printer, not the screen). ?1 prints the cursor's line, ?5 and ?4 start and end auto print (each line as the cursor leaves it), ?10 and ?11 print the whole screen. What is printed goes to the host's print, a line feed after each line; a host without it prints nothing.
CSI ? n J, CSI ? n K DECSED, DECSEL As ED and EL, but protected cells stay.
CSI n " q DECSCA 1 protects the characters that follow from a selective erase; 0 or 2 does not.
CSI level ; n " p DECSCL With a level of 62 or more and a second number, the second number 1 gives 7-bit controls, and any other number 8-bit controls. The terminal has one level: the level itself changes nothing, and DECRQSS answers 64. DECSCL does nothing after VT52 mode.
CSI ! p DECSTR The soft reset: insert, origin, the cursor keys' and the keypad's application modes, left and right margin mode (?69) and national mode (?42) off; autowrap on; the cursor shown; the pen, the protection, the scroll region, the margins, DECSACE, the character sets, the single shifts and the saved cursors as at the start. The screen stays.
CSI n SP q DECSCUSR The cursor: 0 the host's default shape and blink, 1 and 2 a block, 3 and 4 an underline, 5 and 6 a bar; odd numbers blink. A number above 6 is ignored.
CSI n ' }, CSI n ' ~ DECIC, DECDC Inserts or deletes n columns at the cursor, in the scroll region.
CSI n SP @, CSI n SP A SL, SR Moves the text of the scroll region left or right by n columns.
CSI t ; l ; b ; r ; ... $ v and others DECCRA, DECFRA, DECERA, DECSERA Copies a rectangle to the place t ; l ; b ; r ; page ; row ; column ; page $ v names (the page numbers are not used), fills one with a character (CSI character ; t ; l ; b ; r $ x, the character 32 to 126 or 160 to 255, in the pen's colors and attributes), erases one ($ z), or erases its cells that are not protected ($ {). A top or left edge left out is the first line or column, a bottom or right edge left out the last. With origin mode the edges count from the scroll region and the margins.
CSI t ; l ; b ; r ; attributes $ r, $ t DECCARA, DECRARA Sets or reverses bold (1), underline (4), blink (5) and inverse (7) in a rectangle, or in the stream of cells between its corners (DECSACE, CSI n * x: 2 a rectangle, 0 or 1 a stream, the default). 0 or no attribute resets all four, or reverses all four; DECCARA also takes 22, 24, 25 and 27 to reset one.
CSI id ; page ; t ; l ; b ; r * y DECRQCRA Answers a checksum of a rectangle, DCS id ! ~ xxxx ST, by xterm's rules; CSI n # y (XTCHECKSUM) changes them.
CSI ? n $ p, CSI n $ p DECRQM Answers the state of a mode, CSI ? n ; state $ y or CSI n ; state $ y: 1 set, 2 reset, 3 always set, 4 always reset, 0 not known.
CSI 1 $ w, CSI 2 $ w DECRQPSR Answers the cursor information (DECCIR, DCS 1 $ u ... ST: the place, the attributes, the protection, origin mode, the single shift, the last column flag and the character sets) or the tab stops (DECTABSR, DCS 2 $ u the columns with / between them, ST), which DECRSPS takes back.
CSI n $ u DECRQTSR Answers an empty report for 1 and 2.
CSI & u, CSI " v, CSI # S DECRQUPSS, DECRQDE, XTTITLEPOS Answer that the user-preferred set is DEC Supplemental (DCS 0 ! u % 5 ST), that the whole page is shown (CSI rows ; columns ; 1 ; 1 ; 1 " w), and how many titles are saved of 10 (CSI count ; 10 # S).
`CSI n $ , CSI n * `
CSI n $ ~, CSI n $ } DECSSDT, DECSASD The status line: 0 none, 1 the terminal's indicator line, 2 a line the program writes; DECSSDT also makes the line empty. Then DECSASD 1 sends the text that follows to the line the program writes, and 0 to the screen. On the status line the cursor moves only along it: CUP, CHA, CUF, CUB, BS, CR and HT move it, SGR, ED, EL and ECH work on the line, a character wider than one cell is not shown, and the line does not wrap. Control sequences with a private marker or the intermediate byte $, DA and DSR are done as always; the other control sequences, the other C0 controls (but BEL and ENQ) and the escape sequences (but RIS) are ignored there.
CSI n ; u ' z, ' {, `' , ' w` DECELR, DECSLE, DECRQLP, DECEFR
CSI > 4 ; n m, CSI > 4 n, CSI ? 4 m, CSI > 4 ; n f XTMODKEYS, XTQMODKEYS, XTFMTKEYS modifyOtherKeys: set, reset, ask, and its form (Keyboard, Mouse and Paste).
CSI = n ; m u, CSI ? u, CSI > n u, CSI < n u Kitty keyboard Sets the protocol's flags (m 1 sets them, 2 adds them, 3 removes them), asks for them (CSI ? flags u), saves them and sets new ones, and takes saved ones back (n times). The stack keeps 8; when it is full the oldest goes. The primary and the alternate screen each have their own flags and stack.
CSI > n t, CSI > n T XTSMTITLE, XTRMTITLE Set or reset title modes 0 to 3; with no number, the two reset all four. Mode 0 makes OSC 0 and 2 read the title in hexadecimal (two digits for each byte; the title ends at a byte less than space), and mode 1 makes the title report hexadecimal. Modes 2 and 3 are kept and reported, but change nothing.
CSI ? n S XTSMGRAPHICS Answers 256 color registers (CSI ? 1 ; 0 ; 256 S) and 4096 by 4096 pixels (CSI ? 2 ; 0 ; 4096 ; 4096 S), whatever is asked of them; other items answer CSI ? n ; 1 S, an error.

5. Modes

Mode Name When set
4 IRM A character pushes the rest of the line right.
8 BDSM The display shows right-to-left text in its order. Set after a reset.
12 SRM Reset: the host shows what the user types (local echo).
20 LNM Line feed also goes to the first column, and Enter sends CR LF.
?1 DECCKM The cursor keys send SS3.
?2 DECANM Reset: the terminal is a VT52 (section 8).
?3 DECCOLM 132 columns, 80 when reset; only with ?40 set. The width changes at once and the host's resize follows; the scroll region and the margins become the whole screen. The screen is erased and the cursor goes home unless ?95 is set.
?5 DECSCNM Reverse video: the default colors are exchanged.
?6 DECOM Origin mode: places count from the scroll region and the margins, and the cursor stays in them. Setting or resetting it puts the cursor home.
?7 DECAWM Autowrap: a full line continues on the next. Set after a reset.
?9, ?1000, ?1002, ?1003 Mouse Mouse reports (Keyboard, Mouse and Paste).
?1005, ?1006, ?1015, ?1016 Mouse forms UTF-8, SGR, urxvt, SGR in pixels.
?12 The cursor blinks. Reset gives the host's default blink, as programs reset it for "the normal cursor".
?18, ?19 DECPFF, DECPEX A form feed after a printed screen; a printed screen is all of it, not the scroll region.
?25 DECTCEM The cursor is shown.
?40 A program can change between 80 and 132 columns.
?42 DECNRCM The national replacement sets are used.
?45 Reverse wrap.
?47, ?1047, ?1049 The alternate screen; 1047 erases it when the program leaves it; 1049 also saves and restores the cursor and erases the alternate screen when it is entered.
?1048 Saves (set) or restores (reset) the cursor.
?66 DECNKM Application keypad.
?67 DECBKM Backspace sends BS, not DEL.
?69 DECLRMM The left and right margins can be set.
?80 DECSDM Sixel display mode (Pictures).
?95 DECNCSM A change of 80 and 132 columns does not erase the screen.
?1004 Focus reports.
?1007 Alternate scroll: the wheel sends cursor keys on the alternate screen.
?1034, ?1036 Alt sets the eighth bit; Alt sends ESC first (set after a reset, and it comes first when both are set).
?1051, ?1052, ?1053 The Sun, HP and SCO function keys.
?1061 A VT220's keyboard.
?2004 Bracketed paste.
?2026 Synchronized output: the host does not draw until it is reset.
?2027 Grapheme clusters: each one is one cell, or two (set after a reset). Reset, each character with a width has cells of its own (section 1).
?2501 A line's direction is that of its first character with a direction.

DECRQM answers 3 (always set) for ?8 (auto-repeat, which the host's keyboard does), and 4 (always reset) for the modes a VT has that make no sense here: ?4, ?10, ?11, ?13, ?14, ?16, ?38, ?53, ?59, ?60, ?61, ?64, ?68, ?73, and the ECMA-48 editing modes 1, 2, 3, 5, 7, 10, 11 and 13 to 19. hosho_terminal_mode() reads each mode by its HoshoMode; HOSHO_MODE_VT52 is set when DECANM is reset, and HOSHO_MODE_LOCAL_ECHO when SRM is reset.

6. Colors and attributes (SGR)

Parameter What it does
0 All off (a hyperlink and the protection stay).
1, 2, 3 Bold, faint, italic; 22 ends bold and faint, 23 italic.
4, 4:0 to 4:5, 21, 24 Underline; its style: none, single, double, curly, dotted, dashed; 21 double; 24 none.
5, 6, 25 Blink on, off.
7, 27; 8, 28; 9, 29 Inverse, invisible, strike: on, off.
53, 55 Overline on, off.
30 to 37, 90 to 97, 39 Text in a palette color 0 to 7 or 8 to 15; 39 the default.
40 to 47, 100 to 107, 49 Background in a palette color; 49 the default.
38, 48, 58 Text, background, underline color: ;5;n or :5:n a palette color, ;2;r;g;b, :2::r:g:b or :2:r:g:b direct color. A palette color for the underline is kept as the color it is at that moment: a later change of the palette does not change it. 59 ends the underline color.

DECRQSS gives the pen back as an SGR sequence, without the underline color. The 256 palette colors are xterm's: the 16 the theme gives, a 6 by 6 by 6 cube and 24 greys.

7. OSC, DCS and APC strings

String What it does
OSC 0, 1, 2 ; text The icon's name and the title; the icon's name; the title. When a program set the title mode for hexadecimal (CSI > 0 t), the text of all three is read as hexadecimal.
OSC 4 ; n ; color Sets palette color n; ? for the color asks for it. More pairs can follow.
OSC 7 ; file://host/path The shell's directory: the path, with each %XX read as its byte. The host's name is not used.
OSC 8 ; params ; address The text that follows is a hyperlink; an empty address ends it.
OSC 9 ; text, OSC 9 ; 4 ; state ; percent A notification; a job's progress (the host's progress, with a state of 0 to 4 and a percent of 0 to 100). ConEmu's other sub-commands (OSC 9 ; n with n from 1 to 12, then ; or the end) are ignored, not shown as a notification.
OSC 10 to 19 ; color The default text, background and cursor colors, then the pointer's, Tektronix's and the selection's colors; ? asks. More values go on with the next number.
OSC 22 ; name A pointer shape.
OSC 52 ; selection ; data Sets the clipboard or PRIMARY (base64), or asks for it with ?; only what the host permits. A selection with p and no c is PRIMARY; any other is the clipboard.
OSC 99, OSC 777 ; notify ; title ; body Notifications. Of Kitty's OSC 99, the title and the body (p) are read, and d=0 keeps a title for the body that follows; text in base64 (e=1) and the other keys are not used.
OSC 104 ; n, 110, 111, 112, 113 to 119 Puts back palette colors (all with no number), the default text, background and cursor colors, and the colors of OSC 13 to 19.
OSC 133 ; A, B, C, D ; status Shell integration (Shell Integration).
OSC 1337 ; File=... An inline picture (Pictures).
DCS P1 ; P2 ; P3 q A sixel picture.
DCS $ q name ST DECRQSS: answers a setting: the scroll region (r), the margins (s), the pen (m), the cursor (SP q), the protection (" q), the level (" p), DECSACE, the page and screen sizes, the status line, modifyOtherKeys and the title modes. The answer is DCS 1 $ r with the setting as its control function, then ST; others answer DCS 0 $ r ST.
DCS + q names ST XTGETTCAP: answers terminfo capabilities named in hexadecimal: the name, colors, RGB, the cursor style, the clipboard, the underline styles, the alternate screen, the keypad and the keys, with the values of xterm-256color. Each name gets DCS 1 + r name = value ST (the value in hexadecimal too), or DCS 0 + r name ST when it is not known.
DCS 1 $ t, DCS 2 $ t DECRSPS: takes back the cursor information or the tab stops a report gave.
`DCS clear ; lock key/hex;... ST`
APC G ... The Kitty graphics protocol (Pictures).

A color is rgb:R/G/B with 1 to 4 hexadecimal digits each, #RGB with 1 to 4 digits each, or an X11 color name (upper and lower case and spaces do not count). An answer is rgb:rrrr/gggg/bbbb, ended as the request was, by BEL or ST.

8. VT52 mode

After CSI ? 2 l the terminal is a VT52, with ASCII in each set. It does ESC A to ESC D (the cursor), ESC F and ESC G (graphics on and off), ESC H (home), ESC I (reverse line feed), ESC J and ESC K (erase to the end of the screen and of the line), ESC Y row column (each with 32 added), ESC Z (answers ESC / Z), ESC = and ESC > (the keypad) and ESC <, which leaves VT52 mode. Other escape sequences, control sequences (ESC [) and device control strings are ignored in VT52 mode. As on a real VT, the terminal is then a VT100 until a reset: no 8-bit controls, no DECSCL and no DECRQSS.

9. Character sets

Final byte Set
B ASCII
0 DEC Special Graphics (the line drawing characters)
<, % 5 DEC Supplemental (the user-preferred set is the same)
> DEC Technical
A British (used always); in a set of 96, ISO Latin-1
4, C or 5, R or f, Q or 9, K, Y, E or 6, `, Z, H or 7, =, % 6 The national sets: Dutch, Finnish, French, French Canadian, German, Italian, Norwegian/Danish (two forms), Spanish, Swedish, Swiss, Portuguese. They are used only in national mode (?42).

A set that is not known shows as ASCII. LS1R, LS2R and LS3R are not done: with UTF-8 input there are no bytes 0xA0 to 0xFF for them.

10. Known limitations

  • The terminal has one page: the page numbers of the rectangle functions are not used, and the reports say all of the page is shown.
  • There is no Tektronix mode and there are no macros (DECDMAC).
  • The rectangle functions change bold, underline, blink and inverse only, as a VT does; DECRQCRA counts the characters of the screen in use.
  • The Unicode Bidirectional Algorithm is used without the explicit embedding characters and without bracket pairs, and a line is one paragraph.