@@ -10,57 +10,89 @@ that programs interact with the terminal.
1010
1111Programs running within terminals only have a single way to communicate
1212with the terminal: writing bytes to the connected file descriptor
13- (typically a pty). In order to differentiate between text to be
14- displayed and commands to be executed, terminals use special syntax
15- known collectively as ** control sequences** .
16-
17- Due to the historical nature of terminals, control sequences come
18- in a handful of different formats. Most begin with an escape character
19- (` 0x1B ` ), so control sequences are sometimes referred to as
20- ** escape codes** or ** escape sequences** .
21-
22- There are eight types of control sequences:
23-
24- - Control Characters
25- - Escape Sequences
26- - CSI Sequences ("Control Sequence Introducer")
27- - OSC Sequences ("Operating System Command")
28- - DCS Sequences ("Device Control Sequence")
29- - SOS Sequences ("Start Of String")
30- - PM Sequences ("Privacy Message")
31- - APC Sequences ("Application Program Command")
13+ (typically a pseudo-terminal, or a "pty" for short). In order to
14+ differentiate between text to be displayed and commands to be executed,
15+ terminals use special syntax known collectively as ** control sequences** .
3216
3317Each type of control sequence has a different format and purpose.
3418They are described below along with their specific syntax. The
3519[ reference] ( /docs/vt/reference ) contains a full list of supported
3620control sequences grouped by type.
3721
38- ## Control Characters
22+ ## C0 control characters
3923
40- Control characters are single byte characters that have a special
41- meaning. They have no encoding format; you send the bytes as-is.
42- For example, [ backspace ] ( /docs/vt/control/bs ) is ` 0x08 ` ,
43- [ linefeed ] ( /docs/vt/control/lf ) is ` 0x0A ` , etc .
24+ Each control sequence starts with a special, unprintable character, known
25+ as a ** control character ** . The simplest control characters, known as
26+ ** C0 control characters ** , lie between ` 0x00 ` and ` 0x20 ` in the ASCII
27+ encoding and take no parameters other than themselves .
4428
4529There are very few control characters, but they're important
4630to be aware of because the single byte value impacts the terminal
4731state.
4832
49- ## Escape Sequences
33+ C0 characters meaningful to terminal emulation include:
5034
51- Escape sequences are in the format below. An example escape sequence
52- is ` ESC D ` .
35+ | C0 | Abbr | Full name | Function |
36+ | --------| ---------| -----------------| ---------------------------------------------|
37+ | ` 0x07 ` | [ ` BEL ` ] | Bell | Raise the attention of the user. |
38+ | ` 0x08 ` | [ ` BS ` ] | Backspace | Move the cursor backward one position. |
39+ | ` 0x09 ` | [ ` TAB ` ] | Tab | Move the cursor right to the next tab stop. |
40+ | ` 0x0A ` | [ ` LF ` ] | Linefeed | Move the cursor down one line. |
41+ | ` 0x0D ` | [ ` CR ` ] | Carriage Return | Move the cursor to the leftmost column. |
42+
43+ [ `BEL` ] : /docs/vt/control/bel
44+ [ `BS` ] : /docs/vt/control/bs
45+ [ `TAB` ] : /docs/vt/control/tab
46+ [ `LF` ] : /docs/vt/control/lf
47+ [ `CR` ] : /docs/vt/control/cr
48+
49+ ## Escape sequences
50+
51+ One of the C0 characters, Escape (` 0x1b ` , ` ESC ` ), is special. It can either
52+ start a few discrete sequences or begin entire families of sequences, which is
53+ also why control sequences are also * pars pro toto* commonly known as
54+ ** escape sequences** .
55+
56+ Not all sequences that begin with Escape are Escape sequences as defined here
57+ (they are more likely to be [ C1 sequences] ( #c1-sequences ) ),
58+ but a true Escape sequence like ` ESC D ` are in the format below.
5359
5460```
5561esc_sequence = "0x1B" intermediates final
5662intermediates = [0x20-0x2F]*
5763final = [0x30-0x7E]
5864```
5965
60- ## CSI Sequences
66+ ## ` Fe ` sequences
67+
68+ When Escape is followed by certain printable characters, for various historical
69+ reasons they are instead interpreted together as ** C1 control characters** ,
70+ some of which are responsible for many families of sequences known collectively
71+ as ` Fe ` sequences. These characters can also be represented by single 8-bit
72+ characters, but they remain ** predominantly** encoded with Escape characters as
73+ to avoid conflicting with UTF-8 and other high-byte encodings usually used in
74+ modern terminals.
75+
76+ | ESC code | C1 | Abbr | Full name | Function |
77+ | ----------| --------| ------| -----------------------------| ----------------------------------------------------------------------------------------------------------|
78+ | ` ESC [ ` | ` 0x9b ` | CSI | Control Sequence Introducer | By far the most common. Uses integers to control the cursor, the screen, modes, cells, and their styles. |
79+ | ` ESC ] ` | ` 0x9d ` | OSC | Operating System Command | Often used in modern terminal extensions to transmit string data. |
80+ | ` ESC P ` | ` 0x90 ` | DCS | Device Control Sequence | Most often used when querying the terminal's capabilities (XTGETTCAP). Rare otherwise. |
81+ | ` ESC _ ` | ` 0x9f ` | APC | Application Program Command | Used in very complex protocols like the Kitty Graphics Protocol that require arbitrary payloads. |
82+ | ` ESC X ` | ` 0x98 ` | SOS | Start of String | Obsolete. Ignored by Ghostty. |
83+ | ` ESC ^ ` | ` 0x9e ` | PM | Private Message | Obsolete. Ignored by Ghostty. |
84+
85+ ### CSI
86+
87+ CSI sequences are * by far* the most common type of C1 sequences.
88+ They use integer parameters separated either with colons (` : ` ) or semicolons
89+ (` ; ` ) to move the cursor, set the terminal's mode, scroll up or down, insert
90+ and delete cells and lines, change the styles of cells, etc.
6191
62- CSI sequences are in the format below. An example CSI sequence
63- is ` ESC [ 1 ; 2 m ` . CSI sequences are only capable of encoding integer values.
92+ Their primary limitation is that they can ** only** encode integer values.
93+ Other sequence types such as OSC or DCS are needed to encode string data.
94+
95+ CSI sequences follow the format below. An example CSI sequence is ` ESC [ 1 ; 2 m ` .
6496
6597```
6698csi_sequence = "0x1B" "[" params intermediates final
@@ -71,21 +103,45 @@ intermediates = [0x20-0x2F]*
71103final = [0x40-0x7E]
72104```
73105
74- ## OSC Sequences
106+ ### OSC
107+
108+ OSC sequences are typically used to encode strings or other non-integer data to
109+ transmit between the terminal and an application. They usually alter variables
110+ regarding the terminal emulator itself, such as its title, palette,
111+ clipboard, etc., or instruct it on extra features like hyperlinks, progress
112+ reports, the current working directory, and so on.
113+
114+ As a result of its flexibility, OSC sequences are very often used by modern
115+ extensions to offer new functionality that older terminals can simply ignore.
116+
117+ An OSC sequence begins with OSC (` ESC ] ` or ` 0x9d ` ). Then, by convention, an
118+ integer is added to identify the operation
75119
76- OSC Sequences are typically used to encode strings or other non-integer data to
77- transmit between the terminal and the application. The format of an OSC sequence
78- is below. An example OSC sequence is ` OSC 2 ; 👻 Ghostty 👻 ST ` (where ` ST ` is
79- the String Terminator). By convention, OSC sequences * usually* have some integer
80- identifier to identify the sequence.
120+ The format of an OSC sequence is below. By convention, OSC sequences are
121+ identified by an integer identifier followed by a semicolon (` ; ` ), although
122+ this is never required by the standard. Certain obsolete OSC sequences use
123+ non-numeric identifiers before the semicolon, but Ghostty does not support
124+
125+ > [ !NOTE]
126+ > Due to yet more convoluted historical reasons, OSC sequences in xterm (and
127+ > by extension Ghostty) may be terminated with the Bell character (` BEL ` ,
128+ > ` 0x07 ` ) instead of the standard String Terminator (` ST ` , ` ESC \ ` or ` 0x9c ` ).
129+ >
130+ > While they are in most cases interchangeable, Ghostty will try to echo back
131+ > the terminator used in an OSC sequence when replying, to ensure maximum
132+ > compatibility with older programs and terminals. New code should always
133+ > prefer ` ST ` due to standards compliance.
134+
135+ them. An example OSC sequence is ` OSC 2 ; 👻 Ghostty 👻 ST ` (where ` ST ` is
136+ the String Terminator).
81137
82138```
83- osc_sequence = "0x1B" "]" data ST
139+ osc_sequence = "0x1B" "]" data terminator
84140data = [0x20-0xFF]
85- ST = "0x1B" "0x5C"
141+ terminator = "0x1B" "0x5C"
86142```
87143
88- ## DCS Sequences
144+ ### DCS
89145
90146DCS Sequences are like a CSI sequence combined with an OSC sequence: they
91147natively support integer parameters as well as string values. An example DSC
@@ -101,7 +157,7 @@ data = [0x20-0xFF]
101157ST = "0x1B" "0x5C"
102158```
103159
104- ## APC Sequences
160+ ### APC
105161
106162Ghostty supports APC sequences. Currently there is only one supported APC
107163sequence: the Kitty Graphics Protocol sequence. This is documented [ here] ( https://sw.kovidgoyal.net/kitty/graphics-protocol/ ) .
@@ -112,7 +168,7 @@ data = [0x20-0xFF]
112168ST = "0x1B" "0x5C"
113169```
114170
115- ## SOS and PM Sequences
171+ ### SOS and PM
116172
117173SOS and PM sequences are entirely ignored by Ghostty.
118174
0 commit comments