In computing, ANSI escape code is the method
of in-band signaling to control formatting, color, and other output options on video text
terminals. To encode this formatting information, it embeds certain sequences of bytes into
the text, which have to be interpreted specially, not as codes of characters. Although hardware
text terminals have become increasingly rare in the 21st century, the relevance of this
standard persists because most terminal emulators interpret at least some of the ANSI escape
sequences in the output text. One notable exception is the win32 console component of
Microsoft Windows. History
Almost all manufacturers of video terminals added vendor-specific escape sequences to
do operations such as placing the cursor at arbitrary positions on the screen. As these
sequences were all different, elaborate libraries such as termcap had to be created so programs
could use the same API for all of them. In addition, most designs required sending numbers
as the binary values of the characters; for some programming languages and for systems
that did not use ASCII internally it was often difficult or impossible to turn a number into
the correct character. The first standard for ANSI escape sequences
was ECMA-48, adopted in 1976. It was a continuation of a series of character coding standards,
the first one being ECMA-6 from 1961, a 7-bit standard from which ASCII originates. ECMA-48
has been updated several times and the current edition is the 5th from 1991. It is also adopted
by ISO and IEC as standard ISO/IEC 6429. The name “ANSI escape sequence” dates from 1981
when ANSI adopted ECMA-48 as the standard ANSI X3.64.
The first popular video terminal to support these sequences was the Digital VT100 introduced
in 1978, which sparked a variety of “clones”, among the earliest and most popular of which
was the much more affordable Zenith Z-19 in 1979. The popularity of these gradually led
to more and more software assuming the escape sequences worked, leading to almost all new
terminals and emulator programs supporting them.
Support Most terminal emulators running on Unix-like
systems interpret ANSI escape sequences. The Linux console also interprets them. Terminal
programs for Microsoft Windows designed to show text from an outside source almost always
interprets them. Many Unix console applications can generate
them. Most of these use curses or their own system to supposedly allow the update of a
terminal using something other than ANSI, but this is so rarely tested nowadays that
they are unlikely to work. Games, shell scripts, and bulletin board systems often use ANSI
directly and cannot work on a terminal that does not interpret them.
Windows and DOS MS-DOS 1.x did not support the ANSI or any
other escape sequences. Only a few control characters were interpreted by the underlying
BIOS, making it almost impossible to do any kind of full-screen application. Any display
effects had to be done with BIOS calls. DOS 2.0 introduced the ability to add a device
driver for the ANSI escape sequences – the de facto standard being ANSI.SYS, but others
like ANSI.COM, NANSI.SYS and ANSIPLUS.EXE are used as well. Slowness and the fact that
it was not installed by default made software rarely take advantage of it; instead, applications
continued to directly manipulate the hardware to get the text display needed. ANSI.SYS and
similar drivers continued to work in Windows 9x up to Windows Me, and in NT-derived systems
for 16-bit legacy programs executing under the NTVDM.
The Win32 console does not support ANSI escape sequences at all. Some alternate command processors
such as JP Software’s TCC and Michael J. Mefford’s ANSI.COM do interpret ANSI escape sequences
printed by programs. Software such as Jason Hood’s ANSICON can act as a wrapper around
the standard Win32 console and add support for ANSI escape sequences.
Software can manipulate the color and cursor position in the command output window with
the ioctl-like Console API interlaced with the text output. Some software internally
interprets ANSI escape sequences in text being printed and translates them to these calls.
AmigaOS All versions of AmigaOS on the Amiga computer
interpret ANSI code sequences for text output to the screen. The AmigaOS printer driver
also interprets ANSI code sequences and translates them into the codes required for the particular
printer that is actually attached. Sequence elements
Escape sequences start with the character ESC. For two character sequences, the second
character is in the range ASCII 64–95. However, most of the sequences are more than
two characters, and start with the characters ESC and [. This sequence is called CSI for
Control Sequence Introducer. The final character of these sequences is in the range ASCII 64–126.
There is a single-character CSI as well. The ESC[ two-character sequence is more often
used than the single-character alternative. Only the two-character sequence is recognized
by devices that support just ASCII or devices that support 8-bit bytes, but use the 0x80–0x9F
control character range for other purposes. On terminals that use UTF-8 encoding, both
forms take 2 bytes but the ESC[ sequence is clearer.
Though some encodings use multiple bytes per character, the following discussion is restricted
to ASCII characters, and thus assumes a single byte for each character.
Non-CSI codes Note: other C0 codes besides ESC — commonly
BEL, BS, CR, LF, FF, TAB, VT, SO, and SI — may produce similar or identical effects to
some control sequences when output. ESC N=SS2
ESC O=SS3 Select a single character from one of the
alternate character sets. ESC ^=PM
ESC _=APC These each take a single string of text, terminated
by ST. They are ignored by xterm. ESC P=DCS
Device control string, ESC ]=OSC
Operating system command — these are similar to CSI, but not limited to integer arguments.
Because they are frequently used, in many cases BEL is an acceptable alternative to
ST. E.g., in xterm, the window title can be set by: “OSC0;this is the window titleBEL”
Note: pressing special keys on the keyboard, as well as outputting many xterm CSI, DCS,
or OSC sequences, often produces a CSI, DCS, or OSC sequence. CSI codes
The general structure of most ANSI escape sequences is CSI [private mode character(s?)]
n1 ; n2… [trailing intermediate character(s?)] letter. The final byte, modified by private
mode characters and trailing intermediate characters, specifies the command. The numbers
are optional parameters. The default value used for omitted parameters varies with the
command, but is usually 1 or 0. If trailing parameters are omitted, the trailing semicolons
may also be omitted. The final byte is technically any character
in the range 64–126, and may be modified with leading intermediate bytes in the range
32 to 47. The colon is the only character not a part
of the general sequence. It was left for future standardization, so any sequence containing
it should be ignored. Although multiple private mode characters
or trailing intermediates are permitted, there are no such known usages.
If there are any leading private mode characters, the main body of the sequence could theoretically
contain any order of characters 0x30–0x3F instead of a well-formed semicolon-separated
list of numbers, but all known terminals are nice and just use them as a flag. Sequences
are also private if the final byte is in the range 112–126.
Examples of private escape codes include the DECTCEM shown below. It was first introduced
for the VT-300 series of video terminals. The existence of a C0 control, DEL, or a high
characters is undefined. Typically, implementations will either cancel the sequence or execute
the control, and then continue parsing the CSI sequence.
Colors Text colors are manipulated using CSI n1 [;n2
[; …]] m sequences, where each n1, n2, … is an SGR parameter as shown above. Thus, for
instance, you use codes 30+i to specify foreground color, 40+i to specify background color, where
i is the number in the desired color’s column header in the table below. The following examples
can be used with the printf utility, where \x1b[ implements the CSI: To switch the foreground
color to black, use \x1b[30m; to switch to red, use \x1b[31m; utilizing the “bold” parameter,
gray would be \x1b[30;1m; to get bold red, use \x1b[31;1m. To reset colors to their defaults,
use \x1b[39;49m. There are two other color standards CSS/HTML
standard colors and X Window colors which standardize both the color names and associated
RGB color values, but the escape sequence standard only specifies the color names, not
RGB values. The chart below shows default RGB assignments for some common terminal programs,
together with the CSS and the X Window System colors for these color names.
The VGA column denotes the typical colors that are used when booting PCs and leaving
them in their classical 80×25 text mode. The colors are different in the EGA/VGA graphic
modes. In July 2004, the blue colors of xterm changed,
RGB → for normal and → for bright. As of 2010, old xterm versions still linger on
many computers though. Xterm and recent-enough versions of KDE’s
Konsole program support ISO-8613-3 24-bit foreground and background color setting Quoting
one of the text-files in its source-tree: ESC[ … 38;2;
foreground color ESC[ … 48;2;
background color In 256 color mode, the color-codes are the
following: 0x00-0x07: standard colors
0x08-0x0F: high intensity colors
0x10-0xE7: 6*6*6=216 colors: 16 + 36*r + 6*g + b
0xE8-0xFF: grayscale from black to white in 24 steps Xterm allows also to set the default foreground
and background colors using ESC]10;
color specifications, and BEL is the ASCII BEL character. The closing bracket instead
of an opening bracket reveals that it belongs to the operating system control commands.
Examples CSI 2 J — This clears the screen and, on
some devices, locates the cursor to the y,x position 1,1.
CSI 32 m — This makes text green. On MS-DOS, normally the green would be dark, dull green,
so you may wish to enable Bold with the sequence CSI 1 m which would make it bright green,
or combined as CSI 32 ; 1 m. MS-DOS ANSI.SYS uses the Bold state to make the character
Bright; also the Blink state can be set to render the Background in the Bright mode.
MS-DOS ANSI.SYS does not support SGR codes 90–97 and 100–107 directly.
CSI 0 ; 6 8 ; “DIR” ; 13 p — This re-assigns the key F10 to send to the keyboard buffer
the string “DIR” and ENTER, which in the DOS command line would display the contents of
the current directory. This was sometimes used for ANSI bombs. This is a private-use
code, using a non-standard extension to include a string-valued parameter. Following the letter
of the standard would consider the sequence to end at the letter D.
CSI s — This saves the cursor position. Using the sequence CSI u will restore it to
the position. Say the current cursor position is 7(y) and 10(x). The sequence CSI s will
save those two numbers. Now you can move to a different cursor position, such as 20(y)
and 3(x), using the sequence CSI 20 ; 3 H or CSI 20 ; 3 f. Now if you use the sequence
CSI u the cursor position will return to 7(y) and 10(x). Some terminals require the DEC
sequences ESC 7 / ESC 8 instead which is more widely supported.
Example of use in shell scripting ANSI escape codes are often used in UNIX and
UNIX-like terminals to provide syntax highlighting. For example, on compatible terminals, the
following list command color-codes file and directory names by type.
ls –color Users can employ escape codes in their scripts
by including them as part of standard output or standard error. For example, the following
sed command embellishes the output of the make command by displaying lines containing
words starting with “ERR” in reverse video and words starting with “WARN” in bold. make 2>&1 | sed -e ‘s\x1b[7m&\x1b[0m/i’ -e
‘s\x1b[1m&\x1b[0m/i’ The representations of the codes are highlighted.
Invalid and ambiguous sequences in use The Linux console uses OSC P n rr gg bb to
change the palette, which, if hard-coded into an application, may hang other terminals.
However, appending ST will be ignored by Linux and form a proper, ignorable sequence for
other terminals. On the Linux console, certain function keys
generate sequences of the form CSI [ char. The CSI sequence should terminate on the [.
Old versions of Terminator generates SS3 1; modifiers char when F1–F4 are pressed with
modifiers. The faulty behavior was copied from GNOME Terminal.
xterm replies CSI row ; column R if asked for cursor position and CSI 1 ; modifiers
R if the F3 key is pressed with modifiers, which collide in the case of row==1. This
can be avoided by using the ? private modifier, which will be reflected in the response.
many terminals prepend ESC to any character that is typed with the alt key down. This
creates ambiguity for uppercase letters and symbols @[\]^_, which would form C1 codes.
Konsole generates SS3 modifiers char when F1–F4 are pressed with modifiers.
See also ANSI art
Control character Advanced Video Attribute Terminal Assembler
and Recreator ISO/IEC JTC 1/SC 2
Notes External links
Standard ECMA-48, Control Functions For Coded Character Sets., European Computer Manufacturers
Association, Geneva 1991 vt100.net DEC Documents
ANSI.SYS — ansi terminal emulation escape sequences at the Wayback Machine
Xterm / Escape Sequences AIXterm / Escape Sequences
A collection of escape sequences for terminals that are vaguely compliant with ECMA-48 and
friends. ANSI Escape Sequences