Standard escape codes are prefixed with Escape:
- Ctrl-Key:
^[
- Octal:
\033
- Unicode:
\u001b
- Hexadecimal:
\x1B
- Decimal: 27
Followed by the command, somtimes delimited by opening square bracket ([
), known as a Control Sequence
Introducer (CSI), optionally followed by arguments and the command itself.
Arguments are delimeted by semicolon (;
).
For example:
\x1b[1;31m # Set style to bold, red foreground.
- ESC - sequence starting with
ESC
(\x1b
) - CSI - Control Sequence Introducer: sequence starting with
ESC [
or CSI (\x9b
) - DCS - Device Control String: sequence starting with
ESC P
or DCS (\x90
) - OSC - Operating System Command: sequence starting with
ESC ]
or OSC (\x9d
)
Any whitespaces between sequences and arguments should be ignored. They are present for improved readability.
Name | decimal | octal | hex | C-escape | Ctrl-Key | Description |
---|---|---|---|---|---|---|
BEL | 7 | 007 | 0x07 |
\a |
^G [1] |
Terminal bell |
BS | 8 | 010 | 0x08 |
\b |
^H |
Backspace |
HT | 9 | 011 | 0x09 |
\t |
^I |
Horizontal tab |
LF | 10 | 012 | 0x0A |
\n |
^J |
Linefeed (also newline) |
VT | 11 | 013 | 0x0B |
\v |
^K |
Vertical tab |
FF | 12 | 014 | 0x0C |
\f |
^L |
Formfeed (also NP new page) |
CR | 13 | 015 | 0x0D |
\r |
^M |
Carriage return |
ESC | 27 | 033 | 0x1B |
\e [2] |
^[ |
Escape character |
DEL | 127 | 177 | 0x7F |
<none> | <none> | Delete character |
[1] | The Ctrl-Key representation is simply associating the non-printable characters from ASCII code 1
with the printable (letter) characters from ASCII code 65 ("A"). ASCII code 1 would be ^A (Ctrl-A), while
ASCII code 7 (BEL) would be ^G (Ctrl-G). This is a common representation (and input method) and historically
comes from one of the VT series of terminals. |
[2] | Some control escape sequences, like e for ESC, are not guaranteed to work in all languages and compilers. It is recommended to use the decimal, octal or hex representation as escape code. |
ESC Code Sequence | Description |
---|---|
ESC[H |
moves cursor to home position (0, 0) |
|
moves cursor to line #, column # |
ESC[#A |
moves cursor up # lines |
ESC[#B |
moves cursor down # lines |
ESC[#C |
moves cursor right # columns |
ESC[#D |
moves cursor left # columns |
ESC[#E |
moves cursor to beginning of next line, # lines down |
ESC[#F |
moves cursor to beginning of previous line, # lines up |
ESC[#G |
moves cursor to column # |
ESC[6n |
request cursor position (reports as ESC[#;#R ) |
ESC M |
moves cursor one line up, scrolling if needed |
ESC 7 |
save cursor position [3] (DEC) |
ESC 8 |
restores the cursor to the last saved position (DEC) |
ESC[s |
save cursor position (SCO) |
ESC[u |
restores the cursor to the last saved position (SCO) |
[3] | Some sequences, like saving and restoring cursors, are private sequences and are not standardized. While some terminal emulators (i.e. xterm and derived) support both SCO and DEC sequences, they are likely to have different functionality. It is therefore recommended to use DEC sequences. |
ESC Code Sequence | Description |
---|---|
ESC[J |
erase in display (same as ESC[0J ) |
ESC[0J |
erase from cursor until end of screen |
ESC[1J |
erase from cursor to beginning of screen |
ESC[2J |
erase entire screen |
ESC[3J |
erase saved lines |
ESC[K |
erase in line [4] (same as ESC[0K ) |
ESC[0K |
erase from cursor to end of line |
ESC[1K |
erase start of line to the cursor |
ESC[2K |
erase the entire line |
[4] | Erasing the line won't move the cursor, meaning that the cursor will stay at the last position it
was at before the line was erased. You can use \r after erasing the line, to return the cursor to the
start of the current line. |
ESC Code Sequence [5] | Reset Sequence | Description |
---|---|---|
ESC[1;34;{...}m |
set graphics modes for cell, separated by semicolon (; ) |
|
ESC[0m |
reset all modes (styles and colors) | |
ESC[1m |
ESC[22m [6] |
set bold mode |
ESC[2m |
ESC[22m |
set dim/faint mode |
ESC[3m |
ESC[23m |
set italic mode |
ESC[4m |
ESC[24m |
set underline mode |
ESC[5m |
ESC[25m |
set blinking mode |
ESC[7m |
ESC[27m |
set inverse/reverse mode |
ESC[8m |
ESC[28m |
set hidden/invisible mode |
ESC[9m |
ESC[29m |
set strikethrough mode |
[5] | Some terminals may not support some of the graphic mode sequences listed above. |
[6] | Both dim and bold modes are reset with the ESC[22m sequence. The ESC[21m sequence is a non-specified
sequence for double underline mode and only work in some terminals and is reset with ESC[24m . |
Most terminals support 8 and 16 colors, as well as 256 (8-bit) colors. These colors are set by the user, but have commonly defined meanings.
Color Name | Foreground Color Code | Background Color Code |
---|---|---|
Black | 30 |
40 |
Red | 31 |
41 |
Green | 32 |
42 |
Yellow | 33 |
43 |
Blue | 34 |
44 |
Magenta | 35 |
45 |
Cyan | 36 |
46 |
White | 37 |
47 |
Default | 39 |
49 |
Reset [7] | 0 |
0 |
[7] | the Reset color is the reset code that resets all colors and text effects, Use Default color to reset colors only. |
Most terminals, apart from the basic set of 8 colors, also support the "bright" or "bold" colors. These have their
own set of codes, mirroring the normal colors, but with an additional ;1
in their codes:
# Set style to bold, red foreground. \x1b[1;31mHello # Set style to dimmed white foreground with red background. \x1b[2;37;41mWorld
Terminals that support the aixterm specification provides bright versions of the ISO colors, without the need to use the bold modifier:
Color Name | Foreground Color Code | Background Color Code |
---|---|---|
Bright Black | 90 |
100 |
Bright Red | 91 |
101 |
Bright Green | 92 |
102 |
Bright Yellow | 93 |
103 |
Bright Blue | 94 |
104 |
Bright Magenta | 95 |
105 |
Bright Cyan | 96 |
106 |
Bright White | 97 |
107 |
The following escape codes tells the terminal to use the given color ID:
ESC Code Sequence | Description |
---|---|
ESC[38;5;{ID}m |
set foreground color |
ESC[48;5;{ID}m |
set background color |
Where {ID}
should be replaced with the color index from 0 to 255 of the following color table:
The table starts with the original 16 colors (0-15).
The proceeding 216 colors (16-231) or formed by a 3bpc RGB value offset by 16, packed into a single value.
The final 24 colors (232-255) are grayscale starting from a shade slighly lighter than black, ranging up to shade slightly darker than white.
Some emulators interpret these steps as linear increments (256 / 24) on all three channels, although some emulators may explicitly define these values.
More modern terminals supports Truecolor (24-bit RGB), which allows you to set foreground and background colors using RGB.
These escape sequences are usually not well documented.
ESC Code Sequence | Description |
---|---|
ESC[38;2;{r};{g};{b}m |
set foreground color as RGB |
ESC[48;2;{r};{g};{b}m |
set background color as RGB |
- Note:
;38
and;48
corresponds to the 16 color sequence and is interpreted by the terminal to set the foreground and background color respectively. Where as;2
and;5
sets the color format.
These are some examples of private modes, which are not defined by the specification, but are implemented in most terminals.
ESC Code Sequence | Description |
---|---|
ESC[?25l |
make cursor invisible |
ESC[?25h |
make cursor visible |
ESC[?47l |
restore screen |
ESC[?47h |
save screen |
ESC[?1049h |
enables the alternative buffer |
ESC[?1049l |
disables the alternative buffer |
Refer to the XTerm Control Sequences for a more in-depth list of private modes defined by XTerm.
- Note:
- While these modes may be supported by the most terminals, some may not work in multiplexers like tmux.
ReST layout: | Alexandr Shavykin |
---|---|
Contact: | [email protected] |
Date: | 25-May-25 18:53:53 PDT |
Origin: | https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 |