ANSI escape code
ANSI escape sequences are a standard for controlling what appears on video text terminals and terminal emulators. These are the screens and programs that show text on computers. The standard lets a computer control the position of the cursor, the color of text, font styling, and other options. It works through in-band signaling, meaning the control instructions are sent along with the text itself.
Special sequences of bytes are placed inside ordinary text. Most of them begin with an ASCII escape character followed by a bracket character. The terminal reads these sequences as commands rather than as words to show on the screen. Because of this, ANSI codes can change the color of text and move the cursor so that text can be drawn anywhere on the screen.
ANSI sequences were introduced in the 1970s to replace sequences that were specific to each computer maker. By the early 1980s they had become widespread in the computer equipment market.
Hardware text terminals have become increasingly rare in the 21st century. However, the ANSI standard is still important. A great majority of terminal emulators and command consoles understand at least part of the ANSI standard.
The name ANSI stands for "American National Standards Institute".
History
Most makers of video terminals added their own special codes for tasks such as placing the cursor at a spot on the screen. The VT52 terminal, for example, used the ESC character, a Y character, and two more characters that stood for the x,y location. The Hazeltine 1500 had a similar feature, but it used different codes. Because each terminal needed different codes, people created tools such as termcap ("terminal capabilities") and tput so that programs could use the same API with any terminal. Many terminals also needed numbers sent as the binary values of characters, which was hard for some programming languages and for systems that did not use ASCII.
The ANSI standard tried to fix these problems with one command set that all terminals would use, and with all numbers sent as ASCII numbers. Its first version, ECMA-48, was adopted in 1976. It continued a series of character coding standards that began with ECMA-6 in 1965, from which ISO 646 comes. The name "ANSI escape sequence" dates from 1979, when ANSI adopted ANSI X3.64. The two standards were later merged into ISO 6429, and in 1994 ANSI withdrew its standard in favor of the international one.
The first popular video terminal to support these sequences was the Digital VT100, introduced in 1978, and its success led to many clones, such as the affordable Zenith Z-19 in 1979, the Qume QVT-108, the Televideo TVI-970, and the Wyse WY-99GT. Over time, software such as bulletin board systems and online services relied on the escape sequences working, so almost all new terminals and emulator programs supported them. In 1981, FIPS publication 86 adopted ANSI X3.64 for use in the US government, but it was later withdrawn when the government stopped duplicating industry standards. ECMA-48 has been updated several times and is currently in its 5th edition from 1991. It is also adopted by IEC as ISO/IEC 6429, and a version is adopted as a Japanese Industrial Standard, JIS X 0211. Related standards include ITU T.61, the Teletex standard, and ISO/IEC 8613, the Open Document Architecture standard, which share many escape codes with the ANSI system.
Platform support
In the early 1980s, a lot of software used these codes to update what appeared on the screen. This included everything on VMS, most software made to run on CP/M home computers, and much Unix-style software, because it was easier to use than other methods. Terminal emulators that connect to other computers almost always support these codes, and xterm on Unix can also work with software on the same machine. Computers later gained built-in displays that supported the codes, and people tried to extend them to printers and to the Open Document Architecture.
The IBM PC, introduced in 1981, did not support these codes. It only understood a few control characters, such as BEL, CR, LF, and BS. Later, DOS 2.0 included an optional device driver called ANSI.SYS, but it was slow and was not installed by default, so software rarely used it. The Windows Console did not support these codes at all, and programs such as TCC and ConEmu were created to add support. In 2016, Microsoft released a Windows 10 update, version 1511, which unexpectedly added support for these codes. Today, Windows Terminal supports them by default.
C0 control codes
Almost all users expect some single-byte characters to have certain functions. These codes were first defined as part of ASCII. Today, the default C0 control code set is defined in ISO 6429 (ECMA-48), which is part of the same standard as the C1 set used by ANSI escape sequences. ISO 2022 allows the C0 set to be used without the C1 set, and the other way around, as long as 0x1B is always ESC. These codes are used to shorten the amount of data sent or to do some jobs that escape sequences cannot do:
Escape sequences vary in length. Their general format is set by ANSI X3.41. They use only bytes from 0x20 to 0x7F, which are all the non-control ASCII characters, so they can be read without looking ahead. What happens if a control character or other invalid byte appears before the sequence ends is undefined.
| ^ | C0 | Abbr | C escape sequence | Name | Effect |
|---|---|---|---|---|---|
| ^G | 0x07 | BEL | \a | Bell | Makes an audible noise. |
| ^H | 0x08 | BS | \b | Backspace | Moves the cursor left (but may "backwards wrap" if cursor is at start of line). |
| ^I | 0x09 | HT | \t | Tab | Moves the cursor right to next tab stop. |
| ^J | 0x0A | LF | \n | Line Feed | Moves to next line, scrolls the display up if at bottom of the screen. Usually does not move horizontally, though programs should not rely on this. |
| ^L | 0x0C | FF | \f | Form Feed | Move a printer to top of next page. Usually does not move horizontally, though programs should not rely on this. Effect on video terminals varies. |
| ^M | 0x0D | CR | \r | Carriage Return | Moves the cursor to column zero. |
| ^[ | 0x1B | ESC | \x1B, \033 | Escape | Starts all the escape sequences |
Fe Escape sequences
When the ESC character is followed by a certain code, the sequence is called an Fe type. What it means is decided by the rules for C1 control codes. All escape sequences for those control codes follow this same format.
In 8-bit systems, these control codes can be written as single bytes. This works with character encodings based on the ISO 8859 series. However, modern devices use encodings such as UTF-8 or CP-1252, where those codes are often used for other things. So a two-byte sequence is usually used instead. In UTF-8, a C1 control code is written using the C1 Controls and Latin-1 Supplement block, which gives a different two-byte code, but this does not save any space.
| Code | C1 | Abbr | Name | Effect |
|---|---|---|---|---|
| ESC N | 0x8E | SS2 | Single Shift Two | Select a single character from one of the alternative character sets. SS2 selects the G2 character set, and SS3 selects the G3 character set. In a 7-bit environment, this is followed by one or more GL bytes (0x20–0x7F) specifying a character from that set.: 9.4 In an 8-bit environment, these may instead be GR bytes (0xA0–0xFF).: 8.4 |
| ESC O | 0x8F | SS3 | Single Shift Three | |
| ESC P | 0x90 | DCS | Device Control String | Terminated by ST.: 5.6 Xterm's uses of this sequence include defining User-Defined Keys, and requesting or setting Termcap/Terminfo data. |
| ESC [ | 0x9B | CSI | Control Sequence Introducer | Starts most of the useful sequences, terminated by a byte in the range 0x40 through 0x7E.: 5.4 |
| ESC \ | 0x9C | ST | String Terminator | Terminates strings in other controls.: 8.3.143 |
| ESC ] | 0x9D | OSC | Operating System Command | Starts a control string for the operating system to use, terminated by ST.: 8.3.89 |
| ESC X | 0x98 | SOS | Start of String | Takes an argument of a string of text, terminated by ST.: 5.6 The uses for these string control sequences are defined by the application: 8.3.2, 8.3.128 or privacy discipline.: 8.3.94 These functions are rarely implemented and the arguments are ignored by xterm. Some Kermit clients allow the server to automatically execute Kermit commands on the client by embedding them in APC sequences; this is a potential security risk if the server is untrusted. |
| ESC ^ | 0x9E | PM | Privacy Message | |
| ESC _ | 0x9F | APC | Application Program Command |
Control Sequence Introducer commands
Control Sequence Introducer, or CSI, commands start with the characters ESC [, which some programming languages write as \e[, \x1b[, or \033[. After these come some optional settings, and then one final character that tells the terminal what to do.
Most of these commands use numbers separated by semicolons, such as 1;2;3. If a number is missing, the terminal treats it as 0. Some commands were set aside as "private," so that companies making terminals could add their own commands without clashing with the standard.
| Code | Abbr | 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. (not ANSI.SYS) |
| CSI n F | CPL | Cursor Previous Line | Moves cursor to beginning of the line n (default 1) lines up. (not ANSI.SYS) |
| CSI n G | CHA | Cursor Horizontal Absolute | Moves the cursor to column n (default 1). (not ANSI.SYS) |
| 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 in Display | Clears part of the screen. If n is 0 (or missing), clear from cursor to end of screen. If n is 1, clear from cursor to beginning of the screen. If n is 2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS). If n is 3, clear entire screen and delete all lines saved in the scrollback buffer (this feature was added for xterm and is supported by other terminal applications). |
| CSI n K | EL | Erase in Line | Erases part of the line. If n is 0 (or missing), clear from cursor to the end of the line. If n is 1, clear from cursor to beginning of the line. If n is 2, 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 Vertical Position | Same as CUP, but counts as a format effector function (like CR or LF) rather than an editor function (like CUD or CNL). This can lead to different handling in certain terminal modes.: Annex A |
| CSI n m | SGR | Select Graphic Rendition | Sets colors and style of the characters following this code |
| CSI 5i | AUX Port On | Enable aux serial port usually for local serial printer | |
| CSI 4i | AUX Port Off | Disable aux serial port usually for local serial printer | |
| CSI 6n | DSR | Device Status Report | Reports the cursor position (CPR) by transmitting ESC[n;mR, where n is the row and m is the column. |
| Code | Abbr | Name | Effect |
|---|---|---|---|
| CSI s | SCP, SCOSC | Save Current Cursor Position | Saves the cursor position/state in SCO console mode. In vertical split screen mode, instead used to set (as CSI n ; n s) or reset left and right margins. |
| CSI u | RCP, SCORC | Restore Saved Cursor Position | Restores the cursor position/state in SCO console mode. |
| CSI ? 25 h | DECTCEM | Shows the cursor, from the VT220. | |
| CSI ? 25 l | DECTCEM | Hides the cursor. | |
| CSI ? 1004 h | Enable reporting focus. Reports whenever terminal emulator enters or exits focus as ESC [I and ESC [O, respectively. | ||
| CSI ? 1004 l | Disable reporting focus. | ||
| CSI ? 1049 h | Enable alternative screen buffer, from xterm | ||
| CSI ? 1049 l | Disable alternative screen buffer, from xterm | ||
| CSI ? 2004 h | Turn on bracketed paste mode. In bracketed paste mode, text pasted into the terminal will be surrounded by ESC [200~ and ESC [201~; programs running in the terminal should not treat characters bracketed by those sequences as commands (Vim, for example, does not treat them as commands). From xterm | ||
| CSI ? 2004 l | Turn off bracketed paste mode. |
Select Graphic Rendition parameters
The control sequence CSI n m, called Select Graphic Rendition (SGR), changes how text looks on screen. Several settings can be combined in one sequence by separating them with semicolons, and each setting stays in effect until a later SGR changes it. If no codes are given, CSI m works the same as CSI 0 m, which resets the text to normal.
Colors were added to terminals in stages. The first standard had only 8 named colors, and later terminals added brighter versions of them. As 256-color lookup tables became common on graphics cards, sequences were added to choose from 256 colors, and the 8-bit color codes that make this possible are shown below. Later, as "true color" graphics became common, programs such as Konsole and GNOME Terminal added support for 24-bit colors, which use red, green, and blue values. Because the color lists are not fixed by the standards, the exact colors can look different from one terminal emulator to another.
See also: Web colors § Web-safe colors
| n | Name | Note |
|---|---|---|
| 0 | Reset or normal | All attributes become turned off |
| 1 | Bold or increased intensity | As with faint, the color change is a PC (SCO / CGA) invention. |
| 2 | Faint, decreased intensity, or dim | May be implemented as a light font weight like bold. |
| 3 | Italic | Not widely supported. Sometimes treated as inverse or blink. |
| 4 | Underline | Style extensions exist for Kitty, VTE, mintty, iTerm2 and Konsole. |
| 5 | Slow blink | Sets blinking to less than 150 times per minute |
| 6 | Rapid blink | MS-DOS ANSI.SYS, 150+ per minute; not widely supported |
| 7 | Reverse video or invert | Swap foreground and background colors. |
| 8 | Conceal or hide | Not widely supported. |
| 9 | Crossed-out, or strike | Characters legible but marked as if for deletion. Not supported in Terminal.app. |
| 10 | Primary (default) font | |
| 11–19 | Alternative font | Select alternative font n − 10 |
| 20 | Fraktur (Gothic) | Rarely supported |
| 21 | Doubly underlined; or: not bold | Double-underline per ECMA-48,: 8.3.117 but instead disables bold intensity on several terminals, including in the Linux kernel's console before version 4.17. |
| 22 | Normal intensity | Neither bold nor faint; color changes where intensity is implemented as such. |
| 23 | Neither italic, nor blackletter | |
| 24 | Not underlined | Neither singly nor doubly underlined |
| 25 | Not blinking | Turn blinking off |
| 26 | Proportional spacing | ITU T.61 and T.416, not known to be used on terminals |
| 27 | Not reversed | |
| 28 | Reveal | Not concealed |
| 29 | Not crossed out | |
| 30–37 | Set foreground color | |
| 38 | Set foreground color | Next arguments are 5;n or 2;r;g;b |
| 39 | Default foreground color | Implementation defined (according to standard) |
| 40–47 | Set background color | |
| 48 | Set background color | Next arguments are 5;n or 2;r;g;b |
| 49 | Default background color | Implementation defined (according to standard) |
| 50 | Disable proportional spacing | T.61 and T.416 |
| 51 | Framed | Implemented as "emoji variation selector" in mintty. |
| 52 | Encircled | |
| 53 | Overlined | Not supported in Terminal.app |
| 54 | Neither framed nor encircled | |
| 55 | Not overlined | |
| 58 | Set underline color | Not in standard; implemented in Kitty, VTE, mintty, and iTerm2. Next arguments are 5;n or 2;r;g;b. |
| 59 | Default underline color | Not in standard; implemented in Kitty, VTE, mintty, and iTerm2. |
| 60 | Ideogram underline or right side line | Rarely supported |
| 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 | |
| 65 | No ideogram attributes | Reset the effects of all of 60–64 |
| 73 | Superscript | Implemented only in mintty |
| 74 | Subscript | |
| 75 | Neither superscript nor subscript | |
| 90–97 | Set bright foreground color | Not in standard; originally implemented by aixterm |
| 100–107 | Set bright background color |
| FG | BG | Name | CGA/EGA/VGA | Windows Console | PowerShell 1.0–6.0 | VS Code | Windows 10 Console | Terminal.app | PuTTY | mIRC | xterm | Ubuntu | Eclipse Terminal |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 30 | 40 | Black | 0, 0, 0 | 12, 12, 12 | 0, 0, 0 | 1, 1, 1 | 0, 0, 0 | ||||||
| 31 | 41 | Red | 196, 0, 0 | 128, 0, 0 | 205, 49, 49 | 197, 15, 31 | 153, 0, 0 | 187, 0, 0 | 127, 0, 0 | 205, 0, 0 | 222, 56, 43 | 205, 0, 0 | |
| 32 | 42 | Green | 0, 196, 0 | 0, 128, 0 | 13, 188, 121 | 19, 161, 14 | 0, 166, 0 | 0, 187, 0 | 0, 147, 0 | 0, 205, 0 | 57, 181, 74 | 0, 205, 0 | |
| 33 | 43 | Yellow | 196, 126, 0 | 128, 128, 0 | 238, 237, 240 | 229, 229, 16 | 193, 156, 0 | 153, 153, 0 | 187, 187, 0 | 252, 127, 0 | 205, 205, 0 | 255, 199, 6 | 205, 205, 0 |
| 34 | 44 | Blue | 0, 0, 196 | 0, 0, 128 | 36, 114, 200 | 0, 55, 218 | 0, 0, 178 | 0, 0, 187 | 0, 0, 127 | 0, 0, 238 | 0, 111, 184 | 0, 0, 238 | |
| 35 | 45 | Magenta | 196, 0, 196 | 128, 0, 128 | 1, 36, 86 | 188, 63, 188 | 136, 23, 152 | 178, 0, 178 | 187, 0, 187 | 156, 0, 156 | 205, 0, 205 | 118, 38, 113 | 205, 0, 205 |
| 36 | 46 | Cyan | 0, 196, 196 | 0, 128, 128 | 17, 168, 205 | 58, 150, 221 | 0, 166, 178 | 0, 187, 187 | 0, 147, 147 | 0, 205, 205 | 44, 181, 233 | 0, 205, 205 | |
| 37 | 47 | White | 196, 196, 196 | 192, 192, 192 | 229, 229, 229 | 204, 204, 204 | 191, 191, 191 | 187, 187, 187 | 210, 210, 210 | 229, 229, 229 | 204, 204, 204 | 229, 229, 229 | |
| 90 | 100 | Bright Black (Gray) | 78, 78, 78 | 128, 128, 128 | 102, 102, 102 | 118, 118, 118 | 102, 102, 102 | 85, 85, 85 | 127, 127, 127 | 127, 127, 127 | 128, 128, 128 | 0, 0, 0 | |
| 91 | 101 | Bright Red | 220, 78, 78 | 255, 0, 0 | 241, 76, 76 | 231, 72, 86 | 230, 0, 0 | 255, 85, 85 | 255, 0, 0 | ||||
| 92 | 102 | Bright Green | 78, 220, 78 | 0, 255, 0 | 35, 209, 139 | 22, 198, 12 | 0, 217, 0 | 85, 255, 85 | 0, 252, 0 | 0, 255, 0 | |||
| 93 | 103 | Bright Yellow | 243, 243, 78 | 255, 255, 0 | 245, 245, 67 | 249, 241, 165 | 230, 230, 0 | 255, 255, 85 | 255, 255, 0 | ||||
| 94 | 104 | Bright Blue | 78, 78, 220 | 0, 0, 255 | 59, 142, 234 | 59, 120, 255 | 0, 0, 255 | 85, 85, 255 | 0, 0, 252 | 92, 92, 255 | 0, 0, 255 | 92, 92, 255 | |
| 95 | 105 | Bright Magenta | 243, 78, 243 | 255, 0, 255 | 214, 112, 214 | 180, 0, 158 | 230, 0, 230 | 255, 85, 255 | 255, 0, 255 | ||||
| 96 | 106 | Bright Cyan | 78, 243, 243 | 0, 255, 255 | 41, 184, 219 | 97, 214, 214 | 0, 230, 230 | 85, 255, 255 | 0, 255, 255 | ||||
| 97 | 107 | Bright White | 255, 255, 255 | 229, 229, 229 | 242, 242, 242 | 230, 230, 230 | 255, 255, 255 | ||||||
Operating System Command sequences
Most Operating System Command sequences were defined by Xterm, but many other terminal programs also support them. For historical reasons, Xterm can end a command with BEL as well as the standard ending. For example, Xterm lets the icon name and window title be set with a code that ends with BEL. A hyperlink code from 2017 is not part of Xterm, but it is used by VTE, iTerm2, and mintty, among others.
The Linux console uses a code to change its color palette. If this code were built into a program, it might freeze other terminals. Adding an extra ending to the code is ignored by Linux, and other terminals read it as a complete code that they can safely skip.
Fs Escape sequences
When the escape character (ESC) is followed by a code from a certain range of values, the sequence is called an Fs type. This type is used for control functions that are registered one at a time with the ISO-IR registry.
A table of these functions is listed under ISO/IEC 2022.
Fp Escape sequences
When the escape character (ESC) is followed by a byte in the range 0x30 to 0x3F, the sequence is of type Fp. This type is set apart for up to sixteen private-use control functions.
| Abbr | Name | Effect | |
|---|---|---|---|
| ESC 7 | DECSC | DEC Save Cursor | Saves the cursor position, encoding shift state and formatting attributes. |
| ESC 8 | DECRC | DEC Restore Cursor | Restores the cursor position, encoding shift state and formatting attributes from the previous DECSC if any, otherwise resets these all to their defaults. |
nF Escape sequences
An escape sequence is of the nF type when the escape character is followed by a code from one range of values. More codes from that range can follow, and then a final code from another range ends the sequence. These sequences are sorted into groups by their first code, and by whether the final code marks private use.
Most nFt sequences change the current character set, and they are listed in ISO/IEC 2022. Some others:
When the first code is '#', the public sequences are reserved for extra registered control functions. No such sequences are registered at present. Type 3Fp sequences, which include those starting with '#', are available for private-use control functions.
| Abbr | Name | Effect | |
|---|---|---|---|
| ESC SP F | ACS6 S7C1T | Announce Code Structure 6 Send 7-bit C1 Control Character to the Host | Makes the function keys send ESC + letter instead of 8-bit C1 codes. |
| ESC SP G | ACS7 S8C1T | Announce Code Structure 7 Send 8-bit C1 Control Character to the Host | Makes the function keys send 8-bit C1 codes. |
| Abbr | Name | Effect | |
|---|---|---|---|
| ESC # 3 | DECDHL | DEC Double-Height Letters, Top Half | Makes the current line use characters twice as tall. This code is for the top half. |
| ESC # 4 | DECDHL | DEC Double-Height Letters, Bottom Half | Makes the current line use characters twice as tall. This code is for the bottom half. |
| ESC # 5 | DECSWL | DEC Single-Width Line | Makes the current line use single-width characters, per the default behaviour. |
| ESC # 6 | DECDWL | DEC Double-Width Line | Makes the current line use double-width characters, discarding any characters in the second half of the line. |
Examples
Some codes control the screen. CSI 2 J clears the screen and, on some devices, moves the cursor to the upper left corner. CSI 32 m makes text green, and adding CSI 1 m makes it brighter. Some codes change what the keyboard sends, such as one that makes the F10 key type "DIR" and ENTER in MS-DOS, which was ANSI bombs sometimes used. The codes CSI s and CSI u save and restore the cursor position.
In shell scripting, these codes are often used in UNIX and UNIX-like terminals for syntax highlighting. For example, the list command can color file and folder names by type. Users can also add codes to standard output or standard error in their scripts. One example uses the sed command to mark lines from make that contain "WARN" in reverse video and lines with "ERR" in bright yellow, ignoring letter case. A Bash function can flash the terminal until a key is pressed. A C program can also display the numbers 0 to 109 in the format of each Select Graphic Rendition code.
Terminal input sequences
When a user presses special keys, such as arrow keys or function keys, the terminal sends a special code to the computer, as if the user had typed it. For some keys, like the cursor keys, the code matches the code the computer uses to move the cursor. Most other keys have no matching code. Terminals use several different code styles and often mix them, so programs must be able to understand all of them. VT terminals also have a normal mode and an application mode, and a program can switch between them.
When a modifier key such as Shift, Alt, or Ctrl is held down, a number is added to the code to show which keys are pressed. For example, 4;2~ means Shift+End, and [20~ means function key F9. Some terminal programs handle these keys differently. Old versions of [Terminator copied a faulty behavior from GNOME Terminal, and Konsole uses its own codes for F1 to F4 when modifiers are used. Some terminals, including iTerm2 and kitty, can also report extra keys using an enhanced mode.
| Key pressed | Number | Comment |
|---|---|---|
| 1 | always added, the rest are optional | |
| Shift | 1 | |
| (Left) Alt | 2 | |
| Control | 4 | |
| Meta | 8 |

Quiz time!
5 questions about ANSI escape code
What can ANSI escape sequences let a computer control on a terminal screen?
Pictures
Keep exploring
This article is a child-friendly adaptation of the Wikipedia article on ANSI escape code, available under CC BY-SA 4.0.
Images from Wikimedia Commons. Tap any image to view credits and license.