Jump to content

ANSI escape code

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 74.85.42.110 (talk) at 21:44, 5 October 2010 (Illegal and ambiguous sequences in use: Add konsole). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

ANSI escape sequences are used to control text formatting, color, and other output options on text terminals.

History

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 6 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 the years 1981 to 1997, but in 1981 ANSI adopted ECMA-48 as the standard ANSI X3.64 (and later, in 1997, withdrew it)[1].

Sequence elements

Escape sequences start with the character ESC (ASCII decimal 27/hex 0x1B/octal 033). For two character sequences the second character is in the range ASCII 64 to 95 (@ to _). Most of the sequences are however more than two characters, and start with the characters ESC and [ (left bracket). This sequence is called CSI for Control Sequence Introducer (or Control Sequence Initiator). The final character of these sequences is in the range ASCII 64 to 126 (@ to ~).

There is a single-character CSI (155/0x9B/0233) as well. The ESC+[ two-character sequence is more often used than the single-character alternative, for details see C0 and C1 control codes. Only the two-character sequence is recognized by devices that support just ASCII (7-bit bytes) 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 two-character sequence is more clear.

Though some encodings use multiple bytes per character, the following discussion is restricted to ASCII characters, and so assumes each character is directly represented by a single-byte.

Support

Most terminal emulators running on Linux and other Unix-like systems (such as xterm) interpret ANSI escape sequences, as do such systems' native consoles. Many Unix applications (e.g. ls, grep, vim, and emacs) can generate them. Terminal programs for Microsoft Windows (e.g. Windows command prompt, putty, xterm) also support these sequences.

The most commonly used ANSI escape sequences are supported in application programs such as tput, as well as in low-level programming libraries, such as termcap or terminfo, or a higher-level library such as curses.

Windows and DOS

Windows 95, Windows 98 and DOS systems need a device driver for the ANSI escape sequences – the de facto standard being ANSI.SYS, but others are used as well. Console windows in Windows versions based on NT (Windows NT 4.0, Windows 2000, Windows XP, Windows Server 2003, Windows Vista and Windows Server 2008) do not natively support ANSI Escape sequences, though some support is possible. By adding the following line to the CONFIG.NT file located in the Windows System32 directory, ANSI output from 16-bit legacy programs executing under the NTVDM will be interpreted:

DEVICE=%SystemRoot%\system32\ANSI.SYS

Some support is also offered through alternate command processors such as JP Software's 4NT, Michael J. Mefford's ANSI.COM, and Jason Hood's ansicon.

If ANSI.SYS is loaded in MS-DOS, the escape character can be specified as $e in the prompt command, e.g. prompt $e[44m;... would specify a blue background as part of the prompt. In most modern programming languages it can be specified as "\x1B" (Perl and Ruby also offer the equivalent "\e"), in Java it can be specified as "\u001B", and in BASIC it can be specified as CHR$(27) or (in most Microsoft BASICs) CHR$(&H1B).

32-bit character-mode Windows applications don't write ANSI escape sequences to the console. They must interpret the escape code actions and call the native Console API instead to accomplish the proper result. If you do write the sequences directly, all you'll get is the text of the sequence on the screen, not the action intended. To add ANSI support, one must parse the string for ANSI escape sequences and apply the actions oneself using the Console API. For example, if you pull out \e[1;31m from your text stream on the way to a Console for display, you'd read that to mean change the foreground color to bright red and do so with something like this:

foreground = FOREGROUND_RED|FOREGROUND_INTENSITY;
SetConsoleTextAttribute(ConOut, background|foreground);

The Windows hardware abstraction layer does not permit user-mode applications to set the underlying VGA registers.[clarification needed] The Window's Console API has some restrictions regarding the blink, underline and reverse VGA attributes.

  • The API provides bright background colors rather than blink[2].
  • The following attribute bits for SetConsoleTextAttribute() have no effect even though they are listed[3]:
COMMON_LVB_REVERSE_VIDEO
COMMON_LVB_UNDERSCORE
  • Blink could be emulated in an application with a timer.
  • Reverse-video can be emulated quite easily.
  • The color palette cannot be changed unless running Vista[4].

Note: most devices only support a subset of the SGR (Select Graphic Rendition) codes.

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 (ESC \ ). 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,[citation needed] 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 [leading extension letter(s?)] n1 ; n2... [trailing extension letter(s?)] letter. The final byte 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 to 126 (hex 0x40 to 0x7e, ASCII @ to ~), and may be extended with leading intermediate bytes in the range 32 to 47 (hex 0x20 to 0x2f). Only the case of a single intermediate byte of 32 is currently standardized. [citation needed]

Some of the ANSI escape sequences are private (not covered by the standard). There are two ways these are encoded:

  • if the first byte of the string is in the range 60 to 63 (hex 0x3C to 0x3F, ASCII '<', '=', '>' and '?')
  • if the final byte is in the range 112 to 126 (hex 0x70 to 0x7e, ASCII p to ~).

Examples of private escape codes include the DECTCEM (DEC text cursor enable mode) shown below. It was first introduced for the VT-300 series of video terminals.

Some ANSI escape sequences (not a complete list)
Code Name Effect
CSI n A CUU – CUrsor Up Moves the cursor n (default 1) cells in the given direction. If the cursor is already at the edge of the screen, this has no effect.
CSI n B CUD – CUrsor Down
CSI n C CUF – CUrsor Forward
CSI n D CUB – CUrsor Back
CSI n E CNL – Cursor Next Line Moves cursor to beginning of the line n (default 1) lines down.
CSI n F CPL – Cursor Previous Line Moves cursor to beginning of the line n (default 1) lines up.
CSI n G CHA – Cursor Horizontal Absolute Moves the cursor to column n.
CSI n ; m H CUP – CUrsor Position Moves the cursor to row n, column m. The values are 1-based, and default to 1 (top left corner) if omitted. A sequence such as CSI ;5H is a synonym for CSI 1;5H as well as CSI 17;H is the same as CSI 17H and CSI 17;1H
CSI n J ED – Erase Data Clears part of the screen. If n is zero (or missing), clear from cursor to end of screen. If n is one, clear from cursor to beginning of the screen. If n is two, clear entire screen (and moves cursor to upper left on MS-DOS ANSI.SYS).
CSI n K EL – Erase in Line Erases part of the line. If n is zero (or missing), clear from cursor to the end of the line. If n is one, clear from cursor to beginning of the line. If n is two, clear entire line. Cursor position does not change.
CSI n S SU – Scroll Up Scroll whole page up by n (default 1) lines. New lines are added at the bottom. (not ANSI.SYS)
CSI n T SD – Scroll Down Scroll whole page down by n (default 1) lines. New lines are added at the top. (not ANSI.SYS)
CSI n ; m f HVP – Horizontal and Vertical Position Moves the cursor to row n, column m. Both default to 1 if omitted. Same as CUP
CSI n [;k] m SGR – Select Graphic Rendition Sets SGR parameters. After CSI can be zero or more parameters separated with ;. With no parameters, CSI m is treated as CSI 0 m (reset / normal), which is typical of most of the ANSI escape sequences.
CSI 6 n DSR – Device Status Report Reports the cursor position to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and m is the column. (May not work on MS-DOS.)
CSI s SCP – Save Cursor Position Saves the cursor position.
CSI u RCP – Restore Cursor Position Restores the cursor position.
CSI ?25l DECTCEM Hides the cursor.
CSI ?25h DECTCEM Shows the cursor.
SGR (Select Graphic Rendition) parameters
Code Effect Note
0 Reset / Normal all attributes off
1 Bright (increased intensity) or Bold
2 Faint (decreased intensity) not widely supported
3 Italic: on not widely supported. Sometimes treated as inverse.
4 Underline: Single
5 Blink: Slow less than 150 per minute
6 Blink: Rapid MS-DOS ANSI.SYS; 150 per minute or more
7 Image: Negative inverse or reverse; swap foreground and background
8 Conceal not widely supported
9 Crossed-out Characters legible, but marked for deletion.
10 Primary(default) font
11-19 n-th alternate font Select the n-th alternate font. 14 being the fourth alternate font, up to 19 being the 9th alternate font.
20 Fraktur
21 Underline: Double not widely supported
22 Normal color or intensity neither bright, bold nor faint
23 Not italic, not Fraktur
24 Underline: None not singly or doubly underlined
25 Blink: off
26 Reserved
27 Image: Positive
28 Reveal conceal off
29 Not crossed out
30–37 Set text color 30 + x, where x is from the color table below
38 Reserved
39 Default text color implementation defined (according to standard)
40–47 Set background color 40 + x, where x is from the color table below
48 Reserved
49 Default background color implementation defined (according to standard)
50 Reserved
51 Framed
52 Encircled
53 Overlined
54 Not framed or encircled
55 Not overlined
56-59 Reserved
60 ideogram underline or right side line
61 ideogram double underline or double line on the right side
62 ideogram overline or left side line
63 ideogram double overline or double line on the left side
64 ideogram stress marking
90–99 Set foreground color, high intensity aixterm (not in standard)
100–109 Set background color, high intensity aixterm (not in standard)

Colors

Color table[5]
Intensity 0 1 2 3 4 5 6 7
Normal Black Red Green Yellow[6] Blue Magenta Cyan White
Bright Black Red Green Yellow Blue Magenta Cyan White

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 colors for these color names.

Windows XP
command prompt
PuTTY xterm CSS/HTML X Window
normal black 0, 0, 0 0, 0, 0 0, 0, 0 0, 0, 0 0, 0, 0
red 128, 0, 0 187, 0, 0 205, 0, 0 255, 0, 0 255, 0, 0
green 0, 128, 0 0, 187, 0 0, 205, 0 0, 255, 0 0, 128, 0
yellow 128, 128, 0 187, 187, 0 205, 205, 0 255, 255, 0 255, 255, 0
blue 0, 0, 128 0, 0, 187 0, 0, 238 0, 0, 255 0, 0, 255
magenta 128, 0, 128 187, 0, 187 205, 0, 205 255, 0, 255 255, 0, 255
cyan 0, 128, 128 0, 187, 187 0, 205, 205 0, 255, 255 0, 255, 255
white 192, 192, 192 187, 187, 187 229, 229, 229 255, 255, 255 255, 255, 255
bright/light black 128, 128, 128 85, 85, 85 127, 127, 127
red 255, 0, 0 255, 85, 85 255, 0, 0
green 0, 255, 0 85, 255, 85 0, 255, 0 144, 238, 144 144, 238, 144
yellow 255, 255, 0 255, 255, 85 255, 255, 0 255, 255, 224 225, 255, 224
blue 0, 0, 255 85, 85, 255 92, 92, 255 173, 216, 230 173, 216, 230
magenta 255, 0, 255 255, 85, 255 255, 0, 255
cyan 0, 255, 255 85, 255, 255 0, 255, 255 224, 255, 255 224, 225, 255
white 255, 255, 255 255, 255, 255 255, 255, 255

In July 2004 the blue colors of xterm changed,[7] (0,0,205) --> (0,0,238) for normal and (0,0,255) --> (92,92,255) for bright. As of 2010 old xterm versions still linger on many computers though.

Examples

CSI 2 J — This clears the screen and, on some devices, locates the cursor to the y,x position 1,1 (upper left corner).

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 (via INT 10, AX 1003h, BL 00h) 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. (MS-DOS ANSI.SYS only) This is a private-use code (as indicated by the letter p), 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.

Illegal 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 [.
  • Terminator_(terminal_emulator) generates SS3 1; modifiers char when F1-F4 are pressed with modifiers.
  • 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.
  • many terminals prepend ESC to any character that is typed with the alt key down. This may create ambiguity.
  • Konsole generates SS3 modifiers char when F1-F4 are pressed with modifiers.

See also

Notes

  1. ^ See this NIST list of withdrawn standards
  2. ^ See blink VGA register details.
  3. ^ See this discussion for more detail
  4. ^ See details on SetConsoleScreenBufferInfoEx() and the CONSOLE_SCREEN_BUFFER_INFOEX structure
  5. ^ The names are standard, however the exact shade/hue/value of colors are not standardized and will depend on the device used to display them.
  6. ^ On terminals based on CGA compatible hardware, such as ANSI.SYS running on DOS, this normal intensity foreground color is rendered as Orange. CGA RGBI monitors contained hardware to modify the dark yellow color to an orange/brown color by reducing the green component. See this ansi art as an example.
  7. ^ "Patch #192 - 2004/7/12 - XFree86 4.4.99.9".