ANSI

1.28'22

Ref: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797

ANSI Escape Sequences

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 semi colon (;).

For example:

\x1b[1;31m  # Set style to bold, red foreground.

Sequences

  • 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.

General ASCII Codes

NamedecimaloctalhexC-escapeCtrl-KeyDescription
BEL70070x07\a^GTerminal bell
BS80100x08\b^HBackspace
HT90110x09\t^IHorizontal TAB
LF100120x0A\n^JLinefeed (newline)
VT110130x0B\v^KVertical TAB
FF120140x0C\f^LFormfeed (also: New page NP)
CR130150x0D\r^MCarriage return
ESC270330x1B\e*^[Escape character
DEL1271770x7F<none><none>Delete character

Note: 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.

Note: 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.

Cursor Controls

ESC Code SequenceDescription
ESC[Hmoves cursor to home position (0, 0)
ESC[{line};{column}H
ESC[{line};{column}f
moves cursor to line #, column #
ESC[#Amoves cursor up # lines
ESC[#Bmoves cursor down # lines
ESC[#Cmoves cursor right # columns
ESC[#Dmoves cursor left # columns
ESC[#Emoves cursor to beginning of next line, # lines down
ESC[#Fmoves cursor to beginning of previous line, # lines up
ESC[#Gmoves cursor to column #
ESC[6nrequest cursor position (reports as ESC[#;#R)
ESC Mmoves cursor one line up, scrolling if needed
ESC 7save cursor position (DEC)
ESC 8restores the cursor to the last saved position (DEC)
ESC[ssave cursor position (SCO)
ESC[urestores the cursor to the last saved position (SCO)

Note: 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.

Erase Functions

ESC Code SequenceDescription
ESC[Jerase in display (same as ESC[0J)
ESC[0Jerase from cursor until end of screen
ESC[1Jerase from cursor to beginning of screen
ESC[2Jerase entire screen
ESC[3Jerase saved lines
ESC[Kerase in line (same as ESC[0K)
ESC[0Kerase from cursor to end of line
ESC[1Kerase start of line to the cursor
ESC[2Kerase the entire line

Note: 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.

Colors / Graphics Mode

ESC Code SequenceReset SequenceDescription
ESC[1;34;{...}mSet graphics modes for cell, separated by semicolon (;).
ESC[0mreset all modes (styles and colors)
ESC[1mESC[22mset bold mode.
ESC[2mESC[22mset dim/faint mode.
ESC[3mESC[23mset italic mode.
ESC[4mESC[24mset underline mode.
ESC[5mESC[25mset blinking mode
ESC[7mESC[27mset inverse/reverse mode
ESC[8mESC[28mset hidden/invisible mode
ESC[9mESC[29mset strikethrough mode.

Note: Some terminals may not support some of the graphic mode sequences listed above.

Note: 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.

Color codes

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.

8-16 Colors

Color NameForeground Color CodeBackground Color Code
Black3040
Red3141
Green3242
Yellow3343
Blue3444
Magenta3545
Cyan3646
White3747
Default3949
Reset00

Note: 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 NameForeground Color CodeBackground Color Code
Bright Black90100
Bright Red91101
Bright Green92102
Bright Yellow93103
Bright Blue94104
Bright Magenta95105
Bright Cyan96106
Bright White97107

256 Colors

The following escape codes tells the terminal to use the given color ID:

ESC Code SequenceDescription
ESC[38;5;{ID}mSet foreground color.
ESC[48;5;{ID}mSet background color.

Where {ID} should be replaced with the color index from 0 to 255 of the following color table:

256 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.

RGB Colors

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 SequenceDescription
ESC[38;2;{r};{g};{b}mSet foreground color as RGB.
ESC[48;2;{r};{g};{b}mSet background color as RGB.

Note that ;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.

Screen Modes

Set Mode

ESC Code SequenceDescription
ESC[={value}hChanges the screen width or type to the mode specified by value.
ESC[=0h40 x 25 monochrome (text)
ESC[=1h40 x 25 color (text)
ESC[=2h80 x 25 monochrome (text)
ESC[=3h80 x 25 color (text)
ESC[=4h320 x 200 4-color (graphics)
ESC[=5h320 x 200 monochrome (graphics)
ESC[=6h640 x 200 monochrome (graphics)
ESC[=7hEnables line wrapping
ESC[=13h320 x 200 color (graphics)
ESC[=14h640 x 200 color (16-color graphics)
ESC[=15h640 x 350 monochrome (2-color graphics)
ESC[=16h640 x 350 color (16-color graphics)
ESC[=17h640 x 480 monochrome (2-color graphics)
ESC[=18h640 x 480 color (16-color graphics)
ESC[=19h320 x 200 color (256-color graphics)
ESC[={value}lResets the mode by using the same values that Set Mode uses, except for 7, which disables line wrapping. The last character in this escape sequence is a lowercase L.

Common Private Modes

These are some examples of private modes, which are not defined by the specification, but are implemented in most terminals.

ESC Code SequenceDescription
ESC[?25lmake cursor invisible
ESC[?25hmake cursor visible
ESC[?47lrestore screen
ESC[?47hsave screen
ESC[?1049henables the alternative buffer
ESC[?1049ldisables 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.

Keyboard Strings

ESC[{code};{string};{...}p

Redefines a keyboard key to a specified string.

The parameters for this escape sequence are defined as follows:

  • code is one or more of the values listed in the following table. These values represent keyboard keys and key combinations. When using these values in a command, you must type the semicolons shown in this table in addition to the semicolons required by the escape sequence. The codes in parentheses are not available on some keyboards. ANSI.SYS will not interpret the codes in parentheses for those keyboards unless you specify the /X switch in the DEVICE command for ANSI.SYS.

  • string is either the ASCII code for a single character or a string contained in quotation marks. For example, both 65 and "A" can be used to represent an uppercase A.

IMPORTANT: Some of the values in the following table are not valid for all computers. Check your computer's documentation for values that are different.

List of keyboard strings

KeyCodeSHIFT+codeCTRL+codeALT+code
F10;590;840;940;104
F20;600;850;950;105
F30;610;860;960;106
F40;620;870;970;107
F50;630;880;980;108
F60;640;890;990;109
F70;650;900;1000;110
F80;660;910;1010;111
F90;670;920;1020;112
F100;680;930;1030;113
F110;1330;1350;1370;139
F120;1340;1360;1380;140
HOME (num keypad)0;71550;119--
UP ARROW (num keypad)0;7256(0;141)--
PAGE UP (num keypad)0;73570;132--
LEFT ARROW (num keypad)0;75520;115--
RIGHT ARROW (num keypad)0;77540;116--
END (num keypad)0;79490;117--
DOWN ARROW (num keypad)0;8050(0;145)--
PAGE DOWN (num keypad)0;81510;118--
INSERT (num keypad)0;8248(0;146)--
DELETE (num keypad)0;8346(0;147)--
HOME(224;71)(224;71)(224;119)(224;151)
UP ARROW(224;72)(224;72)(224;141)(224;152)
PAGE UP(224;73)(224;73)(224;132)(224;153)
LEFT ARROW(224;75)(224;75)(224;115)(224;155)
RIGHT ARROW(224;77)(224;77)(224;116)(224;157)
END(224;79)(224;79)(224;117)(224;159)
DOWN ARROW(224;80)(224;80)(224;145)(224;154)
PAGE DOWN(224;81)(224;81)(224;118)(224;161)
INSERT(224;82)(224;82)(224;146)(224;162)
DELETE(224;83)(224;83)(224;147)(224;163)
PRINT SCREEN----0;114--
PAUSE/BREAK----0;0--
BACKSPACE88127(0)
ENTER13--10(0
TAB90;15(0;148)(0;165)
NULL0;3------
A976510;30
B986620;48
C996630;46
D1006840;32
E1016950;18
F1027060;33
G1037170;34
H1047280;35
I1057390;23
J10674100;36
K10775110;37
L10876120;38
M10977130;50
N11078140;49
O11179150;24
P11280160;25
Q11381170;16
R11482180;19
S11583190;31
T11684200;20
U11785210;22
V11886220;47
W11987230;17
X12088240;45
Y12189250;21
Z12290260;44
14933--0;120
2506400;121
35135--0;122
45236--0;123
55337--0;124
65494300;125
75538--0;126
85642--0;126
95740--0;127
04841--0;129
-4595310;130
\=6143---0;131
[91123270;26
]93125290;27
92124280;43
;5958--0;39
'3934--0;40
,4460--0;51
.4662--0;52
/4763--0;53
`96126--(0;41)
ENTER (keypad)13--10(0;166)
/ (keypad)4747(0;142)(0;74)
* (keypad)42(0;144)(0;78)--
- (keypad)4545(0;149)(0;164)
+ (keypad)4343(0;150)(0;55)
5 (keypad)(0;76)53(0;143)--

Resources

๐Ÿ“–