ANSI escape code
ANSI escape codes, also known as ANSI escape sequences, are standardized control functions embedded in text streams to direct the operation of video display terminals and compatible character-imaging devices, enabling precise control over text presentation, including cursor movement, screen formatting, color selection, and stylistic attributes such as bold or underline.[1][2]
The codes originated in the 1970s as part of efforts to extend the basic ASCII character set for interactive computing environments. The foundational American standard, ANSI X3.64 (approved in 1979 and published as FIPS PUB 86 in 1979), defined additional encoded controls for two-dimensional input-output devices like terminals and printers, using escape sequences to facilitate data interchange, editing, and formatting.[2] This aligned with the earlier ECMA-48 standard, first edition published by Ecma International in 1976, which specified control functions for 7-bit and 8-bit coded character sets to support consistent device behavior across systems.[3] ECMA-48 was subsequently adopted internationally as ISO/IEC 6429, with the second edition in 1988 and third in 1992, incorporating enhancements like bidirectional text handling while maintaining compatibility with prior versions.[1][4]
In practice, ANSI escape codes typically begin with the Escape character (ESC, ASCII 27 or \x1B), followed by a delimiter like the Control Sequence Introducer (CSI, ESC [ or single-byte 0x9B in 8-bit environments), parameters (numeric or selective values), intermediate characters if needed, and a final byte to invoke the function.[1] They are integral to Unix-like consoles, supporting operations such as clearing the screen (CSI 2 J), moving the cursor (CSI Pn ; Pn H for absolute positioning), and setting graphic renditions like foreground color (CSI 31 m for red text).[5] Modern terminal emulators, including those in Linux distributions, implement a large subset of these sequences alongside extensions for advanced features, ensuring backward compatibility while enhancing user interfaces in command-line tools and applications.[5]
History and Development
Origins in Terminals
In the 1970s, the rise of video display terminals created a demand for control mechanisms beyond the basic ASCII C0 control characters, enabling dynamic cursor positioning, screen manipulation, and text formatting on character-based displays.[6] These terminals, used for interacting with mainframe computers, required efficient ways to manage limited screen real estate without relying solely on hardware switches or vendor-proprietary commands.[7]
Early implementations appeared in terminals like the Lear-Siegler ADM-3A, introduced in 1976 as an affordable ASCII-based video display unit with a 12-inch screen supporting 24 lines of 80 characters.[8] The ADM-3A employed escape sequences for key functions, such as cursor addressing via the escape character (ASCII 27) followed by '=' and row/column codes (e.g., ESC = followed by space for row 1 and space for column 1, where space represents position 1 and subsequent characters increment accordingly up to '/' for row 16 and '_' for column 80), which allowed host computers to direct on-screen operations programmatically.[9] This approach addressed the limitations of simpler teleprinter-style devices by providing flexible, software-driven control over the display.[8]
Heathkit terminals, such as the H19 model released in 1979, further advanced this by incorporating escape sequences for cursor movement, line insertion/deletion, and mode switching in both proprietary Heath and emerging ANSI-compatible modes.[10] The H19's design, aimed at hobbyists and small systems, used sequences like ESC H for cursor home and ESC A for upward movement, building on ASCII foundations to support more interactive applications.[10]
Digital Equipment Corporation's VT52, introduced in 1975, played a key role by popularizing escape sequences for cursor control and editing functions, such as ESC A for cursor up, influencing subsequent terminal designs.[11] The Digital Equipment Corporation's VT100, launched in August 1978, marked a pivotal step by standardizing escape sequences for cursor control, screen clearing, and basic attributes like reverse video, making it compatible with prior DEC models while influencing broader industry adoption.[7] Its sequences, such as ESC [ H for cursor home, facilitated reliable data interchange in multi-vendor environments.[7]
This progression from ASCII's 1963 C0 controls—limited to functions like carriage return and line feed—to escape-initiated sequences enabled richer formatting and control, evolving toward structured commands like those in later CSI formats.[6] The ANSI X3.64-1979 standard formalized these developments by defining additional C1 controls and escape sequences for two-dimensional imaging devices, including cursor positioning (e.g., CUP for cursor to specific row/column) and text attributes (e.g., SGR for selective graphic rendition), to promote interoperability among terminals.[2] Adopted by the U.S. Federal Information Processing Standards as FIPS PUB 86 in 1981, it built directly on terminal innovations to standardize control for cursor movement and formatting.[12]
Standardization Efforts
The formal standardization of escape codes for video terminals built on industry efforts in the late 1970s, aligning with international developments in control sequences for information interchange.
The American National Standards Institute (ANSI) published X3.64-1979, titled "Additional Controls for Use with American National Standard Code for Information Interchange," which defined escape sequences and control functions specifically for video display terminals and peripherals, including cursor positioning and basic graphic renditions.[2] This standard built upon the ASCII framework (ANSI X3.4-1977) by introducing structured escape sequences to enable device-independent text formatting.[2]
The standard was reaffirmed in 1990 (ANSI X3.64-1979/R1990) to align with emerging international efforts, enhancing compatibility for video terminal operations.[13]
Concurrently, Ecma International released the first edition of ECMA-48 in 1976, establishing control functions for coded character sets, including escape sequences that paralleled and influenced ANSI developments.[1] The fourth edition of ECMA-48, published in 1986, was adopted via fast-track procedure as the second edition of ISO/IEC 6429, providing equivalent international mappings for 7-bit and 8-bit coded representations of control functions and ensuring global interoperability.[1] The fifth edition in June 1991 added support for bi-directional text processing and was subsequently adopted as the third edition of ISO/IEC 6429.[1] ANSI X3.64 was ultimately withdrawn in 1997 in favor of these ISO/IEC standards.[14]
Subsequent extensions appeared in ISO/IEC 8613 (also known as ITU-T T.410 series), published in the late 1980s with amendments through the 1990s, which focused on open document architecture for interchange and incorporated escape-based controls for raster and geometric graphics, including indexed and direct color specifications. These standards influenced the Unicode Consortium's mappings of C0 and C1 control codes, integrating escape sequence compatibility into the Unicode Standard (version 1.0, 1991 onward) for multilingual text processing. In the 1990s, updates to related standards and implementations extended support for 256-color palettes and true color modes, building on the foundational CSI structures for enhanced graphic renditions.[15]
Fundamental Components
C0 Control Codes
The C0 control codes form the foundational set of non-printable characters in the 7-bit ASCII standard (ANSI X3.4-1968) and the international ISO/IEC 646 standard, occupying code positions 0 through 31 (hexadecimal 0x00 to 0x1F) and position 127 (0x7F).[16][17] These codes are designed for device control functions in data interchange, such as formatting output on terminals or printers, without representing visible graphics.[16] Unlike graphic characters, C0 codes perform immediate actions upon receipt, enabling essential operations like cursor movement and signaling without multi-byte prefixes.[17]
Key C0 codes include those for alerting, positioning, erasing, and sequence initiation, each with defined binary and hexadecimal representations. The BEL (bell) code, at decimal 7 (binary 0000111, hexadecimal 0x07), generates an audible or visible signal to alert the user or operator.[16] The ESC (escape) code, at decimal 27 (binary 0011011, hexadecimal 0x1B), serves as an introducer for code extension techniques, allowing terminals to interpret subsequent bytes as control commands rather than printable characters.[17] The DEL (delete) code, at decimal 127 (binary 1111111, hexadecimal 0x7F), is a format effector that substitutes an ignorable character to obliterate an erroneous one in data streams or media like paper tape, and is often used for padding in asynchronous transmission.[16]
C0 codes also support basic text positioning without escape sequences, facilitating simple terminal navigation. The LF (line feed) code, at decimal 10 (binary 0001010, hexadecimal 0x0A), advances the active position to the next line, typically moving the cursor down one row while preserving the horizontal position.[17] Similarly, the CR (carriage return) code, at decimal 13 (binary 0001101, hexadecimal 0x0D), returns the active position to the first column of the current line, enabling line beginnings and text wrapping.[16] These single-byte controls provide efficient, prefix-free mechanisms for core terminal behaviors, with ESC acting as a bridge to more complex sequences.[17]
| Code Name | Decimal | Binary | Hexadecimal | Role |
|---|
| BEL | 7 | 0000111 | 0x07 | Audible signal for alerts.[16] |
| LF | 10 | 0001010 | 0x0A | Advance to next line.[17] |
| CR | 13 | 0001101 | 0x0D | Return to line start.[16] |
| ESC | 27 | 0011011 | 0x1B | Initiate control sequences.[17] |
| DEL | 127 | 1111111 | 0x7F | Obliterate erroneous data or pad transmission.[16] |
Escape Sequences Overview
ANSI escape sequences provide a mechanism to extend the functionality of basic C0 control codes, enabling terminals to perform advanced operations such as cursor positioning, screen erasure, and character set selection. These sequences are defined in the ECMA-48 standard, which specifies their coded representations for use in 7-bit and 8-bit environments.[1]
The general syntax of an escape sequence consists of the ESC character (bit combination 01/11, equivalent to ASCII 27 or 0x1B), followed by zero or more intermediate bytes (bit combinations 02/00 through 02/15), and terminated by a final byte (bit combinations 04/00 through 07/14, excluding 02/08 through 02/13 and delete codes). This structure, denoted as ESC I...I F, allows for the introduction of control functions that alter the interpretation of subsequent data, such as shifting character sets or invoking device-specific behaviors.[1] In contrast to single-byte C0 codes like carriage return (CR) or line feed (LF), escape sequences support parameterized instructions, facilitating precise control over terminal attributes.[1]
Escape sequences are categorized into several types based on their structure and final bytes, each serving distinct roles in terminal control. Single-shift sequences (denoted with final byte Fe in the range 04/00–05/15) temporarily invoke an alternative character set for the next character only; for example, ESC N (Fe = 04/14) performs a single shift to the G2 set (SS2). Independent control functions use final bytes Fs in the range 06/00–07/14, such as ESC ( B to designate the G0 set as ASCII (LS0).[1] Privacy escape sequences (Fp) incorporate private-use parameters or intermediates for vendor-specific extensions, while nF sequences allow multiple final bytes for compound functions.[1]
A prominent type is the Control Sequence Introducer (CSI), formed by ESC followed by the intermediate byte [ (05/11), resulting in the 8-bit equivalent 09/11; this is followed by optional parameters, additional intermediates, and a final byte, as in CSI c (final byte 'c' or 06/03) for full reset of the terminal.[1] Common intermediate bytes include [ for CSI to introduce parameterized commands like cursor movement (e.g., CSI n A to move up n lines) or screen clearing (e.g., CSI 2 J to clear the entire screen), thereby expanding C0's limited scope to support interactive text-based interfaces.[1] Other intermediates, such as ] (05/14) for Operating System Command (OSC) strings, enable further extensions but adhere to the same foundational format.[1]
Control Sequence Introducer (CSI) Commands
CSI Structure and Syntax
The Control Sequence Introducer (CSI) serves as the initiator for a broad class of parameterized control functions in the ANSI escape code system, enabling precise terminal operations such as cursor movement and mode setting.[1] In 7-bit environments, CSI is encoded as the escape sequence ESC followed by the left square bracket '[' (bit combination 05/11), while in 8-bit environments, it is a single byte with the value 0x9B (09/11).[1] This representation aligns with the C1 control set defined in ECMA-48, distinguishing CSI from simpler escape sequences.[1]
The syntax of a CSI command follows the format CSI P...P I...I F, where the sequence begins with the CSI introducer, followed by zero or more parameter strings (P), optional intermediate characters (I), and a mandatory final character (F) that specifies the control function.[1] Parameters are decimal integers formed from digit characters (0-9), with multiple parameters separated by semicolons (;); each parameter string may be empty, in which case it assumes a function-specific default value, often 0 or 1.[1][18] Numeric ranges for parameters are not strictly bounded in the standard but are typically positive integers limited by device capabilities, such as screen dimensions for positioning commands; leading zeros in parameters are ignored except to represent zero itself.[1] Intermediate characters, drawn from the range of printable ASCII (02/00 to 02/15), are optional and usually limited to one; they include private-use prefixes like '?' for DEC-specific extensions or '>' for user-defined modes.[1][18] The final character (04/00 to 07/14) terminates the sequence and identifies the action, with values in 07/00 to 07/14 reserved for private use.[1]
A representative example is the Cursor Position (CUP) command, which repositions the cursor to specified row and column coordinates: ESC [Pn1;Pn2 H, where Pn1 defaults to 1 (top row) and Pn2 to 1 (left column) if omitted, and both parameters exceed screen bounds only if the terminal implementation allows wrapping or clipping.[1] Private-use prefixes modify this structure for vendor-specific behaviors; for instance, DEC private modes employ the intermediate '?' followed by parameters and finals like 'h' (set) or 'l' (reset), as in ESC [?1 h to enable the application cursor key mode.[19][1]
Parsers handle CSI sequences by accumulating digits into parameters until a semicolon, intermediate, or final is encountered, with invalid bit combinations (e.g., non-digit in parameters or unallocated finals) treated as implementation-dependent—often ignored or causing the sequence to abort without effect to maintain forward compatibility.[1][18] In DEC private modes, unrecognized parameters may default to no operation, ensuring robustness in mixed environments.[19] This error-tolerant parsing prevents cascading failures from malformed input.[18]
Select Graphic Rendition (SGR) Parameters
The Select Graphic Rendition (SGR) control function enables terminals to apply visual attributes to subsequent characters, such as intensity, font styles, colors, and other effects, without altering the character encoding itself. Defined in the ECMA-48 standard (also known as ISO/IEC 6429), SGR operates as a selective parameter sequence that modifies the rendition cumulatively or by replacement, depending on the terminal's graphic rendition combination mode (GRCM).[1] Attributes take effect at the active cursor position and persist until overridden by another SGR sequence or a reset.[1] If no parameters are specified, the default is normal rendition (equivalent to parameter 0).[1]
The SGR sequence follows the Control Sequence Introducer (CSI) format: ESC [ Ps ; Ps ; ... m, where ESC is the escape character (0x1B), [ introduces the sequence, Ps represents one or more numeric parameters separated by semicolons, and m (0x6D) is the final character.[1] Multiple parameters can be combined in a single sequence to apply several attributes simultaneously, such as ESC [1;31m for bold red foreground text.[1] The effects are implementation-dependent, with support varying by terminal; for instance, some attributes like faint intensity may render as grayscale.[1]
Standard ECMA-48 parameters cover basic text attributes and 8-color palettes, as detailed below. These are widely supported in ANSI-compatible terminals.[1]
| Parameter | Effect |
|---|
| 0 | Normal rendition (resets all attributes) |
| 1 | Bold or increased intensity |
| 2 | Faint or decreased intensity |
| 3 | Italicized text |
| 4 | Single underline |
| 5 | Slow blink (less than 150 blinks per minute) |
| 6 | Rapid blink (150 or more blinks per minute) |
| 7 | Negative image (reverse foreground and background) |
| 8 | Concealed (invisible) text |
| 9 | Crossed-out (strikethrough) |
| 10 | Primary (default) font |
| 11–19 | Alternative fonts (first to ninth alternative) |
| 20 | Fraktur (Gothic style, rarely supported) |
| 21 | Double underline |
| 22 | Normal intensity (neither bold nor faint) |
| 23 | Not italicized (or not Fraktur) |
| 24 | Not underlined |
| 25 | Steady (no blink) |
| 27 | Positive image (no reverse) |
| 28 | Visible (no conceal) |
| 29 | Not crossed-out |
| 30 | Foreground: black |
| 31 | Foreground: red |
| 32 | Foreground: green |
| 33 | Foreground: yellow |
| 34 | Foreground: blue |
| 35 | Foreground: magenta |
| 36 | Foreground: cyan |
| 37 | Foreground: white |
| 39 | Default foreground color |
| 40 | Background: black |
| 41 | Background: red |
| 42 | Background: green |
| 43 | Background: yellow |
| 44 | Background: blue |
| 45 | Background: magenta |
| 46 | Background: cyan |
| 47 | Background: white |
| 49 | Default background color |
Later extensions, originating from terminal emulators like xterm, expand SGR for advanced color support beyond the ECMA-48 8-color limit. These are not part of the core standard but are commonly implemented in modern terminals for 256 indexed colors and 24-bit true color (RGB).[20]
For 256-color mode, the sequence uses ESC [38;5;<n>m for foreground or ESC [48;5;<n>m for background, where <n> is an index from 0 (black) to 255 (bright white); indices 0–15 match the basic colors, 16–231 form a 6x6x6 RGB cube, and 232–255 provide grayscale shades.[20] This extension, introduced in xterm version 195 (2004), draws from ISO-8613 but uses semicolons for backward compatibility.[20]
True color support employs ESC [38;2;<r>;<g>;<b>m for foreground or ESC [48;2;<r>;<g>;<b>m for background, with <r>, <g>, and <b> as 8-bit (0–255) values for red, green, and blue components, respectively.[20] Added in xterm version 282 (2012), this allows arbitrary colors but requires direct-color-capable hardware and software; unsupported terminals may approximate with the nearest palette match or ignore the sequence.[20] Compatibility is high in Unix-like systems and modern Windows terminals but varies in legacy environments.[20]
Color Handling
Basic Color Codes
The basic color codes in ANSI escape sequences are part of the Select Graphic Rendition (SGR) parameters, which allow terminals to set foreground and background colors using a limited palette derived from early 3-bit color models. These codes, standardized in the ECMA-48 specification, map to eight primary colors: black, red, green, yellow, blue, magenta, cyan, and white.[1] Foreground colors are specified with parameters 30–37, while background colors use 40–47, providing a total of 16 possible combinations when both are set.[1]
The color mappings follow a consistent indexing scheme where the parameter value corresponds to the color index (0 for black through 7 for white). For example:
These mappings are defined in the second edition of ECMA-48 (1979), which allocated SGR parameters 30–37 and 40–47 for 3-bit foreground and background colors, respectively.[1] Although standardized in 1979, practical hardware support began in the early 1980s with terminals such as the TeleVideo 950.
The syntax for applying these codes is \e[Ps m, where \e represents the escape character (ASCII 27 or octal 033), [ introduces the control sequence, Ps is the numeric parameter (e.g., 31 for red foreground), and m selects the graphic rendition. For foreground, Ps takes the form 3n where n ranges from 0 to 7 (e.g., \e[31m for red text); for background, it is 4n (e.g., \e[41m for red background). Multiple parameters can be chained with semicolons, such as \e[31;42m for red text on a green background, and the default rendition is reset with \e[0m.[1][15]
To extend the palette to a 4-bit model supporting 16 colors, many terminals implement bright variants of the basic colors using parameters 90–97 for bright foreground and 100–107 for bright background. These provide intensified versions of the same hues, often rendering as bolder or lighter shades (e.g., 90 for bright black, typically gray; 91 for bright red). This extension originated in implementations like AIXterm and xterm, becoming a de facto standard despite not being part of the core ECMA-48 specification.[15] The same indexing applies: 9n for bright foreground and 10n for bright background, with n from 0 to 7 (e.g., \e[91m for bright red text).[15][21]
Historically, these basic color codes were designed for early ANSI-compatible terminals in the late 1970s and 1980s, such as the DEC VT340 series, which supported only the 8-color 3-bit set per type due to hardware limitations in phosphor displays and memory. The addition of bright variants in the 4-bit extension addressed demands for higher contrast in text-based interfaces, enabling a total of 16 distinct colors while maintaining backward compatibility with the original standard.[1][15]
Extended and True Color Support
Extended color support in ANSI escape sequences builds upon the basic 16-color palette by introducing higher-fidelity options for terminals capable of rendering more nuanced hues. The 256-color mode, often referred to as 8-bit color, utilizes the Select Graphic Rendition (SGR) parameters 38;5;<n> for foreground colors and 48;5;<n> for background colors, where <n> is an integer from 0 to 255 indexing into a predefined palette. This palette is divided into three segments: indices 0–15 correspond to the standard ANSI colors (dark variants 0–7 and bright variants 8–15); indices 16–231 form a 6×6×6 RGB color cube, providing 216 colors by evenly spacing red, green, and blue components at levels 0/95/135/175/215/255; and indices 232–255 comprise a 24-step grayscale ramp from black (RGB 0,0,0) to white (RGB 255,255,255). This structure allows for a broader range of colors while remaining compatible with indexed rendering in terminals like xterm.[22]
Further extending capabilities, 24-bit true color support enables direct specification of arbitrary RGB values without relying on a fixed palette. This is achieved through SGR parameters 38;2;<r>;<g>;<b> for foreground and 48;2;<r>;<g>;<b> for background, where <r>, <g>, and <b> are integers from 0 to 255 representing the red, green, and blue components, respectively; the sequence is terminated with m (e.g., \e[38;2;255;0;0m for pure red text). True color, supporting over 16 million combinations, originated as an extension in terminals like xterm (when compiled with direct-color visuals) and is defined in ISO-8613-6 for direct color modes, allowing precise color reproduction in applications such as image viewers or data visualizations. If true color is unavailable, terminals typically approximate by mapping to the nearest palette entry.[22][23]
To detect support for these extended modes in Unix-like environments, applications consult the TERM environment variable and associated terminfo databases managed by libraries like ncurses; for instance, TERM=xterm-256color signals 256-color capability via the Co#256 terminfo entry, while the presence of an RGB boolean indicates true color support. Additionally, some terminals set the COLORTERM environment variable to truecolor (or variants like 24bit) to explicitly advertise 24-bit color availability, a de facto convention adopted by modern emulators to aid programmatic detection.[22][24]
In the 2020s, adoption of these features has become widespread among terminal emulators, with iTerm2 and Alacritty providing full 256-color and true color support out of the box, including GPU-accelerated rendering for smooth performance in high-resolution displays. These emulators set appropriate environment variables and terminfo entries to ensure seamless integration with tools like ncurses-based applications.[25]
Specialized Escape Sequences
Operating System Command (OSC) Sequences
Operating System Command (OSC) sequences provide a mechanism for terminals to invoke operating system-level functions and set or query terminal status information at the application level, such as manipulating window properties, colors, and clipboard data. These extend beyond core ANSI control functions and are initiated by the escape character followed by a right square bracket (ESC ], or single-byte 0x9D in 8-bit environments), making them distinct from Control Sequence Introducer (CSI) commands that start with ESC [. The OSC format is standardized in ECMA-48 (adopted as ISO/IEC 6429), though many specific commands (Ps values) originated as extensions in early terminal emulators like the VT100 and have been widely adopted in modern implementations, particularly xterm and its derivatives.[1][15]
The general syntax for an OSC sequence is ESC ] Ps ; Pt BEL or ESC ] Ps ; Pt ST, where Ps is a numeric parameter indicating the command type, Pt is an optional text parameter providing additional data (such as strings or color specifications), and the sequence terminates with either BEL (bell character, ASCII 7) or ST (string terminator, ESC ). Whitespace between elements is typically ignored, allowing flexibility in implementation. For instance, to set the window title to "My Application", the sequence ESC ] 2 ; My Application BEL (or \e]2;My Application\a where \e is ESC and \a is BEL) is emitted, causing compatible terminals to update the title bar accordingly. To report the current status, Pt can be "?" such as ESC ] 2 ; ? ST, prompting the terminal to respond with the existing title via a CSI sequence.[15]
Common uses include window and icon management:
| Ps Value | Description | Example Sequence | Effect |
|---|
| 0 | Set both icon name and window title | ESC ] 0 ; Icon ; Title BEL | Updates icon label and title bar to "Title", with "Icon" for minimized view |
| 1 | Set icon name only | ESC ] 1 ; Icon BEL | Changes the icon label (e.g., for taskbar or window switcher) |
| 2 | Set window title only | ESC ] 2 ; Title BEL | Modifies the title bar text |
Querying follows the same Ps values with Pt = "?" to retrieve current values, enabling applications to synchronize status. Extended uses include hypertext features in modern terminals, where sequences like ESC ] 8 ; ; https://example.com ST associate hyperlinks with subsequent text until terminated by ESC ] 8 ; ; ST, allowing clickable URLs in output.[15][26]
OSC sequences differ from CSI commands in their scope and termination: while CSI handles low-level terminal attributes like cursor position (ending in letters like 'H' for cursor home), OSC targets application-facing status like UI elements, often interfacing with the host OS. This makes them suitable for shell prompts or programs to dynamically update visible metadata without altering display content.[15]
Key OSC commands also focus on color palette manipulation, which is essential for customizing terminal appearance without relying solely on SGR parameters. To change a specific color in the palette (e.g., one of the 16 ANSI colors indexed 0-15), the sequence OSC 4 ; c ; spec BEL is used, where c is the color index and spec is either a color name (like "red") or an RGB value in the format "rgb:rr/gg/bb" (with rr, gg, bb as two-digit hexadecimal values). An older, compatible form for the 16-color palette is OSC P nrrggbb BEL, where n is the index (0-15) and rrggbb are concatenated hexadecimal RGB components without slashes. For example, setting color 1 (red) to pure red uses \e]P1ff0000\a. These commands allow dynamic palette adjustments, supporting up to 256 colors in extended modes if the terminal is compiled accordingly. To reset a color to its default, OSC 104 ; c BEL restores the value for index c, ensuring reversibility in applications.[27][28]
Additional color-related commands target specific elements: OSC 10 ; spec BEL sets the default foreground color, OSC 11 ; spec BEL sets the background color, and OSC 12 ; spec BEL sets the text cursor foreground color, each using the same RGB specification format. These are particularly useful for global theme changes in terminal sessions. For clipboard access, a critical OS interaction, OSC 52 ; Pc ; Pd BEL enables reading from or writing to selection buffers. Here, Pc specifies the buffer type (e.g., "c" for clipboard, "p" for primary selection, "s" for secondary, or numbers 0-7 for cut buffers), and Pd is either base64-encoded data to write or "?" to query the current contents, which the terminal responds to via another OSC sequence. This facilitates copy-paste operations across remote sessions, such as in SSH or tmux environments.[29][30][5]
OSC sequences are inherently vendor-specific for many Ps values, leading to variations across terminals; for example, while xterm supports querying colors with OSC 4 ; c ; ? BEL to receive an OSC 4 response with the current value, not all emulators do. Their commonality stems from adoption in xterm derivatives like rxvt, urxvt, and even Windows Terminal, making them a de facto standard for OS interactions in Unix-like environments. However, compatibility requires testing, as some commands (e.g., clipboard operations) may fail in non-xterm-compatible terminals without proper selection handling.[15][21]
Security considerations arise with OSC sequences due to their potential for abuse in prompt injection attacks. Malicious input can embed control characters within Pt strings, such as newlines to inject shell commands (e.g., ESC ] 2 ; Fake Title\nmalicious_command BEL), leading to arbitrary code execution in vulnerable terminals like older versions of Terminology (CVE-2015-8971). Similarly, spoofed titles have enabled phishing in environments like Kubernetes by mimicking trusted prompts (CVE-2021-25743). Modern shells and terminals mitigate this by sanitizing inputs, but applications should escape user-provided strings in OSC parameters to prevent exploitation.[31][32][33]
Unix-like Systems
In Unix-like systems such as Linux and macOS, terminal emulators have provided native support for ANSI escape codes since the late 1980s and early 1990s, enabling control over text attributes, cursor positioning, and colors in text-based interfaces. xterm, developed in 1984 as an X11 terminal emulator, incorporates ANSI escape sequences through its emulation of the VT100 standard, which introduced these codes in 1978 for standardized terminal control. [GNOME Terminal](/page/GNOME Terminal), released with GNOME 1.0 in 1999, and Konsole, introduced as part of KDE in 1998, have included full ANSI support from their initial versions, allowing seamless integration with command-line applications. As of 2025, [GNOME Terminal](/page/GNOME Terminal) is being replaced by Ptyxis in recent GNOME-based distributions such as Ubuntu 25.10, which continues to support ANSI escape codes.[15][34][35]
The TERM environment variable is essential for specifying the terminal type, which dictates the available capabilities including ANSI escape code interpretation; for instance, TERM=xterm-256color activates support for extended 256-color palettes via sequences like \E[38;5;<n>m for foreground colors. This variable interfaces with the terminfo database, a compiled repository of terminal descriptions that maps capabilities—such as color setting (setaf=\E[3%p1%dm) and cursor addressing (cup=\E[%i%p1%d;%p2%dH)—to portable queries, ensuring applications adapt to the emulator's features without hardcoding sequences.[36][37]
Modern Linux distributions since around 2010 have widely adopted full 24-bit (true) color support in terminals, utilizing extended ANSI sequences like \E[38;2;<r>;<g>;<b>m for direct RGB specification, building on the basic 8- or 16-color foundations. Libraries such as ncurses abstract these capabilities by leveraging terminfo to generate vendor-neutral output, shielding developers from direct ANSI code management while supporting features up to true color in compatible environments.[38]
A frequent challenge in Unix-like systems involves legacy TERM settings like vt100, which emulate the original VT100 hardware lacking native color support—relying instead on basic attributes like bold (\E[1m)—resulting in uncolored output even when modern emulators could render more. Programs can detect and mitigate this using tput commands, such as tput colors to query the number of supported colors (e.g., outputting 8 for basic ANSI or 256 for extended), allowing conditional fallback to monochrome modes.[37][39]
Windows and DOS Environments
In MS-DOS environments, native support for ANSI escape codes was absent in early versions, requiring the loading of the ANSI.SYS device driver to enable basic terminal emulation features such as cursor control and text attributes.[40] Introduced with MS-DOS 2.0 in March 1983, ANSI.SYS interpreted a subset of ANSI sequences, including those for colors and screen clearing, but its functionality was limited compared to Unix terminals and depended on manual configuration via AUTOEXEC.BAT.[41] This driver remained compatible through subsequent MS-DOS releases but was discontinued in Windows NT-based systems starting from Windows 2000, leaving a gap in ANSI handling for command-line interfaces.[40]
The Windows Console Host (conhost.exe), used in versions prior to Windows 10, provided no native interpretation of ANSI escape sequences, relying instead on proprietary commands for basic formatting like the COLOR utility for text attributes.[21] Full ANSI/VT100 compatibility was introduced in the Windows 10 Anniversary Update (version 1607) on August 2, 2016, allowing sequences for cursor movement, colors, and other controls when enabled via the ENABLE_VIRTUAL_TERMINAL_PROCESSING console mode flag.[21] This update extended to 24-bit true color support through RGB escape codes, marking a significant alignment with cross-platform standards.[42]
To address legacy limitations in pre-Windows 10 environments or enhance compatibility, third-party libraries such as Colorama for Python wrap ANSI sequences to emulate them on Windows by translating to console API calls. Similarly, the Windows Terminal application, released in 2019 as a modern alternative to conhost.exe, natively supports extended ANSI features including 24-bit colors and is recommended for applications requiring rich terminal rendering.
In the 2020s, PowerShell 7, first released in 2018 and updated through versions like 7.4 in 2023 and 7.5 in 2025, incorporated enhancements for consistent ANSI rendering across Windows, Linux, and macOS, including PSStyle class methods for mapping colors to escape sequences and improved terminal detection.[43][44] These updates facilitate cross-platform scripting with ANSI-dependent tools, reducing discrepancies in output formatting.[45]
Practical Usage
Shell Scripting Examples
In shell scripting, ANSI escape codes are commonly employed to enhance output readability, such as by applying colors to text via the echo command with the -e option enabled for escape sequence interpretation.[46] For instance, the following Bash snippet prints red text followed by a reset to default formatting:
bash
echo -e "\e[31mRed text\e[0m"
echo -e "\e[31mRed text\e[0m"
This utilizes the Select Graphic Rendition (SGR) sequence \e[31m for red foreground and \e[0m to reset attributes.[46]
To improve portability across different terminals, scripts often use the tput utility, which queries the terminfo database based on the $TERM environment variable to generate appropriate escape sequences without hardcoding them.[39] A portable function for setting foreground colors might define variables like this:
bash
if [ -t 1 ]; then
RED=$(tput setaf 1 2>/dev/null)
GREEN=$(tput setaf 2 2>/dev/null)
RESET=$(tput sgr0 2>/dev/null)
fi
if [ -t 1 ]; then
RED=$(tput setaf 1 2>/dev/null)
GREEN=$(tput setaf 2 2>/dev/null)
RESET=$(tput sgr0 2>/dev/null)
fi
Here, tput setaf 1 sets the foreground to red (color index 1 in the standard 8-color palette), and the conditional ensures colors are only attempted if standard output is a terminal.[39][47] Usage could then be: echo "${RED}Red text${RESET}". This approach falls back gracefully on non-supporting environments by leaving variables empty.[47]
For dynamic displays like progress bars, cursor control sequences from the Control Sequence Introducer (CSI) allow overwriting output without scrolling.[48] A simple script might save the cursor position, update a bar on the same line using carriage return (\r) combined with CSI for erasure, and restore the position:
bash
# Save cursor position
echo -ne "\e[s"
for i in {1..20}; do
# Restore, erase line, print progress
progress=$(printf '%*s' "$i" '' | tr ' ' '#')
echo -ne "\e[u\e[2K\r[${progress}$((20-i))%]"
sleep 0.1
done
echo # Newline to restore normal flow
# Save cursor position
echo -ne "\e[s"
for i in {1..20}; do
# Restore, erase line, print progress
progress=$(printf '%*s' "$i" '' | tr ' ' '#')
echo -ne "\e[u\e[2K\r[${progress}$((20-i))%]"
sleep 0.1
done
echo # Newline to restore normal flow
The \e[s saves the cursor, \e[u restores it, and \e[2K erases the entire line, enabling in-place updates for visual feedback in supported terminals like xterm.[48]
Error handling in such scripts typically involves checking terminal capabilities to avoid garbled output in non-interactive or color-incapable sessions. The [ -t 1 ] test verifies if stdout is a tty, while tput colors can confirm color support (e.g., returning 8 or more for basic ANSI colors).[47] An example wrapper function:
bash
colorize() {
if [ -t 1 ] && [ "$(tput colors 2>/dev/null)" -ge 8 ]; then
echo -e "\e[31m$1\e[0m"
else
echo "$1"
fi
}
colorize() {
if [ -t 1 ] && [ "$(tput colors 2>/dev/null)" -ge 8 ]; then
echo -e "\e[31m$1\e[0m"
else
echo "$1"
fi
}
This ensures fallback to plain text when ANSI codes are unsupported, maintaining script reliability across environments like pipes or dumb terminals.[47]
Programming Language Integration
ANSI escape codes can be directly integrated into C programs using standard output functions from the <stdio.h> header, allowing developers to embed control sequences for formatting text in compatible terminals. For instance, the sequence \033[1;31m sets bold red foreground color, while \033[0m resets to default attributes. A simple example demonstrates this:
c
#include <stdio.h>
int main() {
printf("\033[1;31mBold red text\033[0m\n");
return 0;
}
#include <stdio.h>
int main() {
printf("\033[1;31mBold red text\033[0m\n");
return 0;
}
This approach outputs the escape sequences as raw bytes to stdout, which terminals interpret for visual effects like color and style changes.[49]
To abstract away direct escape code management and ensure portability across Unix-like systems, libraries such as ncurses provide higher-level interfaces for terminal control, including color handling via predefined pairs. Ncurses initializes color support with start_color() and defines pairs using init_pair(), such as init_pair(1, COLOR_RED, COLOR_BLACK) to associate pair 1 with red text on a black background, followed by attron(COLOR_PAIR(1)) to apply it. These functions translate to appropriate ANSI sequences based on the terminal's terminfo database entry, supporting features like cursor movement and screen updates without manual sequence construction.[38]
For cross-platform development, particularly targeting Windows environments where native console support for ANSI codes is limited, PDCurses offers a public domain implementation of the curses API that emulates Unix ncurses behavior on Windows consoles, DOS, and other platforms. PDCurses handles ANSI-compatible output through its functions, enabling the same color and positioning code to run portably without platform-specific adjustments in most cases. To achieve broader compatibility in pure ANSI C code, developers use conditional compilation directives like #ifdef _WIN32 to enable virtual terminal processing on Windows via SetConsoleMode with the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag before outputting sequences, ensuring escape codes are processed correctly while falling back to direct writes on Unix systems.[50][21]
Programs can dynamically detect terminal capabilities to avoid incompatible sequences by querying the Device Attributes (DA) using the primary DA sequence ESC [ c (CSI c with Ps=0 omitted), which prompts the terminal to respond with its identification and supported features, such as VT100-level compliance. For example, a VT220-compatible terminal might reply with CSI ? 62 ; 1 ; 2 ; 6 ; 7 ; 8 ; 9 c. This query allows conditional use of advanced sequences, enhancing robustness across diverse terminals without assuming full ANSI compliance. For color support, secondary DA (CSI > c) can provide additional details on extended features.[15]
ANSI escape codes enable terminals to transmit keyboard and mouse events to the host application using Control Sequence Introducer (CSI) sequences, which follow the general form ESC [ followed by parameters and a final character.[15] These input sequences allow applications to interpret user actions such as key presses and cursor movements without relying on raw character codes.[15]
For keyboard input, arrow keys and other navigation keys are encoded using simple CSI sequences in normal mode. The up arrow key sends ESC [ A, the down arrow ESC [ B, the right arrow ESC [ C, and the left arrow ESC [ D.[15] In application cursor key mode, enabled via the DECSET private mode ?1 h (DECCKM), these keys instead transmit Single Shift Three (SS3) sequences: SS3 A for up, SS3 B for down, SS3 C for right, and SS3 D for left.[51] Modifier keys like Shift, Ctrl, Alt, and combinations modify these sequences by inserting numeric parameters before the final character; for instance, Ctrl+Up arrow transmits ESC [ 1;5 A, where 5 denotes the Ctrl modifier.[15] Shift+Up uses 2 (ESC [ 1;2 A), and Alt+Up uses 3 (ESC [ 1;3 A), with higher numbers for combinations such as Ctrl+Shift+Up (6, ESC [ 1;6 A).[15]
Extended function keys F1 through F12 are represented using CSI sequences with tilde (~) terminators in PC-style keyboard mode. F1 sends ESC [ 11 ~, F2 sends ESC [ 12 ~, up to F12 sending ESC [ 24 ~.[15] These can include modifiers; for example, Shift+F1 transmits ESC [ 1;2 P (using P as the final for F1 in some variants, though ~ is standard for higher keys), and Ctrl+F1 uses ESC [ 1;5 P.[15] Alt combinations prepend ESC to the base sequence or use modifier codes like 9 for Alt in extended modes.[15]
Mouse input reporting is an extension beyond core ANSI standards, typically enabled through DECSET private modes to allow the terminal to send CSI sequences for mouse events.[15] The VT200 mouse reporting mode (?1000 h) uses the X10 protocol format, where a button press or release at position (x, y) sends ESC [ M b x' y', with b as the button code (32 + button index 0-2 for left/middle/right press, 35 for release), and x', y' as 1-based coordinates plus 32 (as character codes).[15] For more precise tracking, the button-event mouse tracking mode (?1002 h) reports presses, releases, and drags using ESC [ < b ; x ; y M/R, where < indicates the extended format, b is the button (0=left, 1=middle, 2=right, 3=release, plus modifiers), and x, y are cell-based positions; M for press, R for release. Motion is not reported in this mode unless combined with other settings.[15] The SGR extended mouse mode (?1006 h), often referred to in contexts like SGR 1000 for compatibility, supports larger coordinates and distinguishes events: ESC [ < b ; x ; y M for button press and motion (b encodes button and state, e.g., 0-2 for press, 32+ for motion), and ESC [ < b ; x ; y m for release (b=0-2), allowing reporting of positions in terminals larger than 223x223 cells, with coordinates up to 2015.[15] These modes can be disabled with ?1000 l, ?1002 l, or ?1006 l, respectively.[15]
Response and Reporting Sequences
Response and reporting sequences enable applications to query terminals for operational status, device identification, and other attributes using Control Sequence Introducer (CSI) formats, with terminals replying via similar escape sequences read from standard input.[52] These mechanisms, defined in standards like ECMA-48 and extended by DEC for VT-series terminals, facilitate dynamic adaptation of terminal behavior in text-based interfaces. Unlike control sequences that modify display state, reporting sequences elicit asynchronous responses to inform software about the terminal's configuration or position.[15]
The Device Attributes (DA) sequence requests identification of the terminal's type and capabilities. The primary DA query, sent as CSI c (or equivalently ESC [ c), prompts the terminal to report its architectural class and supported features in the format CSI ? Ps1 ; Ps2 ; ... c, where each Ps value indicates specific attributes such as VT level and options like 80/132-column support.[52] For a VT100 terminal, the typical response is CSI ? 1 ; 2 c, where 1 denotes VT100 compatibility and 2 indicates support for additional modes like the Advanced Video Option (AVO).[53] The secondary DA query, CSI > c (or ESC [ > c), elicits a response in the format CSI > Ps ; Pv ; Pc c, providing details on terminal ID (Ps, often 61 for VT220-like), firmware version number (Pv), and hardware options (Pc, such as 0 for standard keyboard).[15] This sequence, introduced in later VT models, allows applications to detect version-specific capabilities without assuming a fixed terminal type.[54]
Cursor position reporting, known as Device Position Report (DPR) or Cursor Position Report (CPR), uses CSI 6 n (or ESC [ 6 n) to request the current cursor location, with the terminal responding as CSI Pn ; Pn R (or ESC [ Pn ; Pn R), where the first Pn is the row (line) number and the second is the column number, both 1-based.[52] For example, if the cursor is at row 10, column 5, the response would be ESC [ 10 ; 5 R.[15] An extended variant, CSI ? 6 n, supported in some DEC terminals, includes page information in the response CSI Pn ; Pn ; Pn R, though many implementations omit the page parameter.[52] This reporting is essential for applications needing to track or restore cursor positions without visual assumptions.
Mode queries, part of the Device Status Report (DSR) using CSI ? Ps n (or ESC [ ? Ps n), allow interrogation of specific DEC private modes, with responses indicating whether the mode is reset (0) or set (2).[15] For instance, Ps = 1 queries the Cursor Keys Mode (DECCKM), which determines if cursor keys send application sequences (set) or ANSI cursor sequences (reset); the response is CSI ? 1 ; s n, where s = 2 for set or s = 0 for reset.[55] Other Ps values query modes like keyboard action (Ps = 67 for DECKAUM) or numeric keypad behavior, enabling software to adapt input handling dynamically.[15] These queries are particularly useful in emulators to verify compatibility with legacy VT100 behaviors.[52]
Version reporting is handled via the secondary DA sequence, as described earlier, where the Pv parameter in CSI > Ps ; Pv ; Pc c encodes the terminal's firmware or software version in a numeric format, such as 20 for version 2.0.[54] This allows applications to probe for supported features like extended character sets or mouse reporting without trial-and-error.[15] In practice, responses vary by terminal emulator; for example, xterm might return CSI > 83 ; 0 ; n c to indicate VT320 compatibility with version 0 and specific patches (n).[15]