ANSI escape code#ED

{{short description|Method used for display options on video text terminals}}

{{Redirect|ANSI code||ANSI (disambiguation)}}

{{Use dmy dates|date=May 2021}}

{{Infobox character encoding

|image=Htop.png

|caption=Output of the system-monitor htop, an ncurses-application (which uses {{ctrl|SGR|internal=1}} and other ANSI/ISO control sequences).

|name=ANSI X3.64 (ISO/IEC 6429)

|standard={{plainlist|

|classification=ISO/IEC 2022 based control code and control sequence set

|otherrelated={{plainlist|

}}

ANSI escape sequences are a standard for in-band signaling to control cursor location, color, font styling, and other options on video text terminals and terminal emulators. Certain sequences of bytes, most starting with an ASCII escape character and a bracket character, are embedded into text. The terminal interprets these sequences as commands, rather than text to display verbatim.

ANSI sequences were introduced in the 1970s to replace vendor-specific sequences and became widespread in the computer equipment market by the early 1980s. Although hardware text terminals have become increasingly rare in the 21st century, the relevance of the ANSI standard persists because a great majority of terminal emulators and command consoles interpret at least a portion of the ANSI standard.

History

Almost all manufacturers of video terminals added vendor-specific escape sequences to perform operations such as placing the cursor at arbitrary positions on the screen. One example is the VT52 terminal, which allowed the cursor to be placed at an x,y location on the screen by sending the {{code|ESC}} character, a {{code|Y}} character, and then two characters representing numerical values equal to the x,y location plus 32 (thus starting at the ASCII space character and avoiding the control characters). The Hazeltine 1500 had a similar feature, invoked using {{code|~}}, {{code|DC1}} and then the X and Y positions separated with a comma. While the two terminals had identical functionality in this regard, different control sequences had to be used to invoke them.

As these sequences were different for different terminals, elaborate libraries such as termcap ("terminal capabilities") and utilities such as tput had to be created so programs could use the same API to work with any terminal. In addition, many of these terminals required sending numbers (such as row and column) as the binary values of the characters; for some programming languages, and for systems that did not use ASCII internally, it was often difficult to turn a number into the correct character.

The ANSI standard attempted to address these problems by making a command set that all terminals would use and requiring all numeric information to be transmitted as ASCII numbers. The first standard in the series was ECMA-48, adopted in 1976. It was a continuation of a series of character coding standards, the first one being ECMA-6 from 1965, a 7-bit standard from which ISO 646 originates. The name "ANSI escape sequence" dates from 1979 when ANSI adopted ANSI X3.64. The ANSI X3L2 committee collaborated with the ECMA committee TC 1 to produce nearly identical standards. These two standards were merged into an international standard, ISO 6429. In 1994, ANSI withdrew its standard in favor of the international standard.

File:DEC VT100 terminal.jpg

The first popular video terminal to support these sequences was the Digital VT100, introduced in 1978. This model was very successful in the market, which sparked a variety of VT100 clones, among the earliest and most popular of which was the much more affordable Zenith Z-19 in 1979.{{cite web |url=http://www.pestingers.net/Computer_history/Computers_79.htm |title=Heathkit Catalog 1979 |author=Heathkit Company |date=1979 |publisher=Heathkit Company |access-date=2011-11-04 |url-status=dead |archive-url=https://web.archive.org/web/20120113230301/http://www.pestingers.net/Computer_history/Computers_79.htm |archive-date=2012-01-13}} Others included the Qume QVT-108, Televideo TVI-970, Wyse WY-99GT as well as optional "VT100" or "VT103" or "ANSI" modes with varying degrees of compatibility on many other brands. The popularity of these gradually led to more and more software (especially bulletin board systems and other online services) assuming the escape sequences worked, leading to almost all new terminals and emulator programs supporting them.

In 1981, ANSI X3.64 was adopted for use in the US government by FIPS publication 86. Later, the US government stopped duplicating industry standards, so FIPS pub. 86 was withdrawn.{{cite web|url=https://www.nist.gov/system/files/documents/2016/12/15/withdrawn_fips_by_numerical_order_index.pdf|title=Withdrawn FIPS Listed by Number|date=15 December 2016|website=National Institute of Standards and Technology|access-date=2 January 2022}}

ECMA-48 has been updated several times and is currently at its 5th edition, from 1991.{{cite web |url=https://ecma-international.org/publications-and-standards/standards/ecma-48/ |title=ECMA-48 — Control functions for coded character sets|publisher=ECMA International}} It is also adopted by ISO and IEC as standard ISO/IEC 6429.{{cite ISO standard|csnumber=12782 |title= ISO/IEC 6429:1992 — Information technology — Control functions for coded character sets |publisher=ISO}} A version is adopted as a Japanese Industrial Standard, as JIS X 0211.

Related standards include ITU T.61, the Teletex standard, and the ISO/IEC 8613, the Open Document Architecture standard (mainly ISO/IEC 8613-6 or ITU T.416). The two systems share many escape codes with the ANSI system, with extensions that are not necessarily meaningful to computer terminals. Both systems quickly fell into disuse, but ECMA-48 does mark the extensions used in them as reserved.

Platform support

File:XtermMenus.pngIn the early 1980s, large amounts of software directly used these sequences to update screen displays. This included everything on VMS (which assumed DEC terminals), most software designed to be portable on CP/M home computers, and even lots of Unix software as it was easier to use than the termcap libraries, such as the shell script examples below in this article.

Terminal emulators for communicating with remote machines almost always implement ANSI escape codes. This includes anything written to communicate with bulletin-board systems on home and personal computers. On Unix terminal emulators such as xterm also can communicate with software running on the same machine, and thus software running in X11 under a terminal emulator could assume the ability to write these sequences.

As computers got more powerful even built-in displays started supporting them, allowing software to be portable between CP/M systems. There were attempts to extend the escape sequences to support printers{{cite web |url=http://wiki.amigaos.net/wiki/Printer_Device#Printer_Command_Definitions |title=Amiga Printer Command Definitions |publisher=Commodore |access-date=2013-07-10}} and as an early PDF-like document storage format, the Open Document Architecture.{{cn|date=November 2023}}

= DOS and Windows =

The IBM PC, introduced in 1981, did not support these or any other escape sequences for updating the screen. Only a few control characters (BEL, CR, LF, BS) were interpreted by the underlying BIOS. Any display effects had to be done with BIOS calls, which were notoriously slow, or by directly manipulating the IBM PC hardware. This made any interesting software non-portable and led to the need to duplicate details of the display hardware in PC Clones.

DOS version 2.0 included an optional device driver named {{mono|ANSI.SYS}}. Poor performance, and the fact that it was not installed by default, meant software rarely (if ever) took advantage of it.

The Windows Console did not support ANSI escape sequences, nor did Microsoft provide any method to enable them. Some replacements such as JP Software's TCC (formerly 4NT), Michael J. Mefford's ANSI.COM, Jason Hood's {{mono|ANSICON}}{{cite web

|url=https://github.com/adoxa/ansicon

|title=Process ANSI escape sequences for Windows console programs

|publisher=Jason Hood's Home page

|author-first=Jason |author-last=Hood

|date=2005 |access-date=2013-05-09

}} and Maximus5's ConEmu enabled ANSI escape sequences. Software such as the Python colorama package{{cite web |url=https://pypi.python.org/pypi/colorama |title=colorama · PyPI |work=Python Package Index |access-date=2022-02-27}} or Cygwin modified text in-process as it was sent to the console, extracting the ANSI Escape sequences and emulating them with Windows calls.

In 2016, Microsoft released the Windows 10 version 1511 update which unexpectedly implemented support for ANSI escape sequences, over three decades after the debut of Windows.{{Cite web|url=https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences|title=Console Virtual Terminal Sequences - Windows Console|last=bitcrazed|website=docs.microsoft.com|language=en-us|access-date=2018-05-30}} This was done alongside Windows Subsystem for Linux, apparently to allow Unix-like terminal-based software to use the Windows Console. Windows PowerShell 5.1 enabled this by default, and PowerShell 6 made it possible to embed the necessary ESC character into a string with {{code|`e}}.{{cite web|url=https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-6|title=PowerShell Help: About Special Characters|date=12 January 2023 }}

Windows Terminal, introduced in 2019, supports the sequences by default, and Microsoft intends to replace the Windows Console with Windows Terminal.{{cite web

|url = https://docs.microsoft.com/en-us/windows/console/ecosystem-roadmap#roadmap-for-the-future

|title = Windows Console and Terminal Ecosystem Roadmap

|publisher = Microsoft

|date = 2018

|access-date = 2021-03-13

| quote=this includes retiring the classic console host user interface from its default position in favor of Windows Terminal, ConPTY, and virtual terminal sequences.

}}

C0 control codes

Almost all users assume some functions of some single-byte characters. Initially defined as part of ASCII, the default C0 control code set is now defined in ISO 6429 (ECMA-48), making it part of the same standard as the C1 set invoked by the ANSI escape sequences (although ISO 2022 allows the ISO 6429 C0 set to be used without the ISO 6429 C1 set, and vice versa, provided that 0x1B is always ESC). This is used to shorten the amount of data transmitted, or to perform some functions that are unavailable from escape sequences:

class="wikitable"

|+ Popular C0 control codes (not an exhaustive list)

^C0AbbrNameEffect
id=BEL

| {{tt|^G}}

0x07{{tt|BEL}}BellMakes an audible noise.
id=BS

| {{tt|^H}}

0x08{{tt|BS}}BackspaceMoves the cursor left (but may "backwards wrap" if cursor is at start of line).
id=HT

| {{tt|^I}}

0x09{{tt|HT}}TabMoves the cursor right to next tab stop.
id=LF

| {{tt|^J}}

0x0A{{tt|LF}}Line FeedMoves 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.
id=FF

| {{tt|^L}}

0x0C{{tt|FF}}Form FeedMove a printer to top of next page. Usually does not move horizontally, though programs should not rely on this. Effect on video terminals varies.
id=CR

| {{tt|^M}}

0x0D{{tt|CR}}{{nobr|Carriage Return}}Moves the cursor to column zero.
id=ESC

| {{tt|^[}}

0x1B{{tt|{{visible anchor|ESC}}}}EscapeStarts all the escape sequences

Escape sequences vary in length. The general format for an ANSI-compliant escape sequence is defined by ANSI X3.41 (equivalent to ECMA-35 or ISO/IEC 2022).{{Rp|13.1}} The escape sequences consist only of bytes in the range {{tt|0x20—0x7F}} (all the non-control ASCII characters), and can be parsed without looking ahead. The behavior when a control character, a byte with the high bit set, or a byte that is not part of any valid sequence, is encountered before the end is undefined.

Fe Escape sequences

If the #ESC is followed by a byte in the range 0x40 to 0x5F, the escape sequence is of type {{code|Fe}}. Its interpretation is delegated to the applicable C1 control code standard.{{Rp|13.2.1}} Accordingly, all escape sequences corresponding to C1 control codes from ANSI X3.64 / ECMA-48 follow this format.{{Rp|5.3.a}}

The standard says that, in 8-bit environments, the control functions corresponding to type {{code|Fe}} escape sequences (those from the set of C1 control codes) can be represented as single bytes in the 0x80–0x9F range.{{Rp|5.3.b}} This is possible in character encodings conforming to the provisions for an 8-bit code made in ISO 2022, such as the ISO 8859 series. However, in character encodings used on modern devices such as UTF-8 or CP-1252, those codes are often used for other purposes, so only the 2-byte sequence is typically used. In the case of UTF-8, representing a C1 control code via the C1 Controls and Latin-1 Supplement block results in a different two-byte code (e.g. {{tt|0xC2,0x8E}} for {{tt|U+008E}}), but no space is saved this way.

class="wikitable"

|+ Some type {{tt|Fe}} (C1 set element) ANSI escape sequences (not an exhaustive list)

CodeC1AbbrNameEffect
id=SS2

| {{tt|ESC N}}

| {{tt|0x8E}}

| {{tt|SS2}}

| Single Shift Two

| rowspan=2 | 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.{{Rp|9.4}} In an 8-bit environment, these may instead be GR bytes (0xA0–0xFF).{{Rp|8.4}}

id=SS3

| {{nobr|{{tt|ESC O}}}}

| {{tt|0x8F}}

| {{tt|SS3}}

| Single Shift Three

id=DCS

| {{tt|ESC P}}

| {{tt|0x90}}

| {{tt|DCS}}

| Device Control String

| Terminated by ST.{{Rp|5.6}} Xterm's uses of this sequence include defining User-Defined Keys, and requesting or setting Termcap/Terminfo data.

id=CSI

| {{tt|ESC [}}

| {{tt|0x9B}}

| {{tt|CSI}}

| Control Sequence Introducer

| Starts most of the useful sequences, terminated by a byte in the range 0x40 through 0x7E.{{Rp|5.4}}

id=ST

| {{tt|ESC \}}

| {{tt|0x9C}}

| {{tt|ST}}

| String Terminator

| Terminates strings in other controls.{{Rp|8.3.143}}

{{tt|ESC ]}}

| {{tt|0x9D}}

| {{tt|OSC}}

| Operating System Command

| Starts a control string for the operating system to use, terminated by ST.{{Rp|8.3.89}}

id=SOS

| {{tt|ESC X}}

| {{tt|0x98}}

| {{tt|SOS}}

| Start of String

| rowspan=3 | Takes an argument of a string of text, terminated by ST.{{Rp|5.6}} The uses for these string control sequences are defined by the application{{Rp|8.3.2,8.3.128}} or privacy discipline.{{Rp|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.{{cite book|author1=Frank da Cruz|author2=Christine Gianone|title=Using C-Kermit|url=https://books.google.com/books?id=QuYQPc-c67MC&pg=PA278|year=1997|publisher=Digital Press|isbn=978-1-55558-164-0|page=278}}

id=PM

| {{tt|ESC ^}}

| {{tt|0x9E}}

| {{tt|PM}}

| Privacy Message

id=APC

| {{tt|ESC _}}

| {{tt|0x9F}}

| {{tt|APC}}

| {{nobr|Application Program Command}}

{{anchor|CBT|CCH|CHT|CTC|CVT}}

{{anchor|DA|DAQ|DCH|DL|DMI|EA|ECH|EF|EMI}}

{{anchor|EPA|ESA|FNT|GSM|GSS|HPA|HPR|HTJ|HTS|ICH}}

{{anchor|IL|IND|INT|JFY|MC|MW|NEL|NP|PLD}}

{{anchor|PLU|PP|PU1|PU2|QUAD|REP|RI|RIS|RM}}

{{anchor|SEM|SL|SM|SPA|SPI|SR|SSA}}

{{anchor|STS|TBC|TSS|VPA|VPR|VTS}}

{{anchor|SKR|KR}}

{{anchor|CSIsection}} Control Sequence Introducer commands

For Control Sequence Introducer, or CSI, commands, the {{code|ESC [}} (written as {{code|\e[}}, {{code|\x1b[}} or {{code|\033[}} in several programming languages) is followed by any number (including none) of "parameter bytes" in the range 0x30–0x3F (ASCII 0–9:;<=>?), then by any number of "intermediate bytes" in the range 0x20–0x2F (ASCII space and {{code|!"#$%&'()*+,-./}}), then finally by a single "final byte" in the range 0x40–0x7E (ASCII @A–Z[\]^_`a–z

}~).{{Rp|5.4}}

All common sequences just use the parameters as a series of semicolon-separated numbers such as {{code|1;2;3}}.{{Rp|page=5.4.2}} Missing numbers are treated as {{code|0}} ({{code|1;;3}} acts like the middle number is {{code|0}}, and no parameters at all in {{code|ESC[m}} acts like a {{code|0}} reset code). Some sequences (such as CUU) treat {{code|0}} as {{code|1}} in order to make missing parameters useful.{{Rp|F.4.2}}

A subset of arrangements was declared "private" so that terminal manufacturers could insert their own sequences without conflicting with the standard. Sequences containing the parameter bytes <=>? or the final bytes 0x70–0x7E (p–z{

~) are private.

The behavior of the terminal is undefined in the case where a CSI sequence contains any character outside of the range 0x20–0x7E. These illegal characters are either C0 control characters (the range 0–0x1F), DEL (0x7F), or bytes with the high bit set. Possible responses are to ignore the byte, to process it immediately, and furthermore whether to continue with the CSI sequence, to abort it immediately, or to ignore the rest of it.{{Citation needed|date=July 2015}}

class="wikitable"

|+ Some ANSI control sequences (not an exhaustive list)

CodeAbbrNameEffect
id=CUU

| style="font-family:monospace;" | CSI {{var|n}} A

| {{tt|CUU}}

| Cursor Up

| rowspan=4 | Moves the cursor {{var|n}} (default {{code|1}}) cells in the given direction. If the cursor is already at the edge of the screen, this has no effect.

id=CUD

| style="font-family:monospace;" | CSI {{var|n}} B

| {{tt|CUD}}

| Cursor Down

id=CUF

| style="font-family:monospace;" | CSI {{var|n}} C

| {{tt|CUF}}

| Cursor Forward

id=CUB

| style="font-family:monospace;" | CSI {{var|n}} D

| {{tt|CUB}}

| Cursor Back

id=CNL

| style="font-family:monospace;" | CSI {{var|n}} E

| {{tt|CNL}}

| Cursor Next Line

| Moves cursor to beginning of the line {{var|n}} (default {{code|1}}) lines down. (not ANSI.SYS)

id=CPL

| style="font-family:monospace;" | CSI {{var|n}} F

| {{tt|CPL}}

| Cursor Previous Line

| Moves cursor to beginning of the line {{var|n}} (default {{code|1}}) lines up. (not ANSI.SYS)

id=CHA

| style="font-family:monospace;" | CSI {{var|n}} G

| {{tt|CHA}}

| {{nobr|Cursor Horizontal Absolute}}

| Moves the cursor to column {{var|n}} (default {{code|1}}). (not ANSI.SYS)

id=CUP

| style="font-family:monospace;" | CSI {{var|n}} ; {{var|m}} H

| {{tt|CUP}}

| Cursor Position

| Moves the cursor to row {{var|n}}, column {{var|m}}. The values are 1-based, and default to {{code|1}} (top left corner) if omitted. A sequence such as {{code|CSI ;5H}} is a synonym for {{code|CSI 1;5H}} as well as {{code|CSI 17;H}} is the same as {{code|CSI 17H}} and {{code|CSI 17;1H}}

id=ED

| style= "font-family:monospace;" | CSI {{var|n}} J

| {{tt|ED}}

| Erase in Display

| Clears part of the screen. If {{var|n}} is {{code|0}} (or missing), clear from cursor to end of screen. If {{var|n}} is {{code|1}}, clear from cursor to beginning of the screen. If {{var|n}} is {{code|2}}, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS). If {{var|n}} is {{code|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).

id=EL

| style="font-family:monospace;" | CSI {{var|n}} K

| {{tt|EL}}

| Erase in Line

| Erases part of the line. If {{var|n}} is {{code|0}} (or missing), clear from cursor to the end of the line. If {{var|n}} is {{code|1}}, clear from cursor to beginning of the line. If {{var|n}} is {{code|2}}, clear entire line. Cursor position does not change.

id=SU

| style="font-family:monospace;" | CSI {{var|n}} S

| {{tt|SU}}

| Scroll Up

| Scroll whole page up by {{var|n}} (default {{code|1}}) lines. New lines are added at the bottom. (not ANSI.SYS)

id=SD

| style="font-family:monospace;" | CSI {{var|n}} T

| {{tt|SD}}

| Scroll Down

| Scroll whole page down by {{var|n}} (default {{code|1}}) lines. New lines are added at the top. (not ANSI.SYS)

id=HVP

| style="font-family:monospace;" | CSI {{var|n}} ; {{var|m}} f

| {{tt|HVP}}

| {{nobr|Horizontal Vertical Position}}

| Same as CUP, but counts as a format effector function (like {{control code link|CR}} or {{control code link|LF}}) rather than an editor function (like CUD or CNL). This can lead to different handling in certain terminal modes.{{Rp|Annex A}}

style="font-family:monospace;" | CSI {{var|n}} m

| {{tt|SGR}}

| Select Graphic Rendition

| Sets colors and style of the characters following this code

id=AUX

| style="font-family:monospace;" | CSI 5i

|

| AUX Port On

| Enable aux serial port usually for local serial printer

id=noAUX

| style="font-family:monospace;" | CSI 4i

|

| AUX Port Off

| Disable aux serial port usually for local serial printer

{{anchor|CPR}}

id=DSR

| style="font-family:monospace;" | CSI 6n

| {{tt|DSR}}

| Device Status Report

| Reports the cursor position (CPR) by transmitting {{code|ESC[n;mR}}, where {{var|n}} is the row and {{var|m}} is the column.

class="wikitable"

|+ Some popular private sequences

CodeAbbrNameEffect
id=SCP

| style="font-family:monospace;" | CSI s

| {{tt|SCP, SCOSC}}

| Save Current Cursor Position

| Saves the cursor position/state in SCO console mode.{{cite web |url=https://vt100.net/docs/vt510-rm/SCOSC.html |title=SCOSC—Save Current Cursor Position |publisher=DEC |work=VT510 Video Terminal Programmer Information}} In vertical split screen mode, instead used to set (as CSI n ; n s) or reset left and right margins.{{cite web |url=https://vt100.net/docs/vt510-rm/DECSLRM.html |title=DECSLRM—Set Left and Right Margins |publisher=DEC |work=VT510 Video Terminal Programmer Information}}

id=RCP

| style="font-family:monospace;" | CSI u

| {{tt|RCP, SCORC}}

| {{nobr|Restore Saved Cursor Position}}

| Restores the cursor position/state in SCO console mode.{{cite web |url=https://vt100.net/docs/vt510-rm/SCORC.html |title=SCORC—Restore Saved Cursor Position |publisher=DEC |work=VT510 Video Terminal Programmer Information}}

style="font-family:monospace;" | CSI ? 25 h

| {{tt|DECTCEM}}

|

| Shows the cursor, from the VT220.

style="font-family:monospace;" | CSI ? 25 l

| {{tt|DECTCEM}}

|

| Hides the cursor.

style="font-family:monospace;" | CSI ? 1004 h

|

|

| Enable reporting focus. Reports whenever terminal emulator enters or exits focus as {{code|ESC [I}} and {{code|ESC [O}}, respectively.

style="font-family:monospace;" | CSI ? 1004 l

|

|

| Disable reporting focus.

style="font-family:monospace;" | CSI ? 1049 h

|

|

| Enable alternative screen buffer, from xterm

style="font-family:monospace;" | CSI ? 1049 l

|

|

| Disable alternative screen buffer, from xterm

style="font-family:monospace;" | CSI ? 2004 h

|

|

| Turn on bracketed paste mode.{{cite web |first1=Edward |last1=Moy |first2=Stephen |last2=Gildea |first3=Thomas | last3=Dickey |title=XTerm Control Sequences |url=https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_ |at=Functions using CSI, ordered by the final character(s) |access-date=4 February 2022}} In bracketed paste mode, text pasted into the terminal will be surrounded by {{code|ESC [200~}} and {{code|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).{{cite web|url=https://cirw.in/blog/bracketed-paste|title=bracketed paste mode|author=Conrad Irwin|date=April 2013|website=cirw.in}} From xterm{{cite web |first1=Edward |last1=Moy |first2=Stephen |last2=Gildea |first3=Thomas | last3=Dickey |title=XTerm Control Sequences |url=https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Bracketed-Paste-Mode |at=Bracketed Paste Mode |access-date=4 February 2022}}

style="font-family:monospace;" | CSI ? 2004 l

|

|

| Turn off bracketed paste mode.

{{anchor|SGR}} Select Graphic Rendition parameters

The control sequence CSI {{var|n}} m, named Select Graphic Rendition (SGR), sets display attributes. Several attributes can be set in the same sequence, separated by semicolons.{{Cite web|url=http://man7.org/linux/man-pages/man4/console_codes.4.html|title=console_codes(4) - Linux manual page|website=man7.org|access-date=2018-03-23}} Each display attribute remains in effect until a following occurrence of SGR resets it. If no codes are given, {{code|CSI m}} is treated as {{code|CSI 0 m}} (reset / normal).

class="wikitable"

! n!! Name !! Note

{{tt|0}}

| Reset or normal

| All attributes become turned off

{{tt|1}}

| Bold or increased intensity

| As with faint, the color change is a PC (SCO / CGA) invention.{{cite web |title=screen(HW) |url=http://osr507doc.sco.com/man/html.HW/screen.HW.html |website=SCO OpenServer Release 5.0.7 Manual |date=11 February 2003}}{{better source needed|date=June 2020}}

{{tt|2}}

| Faint, decreased intensity, or dim

| May be implemented as a light font weight like bold.{{cite web |title=Bug 791596 – Thoughts about faint (SGR 2) |url=https://bugzilla.gnome.org/show_bug.cgi?id=791596 |website=bugzilla.gnome.org}}

{{tt|3}}

| Italic

| Not widely supported. Sometimes treated as inverse or blink.

{{tt|4}}

| Underline

| Style extensions exist for Kitty, VTE, mintty, iTerm2 and Konsole.{{cite web |title=Curly and colored underlines (#6382) · Issues · George Nachman / iterm2 |url=https://gitlab.com/gnachman/iterm2/-/issues/6382 |website=GitLab |date=11 December 2017 |language=en}}{{cite web |title=Extensions to the xterm protocol |url=https://sw.kovidgoyal.net/kitty/protocol-extensions.html#colored-and-styled-underlines |website=kitty documentation |language=en |access-date=2020-07-01}}{{cite web |title=Curly and colored underlines |url=https://bugs.kde.org/387811 |website=KDE bugtracker |date=27 August 2022 |language=en}}

{{tt|5}}

| Slow blink

| Sets blinking to less than 150 times per minute

{{tt|6}}

| Rapid blink

| MS-DOS ANSI.SYS, 150+ per minute; not widely supported

{{tt|7}}

| Reverse video or invert

| Swap foreground and background colors; inconsistent emulation{{cite web |title=console-termio-realizer |url=http://jdebp.uk/Softwares/nosh/guide/commands/console-termio-realizer.xml |website=jdebp.uk}}{{dubious|inconsistent emulation|date=April 2023}}

{{tt|8}}

| Conceal or hide

| Not widely supported.

{{tt|9}}

| Crossed-out, or strike

| Characters legible but marked as if for deletion. Not supported in Terminal.app.

{{tt|10}}

| Primary (default) font

|

{{tt|11–19}}

| Alternative font

| Select alternative font {{math|n − 10}}

{{tt|20}}

| Fraktur (Gothic)

| Rarely supported

{{tt|21}}

| Doubly underlined; or: not bold

| Double-underline per ECMA-48,{{Rp|8.3.117}} but instead disables bold intensity on several terminals, including in the Linux kernel's console before version 4.17.{{cite web |url=https://man7.org/linux/man-pages/man4/console_codes.4.html |title=console_codes(4) |work=Linux Programmer's Manual |publisher=Linux man-pages project |version=5.10}}

{{tt|22}}

| Normal intensity

| Neither bold nor faint; color changes where intensity is implemented as such.

{{tt|23}}

| Neither italic, nor blackletter

|

{{tt|24}}

| Not underlined

| Neither singly nor doubly underlined

{{tt|25}}

| Not blinking

|Turn blinking off

{{tt|26}}

| Proportional spacing

| ITU T.61 and T.416, not known to be used on terminals

{{tt|27}}

| Not reversed

|

{{tt|28}}

| Reveal

| Not concealed

{{tt|29}}

| Not crossed out

|

{{tt|30–37}}

| Set foreground color

|

{{tt|38}}

| Set foreground color

| #8-bit

{{tt|39}}

| Default foreground color

| Implementation defined (according to standard)

{{tt|40–47}}

| Set background color

|

{{tt|48}}

| Set background color

| #8-bit

{{tt|49}}

| Default background color

| Implementation defined (according to standard)

{{tt|50}}

| Disable proportional spacing

| T.61 and T.416

{{tt|51}}

| Framed

| rowspan=2| Implemented as "emoji variation selector" in mintty.{{cite web |title=mintty/mintty: Text attributes and rendering |url=https://github.com/mintty/mintty/wiki/Tips#text-attributes-and-rendering |website=GitHub |language=en}}

{{tt|52}}

| Encircled

{{tt|53}}

| Overlined

| Not supported in Terminal.app

{{tt|54}}

| Neither framed nor encircled

|

{{tt|55}}

| Not overlined

|

{{tt|58}}

| Set underline color

| Not in standard; implemented in Kitty, VTE, mintty, and iTerm2. #8-bit.

{{tt|59}}

| Default underline color

| Not in standard; implemented in Kitty, VTE, mintty, and iTerm2.

{{tt|60}}

| Ideogram underline or right side line

| rowspan="5"| Rarely supported

{{tt|61}}

| Ideogram double underline, or double line on the right side

{{tt|62}}

| Ideogram overline or left side line

{{tt|63}}

| Ideogram double overline, or double line on the left side

{{tt|64}}

| Ideogram stress marking

{{tt|65}}

| No ideogram attributes

| Reset the effects of all of {{code|60}}–{{code|64}}

{{tt|73}}

| Superscript

| rowspan=3| Implemented only in mintty

{{tt|74}}

| Subscript

{{tt|75}}

| Neither superscript nor subscript

{{tt|90–97}}

| Set bright foreground color

| rowspan=2| Not in standard; originally implemented by aixterm

{{tt|{{nobr|100–107}}}}

| Set bright background color

= {{anchor|colors}} Colors =

== 3-bit and 4-bit ==

The original specification only had 8 colors, and just gave them names. The SGR parameters 30–37 selected the foreground color, while 40–47 selected the background. Quite a few terminals implemented "bold" (SGR code 1) as a brighter color rather than a different font, thus providing 8 additional foreground colors. Usually you could not get these as background colors, though sometimes inverse video (SGR code 7) would allow that. Examples: to get black letters on white background use {{code|ESC[30;47m}}, to get red use {{code|ESC[31m}}, to get bright red use {{code|ESC[1;31m}}. To reset colors to their defaults, use {{code|ESC[39;49m}} (not supported on some terminals), or reset all attributes with {{code|ESC[0m}}. Later terminals added the ability to directly specify the "bright" colors with 90–97 and 100–107.

The chart below shows a few examples of how VGA standard and modern terminal emulators translate the 4-bit color codes into 24-bit color codes.

class="wikitable"
{{abbr|FG|Foreground}}

! {{abbr|BG|Background}}

! Name

! VGA{{ efn | Typical colors that are used when booting PCs and leaving them in text mode, which used a 16-entry color table. The colors are different in the EGA/VGA graphic modes. }}

! Windows XP
Console
{{ efn | Seen in Windows XP through Windows 8.1 }}

! Windows
PowerShell&
1.0–6.0
{{ efn | PowerShell's default shortcut .lnk, unchanged for over a decade, remaps yellow and magenta to give PowerShell distinctive foreground/background colors compared to the Command Prompt.{{Cite web |title=default shortcut on Windows remaps ANSI colors 35,36 · Issue #4266 · PowerShell/PowerShell |url=https://github.com/PowerShell/PowerShell/issues/4266 |access-date=2022-12-21 |website=GitHub |language=en}} PowerShell 7 is unaffected.}}

! Visual Studio Code{{efn|Debug console, "Dark+" theme}}

! Windows 10
Console
{{efn | Campbell theme, used as of Windows 10 version 1709.}}

! Terminal.app

! PuTTY

! mIRC

! xterm

! Ubuntu{{efn | For virtual terminals, from /etc/vtrgb.}}

!Eclipse Terminal

{{tt|30}}{{tt|40}}Black

| colspan="4" style="background: #000; color: white" | 0,{{nnbsp}}0,{{nnbsp}}0

| style="background: #0c0c0c; color: white" | 12,{{nnbsp}}12,{{nnbsp}}12

| colspan="4" style="background: #000; color: white" | 0,{{nnbsp}}0,{{nnbsp}}0

| style="background: #010101; color: white" | 1,{{nnbsp}}1,{{nnbsp}}1

| style="background: #000; color: white" | 0,{{nnbsp}}0,{{nnbsp}}0

{{tt|31}}{{tt|41}}Red

| style="background: #AA0000; color: white" | 170,{{nnbsp}}0,{{nnbsp}}0

| colspan="2" style="background: #800000; color: white" | 128,{{nnbsp}}0,{{nnbsp}}0

| style="background: #cd3131; color: white" | 205, 49, 49

| style="background: #c50f1f; color: white" | 197,{{nnbsp}}15,{{nnbsp}}31

| style="background: #990000; color: white" | 153,{{nnbsp}}0,{{nnbsp}}0

| style="background: #bb0000; color: white" | 187,{{nnbsp}}0,{{nnbsp}}0

| style="background: #7f0000; color: white" | 127,{{nnbsp}}0,{{nnbsp}}0

| style="background: #cd0000; color: white" | 205,{{nnbsp}}0,{{nnbsp}}0

| style="background: #de382b; color: white" | 222,{{nnbsp}}56,{{nnbsp}}43

| style="background: #cd0000; color: white" | 205,{{nnbsp}}0,{{nnbsp}}0

{{tt|32}}{{tt|42}}Green

| style="background: #00AA00; color: white" | 0,{{nnbsp}}170,{{nnbsp}}0

| colspan="2" style="background: #008000; color: white" | 0,{{nnbsp}}128,{{nnbsp}}0

| style="background: #0dbc79; color: white" | 13, 188, 121

| style="background: #13a10e; color: white" | 19,{{nnbsp}}161,{{nnbsp}}14

| style="background: #00A600; color: white" | 0,{{nnbsp}}166,{{nnbsp}}0

| style="background: #00bb00; color: white" | 0,{{nnbsp}}187,{{nnbsp}}0

| style="background: #009300; color: white" | 0,{{nnbsp}}147,{{nnbsp}}0

| style="background: #00cd00; color: white" | 0,{{nnbsp}}205,{{nnbsp}}0

| style="background: #39b54a; color: black" | 57,{{nnbsp}}181,{{nnbsp}}74

| style="background: #00cd00; color: white" | 0,{{nnbsp}}205,{{nnbsp}}0

{{tt|33}}{{tt|43}}Yellow

| style="background: #AA5500; color: white" | 170,{{nnbsp}}85,{{nnbsp}}0{{ efn | 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{{cite web|url=https://int10h.org/blog/2022/06/ibm-5153-color-true-cga-palette/|title=The IBM 5153's True CGA Palette and Color Output|publisher=VileR|date=2022-06-11|access-date=2024-05-18}} }}

| style="background: #808000; color: white" | 128,{{nnbsp}}128,{{nnbsp}}0

| style="background: #eeedf0; color: black" | 238,{{nnbsp}}237,{{nnbsp}}240

| style="background: #e5e510; color: black" | 229, 229, 16

| style="background: #c19c00; color: white" | 193,{{nnbsp}}156,{{nnbsp}}0

| style="background: #999900; color: white" | 153,{{nnbsp}}153,{{nnbsp}}0

| style="background: #bbbb00; color: black" | 187,{{nnbsp}}187,{{nnbsp}}0

| style="background: #fc7f00; color: white" | 252,{{nnbsp}}127,{{nnbsp}}0

| style="background: #cdcd00; color: black" | 205,{{nnbsp}}205,{{nnbsp}}0

| style="background: #ffc706; color: black" | 255,{{nnbsp}}199,{{nnbsp}}6

| style="background: #cdcd00; color: black" | 205,{{nnbsp}}205,{{nnbsp}}0

{{tt|34}}{{tt|44}}Blue

| style="background: #0000AA; color: white" | 0,{{nnbsp}}0,{{nnbsp}}170

| colspan="2" style="background: #000080; color: white" | 0,{{nnbsp}}0,{{nnbsp}}128

| style="background: #2472c8; color: white" | 36, 114, 200

| style="background: #0037da; color: white" | 0,{{nnbsp}}55,{{nnbsp}}218

| style="background: #0000B2; color: white" | 0,{{nnbsp}}0,{{nnbsp}}178

| style="background: #0000bb; color: white" | 0,{{nnbsp}}0,{{nnbsp}}187

| style="background: #00007f; color: white" | 0,{{nnbsp}}0,{{nnbsp}}127

| style="background: #0000ee; color: white" | 0,{{nnbsp}}0,{{nnbsp}}238Changed from 0,{{nnbsp}}0,{{nnbsp}}205 in July 2004 {{cite web |url=http://invisible-island.net/xterm/xterm.log.html#xterm_192 |title=Patch #192 – 2004/7/12 – XFree86 4.4.99.9}}

| style="background: #006fb8; color: white" | 0,{{nnbsp}}111,{{nnbsp}}184

| style="background: #0000ee; color: white" | 0,{{nnbsp}}0,{{nnbsp}}238

{{tt|35}}{{tt|45}}Magenta

| style="background: #AA00AA; color: white" | 170,{{nnbsp}}0,{{nnbsp}}170

| style="background: #800080; color: white" | 128,{{nnbsp}}0,{{nnbsp}}128

| style="background: #012456; color: white" | 1,{{nnbsp}}36,{{nnbsp}}86

| style="background: #bc3fbc; color: white" | 188, 63, 188

| style="background: #881798; color: white" | 136,{{nnbsp}}23,{{nnbsp}}152

| style="background: #B200B2; color: white" | 178,{{nnbsp}}0,{{nnbsp}}178

| style="background: #bb00bb; color: white" | 187,{{nnbsp}}0,{{nnbsp}}187

| style="background: #9c009c; color: white" | 156,{{nnbsp}}0,{{nnbsp}}156

| style="background: #cd00cd; color: white" | 205,{{nnbsp}}0,{{nnbsp}}205

| style="background: #762671; color: white" | 118,{{nnbsp}}38,{{nnbsp}}113

| style="background: #cd00cd; color: white" | 205,{{nnbsp}}0,{{nnbsp}}205

{{tt|36}}{{tt|46}}Cyan

| style="background: #00AAAA; color: white" | 0,{{nnbsp}}170,{{nnbsp}}170

| colspan="2" style="background: #008080; color: white" | 0,{{nnbsp}}128,{{nnbsp}}128

| style="background: #11a8cd; color: white" | 17, 168, 205

| style="background: #3a96dd; color: white" | 58,{{nnbsp}}150,{{nnbsp}}221

| style="background: #00A6B2; color: white" | 0,{{nnbsp}}166,{{nnbsp}}178

| style="background: #00bbbb; color: black" | 0,{{nnbsp}}187,{{nnbsp}}187

| style="background: #009393; color: white" | 0,{{nnbsp}}147,{{nnbsp}}147

| style="background: #00cdcd; color: black" | 0,{{nnbsp}}205,{{nnbsp}}205

| style="background: #2cb5e9; color: black" | 44,{{nnbsp}}181,{{nnbsp}}233

| style="background: #00cdcd; color: black" | 0,{{nnbsp}}205,{{nnbsp}}205

{{tt|37}}{{tt|47}}White

| style="background: #AAAAAA; color: black" | 170,{{nnbsp}}170,{{nnbsp}}170

| colspan="2" style="background: #c0c0c0; color: black" | 192,{{nnbsp}}192,{{nnbsp}}192

| style="background: #e5e5e5; color: black" | 229, 229, 229

| style="background: #cccccc; color: black" | 204,{{nnbsp}}204,{{nnbsp}}204

| style="background: #BFBFBF; color: black" | 191,{{nnbsp}}191,{{nnbsp}}191

| style="background: #bbbbbb; color: black" | 187,{{nnbsp}}187,{{nnbsp}}187

| style="background: #d2d2d2; color: black" | 210,{{nnbsp}}210,{{nnbsp}}210

| style="background: #e5e5e5; color: black" | 229,{{nnbsp}}229,{{nnbsp}}229

| style="background: #cccccc; color: black" | 204,{{nnbsp}}204,{{nnbsp}}204

| style="background: #e5e5e5; color: black" | 229,{{nnbsp}}229,{{nnbsp}}229

{{tt|90}}{{tt|100}}{{nobr|Bright Black (Gray)}}

| style="background: #555555; color: white" | 85,{{nnbsp}}85,{{nnbsp}}85

| colspan="2" style="background: #808080; color: black" | 128,{{nnbsp}}128,{{nnbsp}}128

| style="background: #666666; color: white" | 102, 102, 102

| style="background: #767676; color: white" | 118,{{nnbsp}}118,{{nnbsp}}118

| style="background: #666666; color: white" | 102,{{nnbsp}}102,{{nnbsp}}102

| style="background: #555555; color: white" | 85,{{nnbsp}}85,{{nnbsp}}85

| style="background: #7f7f7f; color: white" | 127,{{nnbsp}}127,{{nnbsp}}127

| style="background: #7f7f7f; color: white" | 127,{{nnbsp}}127,{{nnbsp}}127

| style="background: #808080; color: black" | 128,{{nnbsp}}128,{{nnbsp}}128

| style="background: #000000; color: white" | 0,{{nnbsp}}0,{{nnbsp}}0

{{tt|91}}{{tt|101}}Bright Red

| style="background: #FF5555; color: white" | 255,{{nnbsp}}85,{{nnbsp}}85

| colspan="2" style="background: #ff0000; color: white" | 255,{{nnbsp}}0,{{nnbsp}}0

| style="background: #f14c4c; color: white" | 241, 76, 76

| style="background: #e74856; color: white" | 231,{{nnbsp}}72,{{nnbsp}}86

| style="background: #E60000; color: white" | 230,{{nnbsp}}0,{{nnbsp}}0

| style="background: #ff5555; color: white" | 255,{{nnbsp}}85,{{nnbsp}}85

| colspan="4" style="background: #ff0000; color: white" | 255,{{nnbsp}}0,{{nnbsp}}0

{{tt|92}}{{tt|102}}Bright Green

| style="background: #55FF55; color: black" | 85,{{nnbsp}}255,{{nnbsp}}85

| colspan="2" style="background: #00ff00; color: black" | 0,{{nnbsp}}255,{{nnbsp}}0

| style="background: #23d18b; color: white" | 35, 209, 139

| style="background: #16c60c; color: black" | 22,{{nnbsp}}198,{{nnbsp}}12

| style="background: #00D900; color: black" | 0,{{nnbsp}}217,{{nnbsp}}0

| style="background: #55ff55; color: black" | 85,{{nnbsp}}255,{{nnbsp}}85

| style="background: #00fc00; color: black" | 0,{{nnbsp}}252,{{nnbsp}}0

| colspan="3" style="background: #00ff00; color: black" | 0,{{nnbsp}}255,{{nnbsp}}0

{{tt|93}}{{tt|103}}Bright Yellow

| style="background: #FFFF55; color: black" | 255,{{nnbsp}}255,{{nnbsp}}85

| colspan="2" style="background: #ffff00; color: black" | 255,{{nnbsp}}255,{{nnbsp}}0

| style="background: #f5f543; color: black" | 245, 245, 67

| style="background: #f9f1a5; color: black" | 249,{{nnbsp}}241,{{nnbsp}}165

| style="background: #E5E500; color: black" | 230,{{nnbsp}}230,{{nnbsp}}0

| style="background: #ffff55; color: black" | 255,{{nnbsp}}255,{{nnbsp}}85

| colspan="4" style="background: #ffff00; color: black" | 255,{{nnbsp}}255,{{nnbsp}}0

{{tt|94}}{{tt|104}}Bright Blue

| style="background: #5555FF; color: white" | 85,{{nnbsp}}85,{{nnbsp}}255

| colspan="2" style="background: #0000ff; color: white" | 0,{{nnbsp}}0,{{nnbsp}}255

| style="background: #3b8eea; color: white" | 59, 142, 234

| style="background: #3b78ff; color: white" | 59,{{nnbsp}}120,{{nnbsp}}255

| style="background: #0000FF; color: white" | 0,{{nnbsp}}0,{{nnbsp}}255

| style="background: #5555ff; color: white" | 85,{{nnbsp}}85,{{nnbsp}}255

| style="background: #0000fc; color: white" | 0,{{nnbsp}}0,{{nnbsp}}252

| style="background: #5c5cff; color: white" | 92,{{nnbsp}}92,{{nnbsp}}255Changed from 0,{{nnbsp}}0,{{nnbsp}}255 in July 2004 {{cite web |url=http://invisible-island.net/xterm/xterm.log.html#xterm_192 |title=Patch #192 – 2004/7/12 – XFree86 4.4.99.9}}

| style="background: #0000ff; color: white" | 0,{{nnbsp}}0,{{nnbsp}}255

| style="background: #5c5cff; color: white" | 92,{{nnbsp}}92,{{nnbsp}}255

{{tt|95}}{{tt|105}}Bright Magenta

| style="background: #FF55FF; color: black" | 255,{{nnbsp}}85,{{nnbsp}}255

| colspan="2" style="background: #ff00ff; color: white" | 255,{{nnbsp}}0,{{nnbsp}}255

| style="background: #d670d6; color: white" | 214, 112, 214

| style="background: #b4009e; color: white" | 180,{{nnbsp}}0,{{nnbsp}}158

| style="background: #E600E6; color: white" | 230,{{nnbsp}}0,{{nnbsp}}230

| style="background: #ff55ff; color: white" | 255,{{nnbsp}}85,{{nnbsp}}255

| colspan="4" style="background: #ff00ff; color: white" | 255,{{nnbsp}}0,{{nnbsp}}255

{{tt|96}}{{tt|106}}Bright Cyan

| style="background: #55FFFF; color: black" | 85,{{nnbsp}}255,{{nnbsp}}255

| colspan="2" style="background: #00ffff; color: black" | 0,{{nnbsp}}255,{{nnbsp}}255

| style="background: #29b8dB; color: white" | 41, 184, 219

| style="background: #61d6d6; color: black" | 97,{{nnbsp}}214,{{nnbsp}}214

| style="background: #00E6E6; color: black" | 0,{{nnbsp}}230,{{nnbsp}}230

| style="background: #55ffff; color: black" | 85,{{nnbsp}}255,{{nnbsp}}255

| colspan="4" style="background: #00ffff; color: black" | 0,{{nnbsp}}255,{{nnbsp}}255

{{tt|97}}{{tt|107}}Bright White

| colspan="3" style="background: #ffffff; color: black" | 255,{{nnbsp}}255,{{nnbsp}}255

| style="background: #e5e5e5; color: black" | {{nobr|229, 229, 229}}

| style="background: #f2f2f2; color: black" | 242,{{nnbsp}}242,{{nnbsp}}242

| style="background: #E6E6E6; color: black" | 230,{{nnbsp}}230,{{nnbsp}}230

| colspan="5" style="background: #ffffff; color: black" | 255,{{nnbsp}}255,{{nnbsp}}255

== 8-bit ==

{{seealso|Web colors#Web-safe colors}}

As 256-color lookup tables became common on graphic cards, escape sequences were added to select from a pre-defined set of 256 colors:{{cite web |url=https://invisible-island.net/xterm/xterm.faq.html#color_by_number |title=XTerm FAQ: Can I set a color by its number? |author-first=Thomas E. |author-last=Dickey |accessdate=March 28, 2025}}

ESC[38;5;{{angbr|n}}m Select foreground color where n is a number from the table below

ESC[48;5;{{angbr|n}}m Select background color

0- 7: standard colors (as in ESC [ 30–37 m)

8- 15: high intensity colors (as in ESC [ 90–97 m)

16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)

232-255: grayscale from dark to light in 24 steps

The colors displayed by these values vary across terminal/emulator implementations as the recognized ECMA-48 and ITU's T.416 specifications do not define a specific color palette for this lookup table. While it is common to use the above formula for the color palette, in particular the algorithm and choice of colors for the 16-231 cube values differs between implementations. The color palette and algorithm used by XTerm is specified below as a sample.

The ITU's T.416 Information technology - Open Document Architecture (ODA) and interchange format: Character content architectures{{cite web |url=https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.416-199303-I!!PDF-E&type=items |title=T.416 Information technology - Open Document Architecture (ODA) and interchange format: Character content architectures}} uses ":" as separator characters instead:

ESC[38:5:{{angbr|n}}m Select foreground color where n is a number from the table below

ESC[48:5:{{angbr|n}}m Select background color

{{anchor|256_color_table}}

class="collapsible" style="text-align:center;font-size:80%;width:100%;background:#f6f6f6;cursor:default;" cellpadding="0" cellspacing="1"
colspan="36" | 256-color mode — foreground: ESC[38;5;#m   background: ESC[48;5;#m
colspan="18" | Standard colors

| colspan="18" | High-intensity colors

colspan="36" |

{| style="width:100%;text-align:center;font-weight:bold;"

style="color:#ffffff;background:#000000;" title="#000000" | 0 

| style="color:#ffffff;background:#800000;" title="#800000" | 1 

| style="color:#ffffff;background:#008000;" title="#008000" | 2 

| style="color:#ffffff;background:#808000;" title="#808000" | 3 

| style="color:#ffffff;background:#000080;" title="#000080" | 4 

| style="color:#ffffff;background:#800080;" title="#800080" | 5 

| style="color:#ffffff;background:#008080;" title="#008080" | 6 

| style="color:#ffffff;background:#c0c0c0;" title="#c0c0c0" | 7 

| style="width:1em;" |

| style="color:#000000;background:#808080;" title="#808080" | 8 

| style="color:#000000;background:#ff0000;" title="#ff0000" | 9 

| style="color:#000000;background:#00ff00;" title="#00ff00" |10

| style="color:#000000;background:#ffff00;" title="#ffff00" |11

| style="color:#000000;background:#0000ff;" title="#0000ff" |12

| style="color:#000000;background:#ff00ff;" title="#ff00ff" |13

| style="color:#000000;background:#00ffff;" title="#00ffff" |14

| style="color:#000000;background:#ffffff;" title="#ffffff" |15

|-

| colspan="36" |216 colors

|-

| style="color:#ffffff;background:#000000;" title="#000000" |16

| style="color:#ffffff;background:#00005f;" title="#00005f" |17

| style="color:#ffffff;background:#000087;" title="#000087" |18

| style="color:#ffffff;background:#0000af;" title="#0000af" |19

| style="color:#ffffff;background:#0000d7;" title="#0000d7" |20

| style="color:#ffffff;background:#0000ff;" title="#0000ff" |21

| style="color:#ffffff;background:#005f00;" title="#005f00" |22

| style="color:#ffffff;background:#005f5f;" title="#005f5f" |23

| style="color:#ffffff;background:#005f87;" title="#005f87" |24

| style="color:#ffffff;background:#005faf;" title="#005faf" |25

| style="color:#ffffff;background:#005fd7;" title="#005fd7" |26

| style="color:#ffffff;background:#005fff;" title="#005fff" |27

| style="color:#ffffff;background:#008700;" title="#008700" |28

| style="color:#ffffff;background:#00875f;" title="#00875f" |29

| style="color:#ffffff;background:#008787;" title="#008787" |30

| style="color:#ffffff;background:#0087af;" title="#0087af" |31

| style="color:#ffffff;background:#0087d7;" title="#0087d7" |32

| style="color:#ffffff;background:#0087ff;" title="#0087ff" |33

| style="color:#000000;background:#00af00;" title="#00af00" |34

| style="color:#000000;background:#00af5f;" title="#00af5f" |35

| style="color:#000000;background:#00af87;" title="#00af87" |36

| style="color:#000000;background:#00afaf;" title="#00afaf" |37

| style="color:#000000;background:#00afd7;" title="#00afd7" |38

| style="color:#000000;background:#00afff;" title="#00afff" |39

| style="color:#000000;background:#00d700;" title="#00d700" |40

| style="color:#000000;background:#00d75f;" title="#00d75f" |41

| style="color:#000000;background:#00d787;" title="#00d787" |42

| style="color:#000000;background:#00d7af;" title="#00d7af" |43

| style="color:#000000;background:#00d7d7;" title="#00d7d7" |44

| style="color:#000000;background:#00d7ff;" title="#00d7ff" |45

| style="color:#000000;background:#00ff00;" title="#00ff00" |46

| style="color:#000000;background:#00ff5f;" title="#00ff5f" |47

| style="color:#000000;background:#00ff87;" title="#00ff87" |48

| style="color:#000000;background:#00ffaf;" title="#00ffaf" |49

| style="color:#000000;background:#00ffd7;" title="#00ffd7" |50

| style="color:#000000;background:#00ffff;" title="#00ffff" |51

|-

| style="color:#ffffff;background:#5f0000;" title="#5f0000" |52

| style="color:#ffffff;background:#5f005f;" title="#5f005f" |53

| style="color:#ffffff;background:#5f0087;" title="#5f0087" |54

| style="color:#ffffff;background:#5f00af;" title="#5f00af" |55

| style="color:#ffffff;background:#5f00d7;" title="#5f00d7" |56

| style="color:#ffffff;background:#5f00ff;" title="#5f00ff" |57

| style="color:#ffffff;background:#5f5f00;" title="#5f5f00" |58

| style="color:#ffffff;background:#5f5f5f;" title="#5f5f5f" |59

| style="color:#ffffff;background:#5f5f87;" title="#5f5f87" |60

| style="color:#ffffff;background:#5f5faf;" title="#5f5faf" |61

| style="color:#ffffff;background:#5f5fd7;" title="#5f5fd7" |62

| style="color:#ffffff;background:#5f5fff;" title="#5f5fff" |63

| style="color:#ffffff;background:#5f8700;" title="#5f8700" |64

| style="color:#ffffff;background:#5f875f;" title="#5f875f" |65

| style="color:#ffffff;background:#5f8787;" title="#5f8787" |66

| style="color:#ffffff;background:#5f87af;" title="#5f87af" |67

| style="color:#ffffff;background:#5f87d7;" title="#5f87d7" |68

| style="color:#ffffff;background:#5f87ff;" title="#5f87ff" |69

| style="color:#000000;background:#5faf00;" title="#5faf00" |70

| style="color:#000000;background:#5faf5f;" title="#5faf5f" |71

| style="color:#000000;background:#5faf87;" title="#5faf87" |72

| style="color:#000000;background:#5fafaf;" title="#5fafaf" |73

| style="color:#000000;background:#5fafd7;" title="#5fafd7" |74

| style="color:#000000;background:#5fafff;" title="#5fafff" |75

| style="color:#000000;background:#5fd700;" title="#5fd700" |76

| style="color:#000000;background:#5fd75f;" title="#5fd75f" |77

| style="color:#000000;background:#5fd787;" title="#5fd787" |78

| style="color:#000000;background:#5fd7af;" title="#5fd7af" |79

| style="color:#000000;background:#5fd7d7;" title="#5fd7d7" |80

| style="color:#000000;background:#5fd7ff;" title="#5fd7ff" |81

| style="color:#000000;background:#5fff00;" title="#5fff00" |82

| style="color:#000000;background:#5fff5f;" title="#5fff5f" |83

| style="color:#000000;background:#5fff87;" title="#5fff87" |84

| style="color:#000000;background:#5fffaf;" title="#5fffaf" |85

| style="color:#000000;background:#5fffd7;" title="#5fffd7" |86

| style="color:#000000;background:#5fffff;" title="#5fffff" |87

|-

| style="color:#ffffff;background:#870000;" title="#870000" |88

| style="color:#ffffff;background:#87005f;" title="#87005f" |89

| style="color:#ffffff;background:#870087;" title="#870087" |90

| style="color:#ffffff;background:#8700af;" title="#8700af" |91

| style="color:#ffffff;background:#8700d7;" title="#8700d7" |92

| style="color:#ffffff;background:#8700ff;" title="#8700ff" |93

| style="color:#ffffff;background:#875f00;" title="#875f00" |94

| style="color:#ffffff;background:#875f5f;" title="#875f5f" |95

| style="color:#ffffff;background:#875f87;" title="#875f87" |96

| style="color:#ffffff;background:#875faf;" title="#875faf" |97

| style="color:#ffffff;background:#875fd7;" title="#875fd7" |98

| style="color:#ffffff;background:#875fff;" title="#875fff" |99

| style="color:#ffffff;background:#878700;" title="#878700" |100

| style="color:#ffffff;background:#87875f;" title="#87875f" |101

| style="color:#ffffff;background:#878787;" title="#878787" |102

| style="color:#ffffff;background:#8787af;" title="#8787af" |103

| style="color:#ffffff;background:#8787d7;" title="#8787d7" |104

| style="color:#ffffff;background:#8787ff;" title="#8787ff" |105

| style="color:#000000;background:#87af00;" title="#87af00" |106

| style="color:#000000;background:#87af5f;" title="#87af5f" |107

| style="color:#000000;background:#87af87;" title="#87af87" |108

| style="color:#000000;background:#87afaf;" title="#87afaf" |109

| style="color:#000000;background:#87afd7;" title="#87afd7" |110

| style="color:#000000;background:#87afff;" title="#87afff" |111

| style="color:#000000;background:#87d700;" title="#87d700" |112

| style="color:#000000;background:#87d75f;" title="#87d75f" |113

| style="color:#000000;background:#87d787;" title="#87d787" |114

| style="color:#000000;background:#87d7af;" title="#87d7af" |115

| style="color:#000000;background:#87d7d7;" title="#87d7d7" |116

| style="color:#000000;background:#87d7ff;" title="#87d7ff" |117

| style="color:#000000;background:#87ff00;" title="#87ff00" |118

| style="color:#000000;background:#87ff5f;" title="#87ff5f" |119

| style="color:#000000;background:#87ff87;" title="#87ff87" |120

| style="color:#000000;background:#87ffaf;" title="#87ffaf" |121

| style="color:#000000;background:#87ffd7;" title="#87ffd7" |122

| style="color:#000000;background:#87ffff;" title="#87ffff" |123

|-

| style="color:#ffffff;background:#af0000;" title="#af0000" |124

| style="color:#ffffff;background:#af005f;" title="#af005f" |125

| style="color:#ffffff;background:#af0087;" title="#af0087" |126

| style="color:#ffffff;background:#af00af;" title="#af00af" |127

| style="color:#ffffff;background:#af00d7;" title="#af00d7" |128

| style="color:#ffffff;background:#af00ff;" title="#af00ff" |129

| style="color:#ffffff;background:#af5f00;" title="#af5f00" |130

| style="color:#ffffff;background:#af5f5f;" title="#af5f5f" |131

| style="color:#ffffff;background:#af5f87;" title="#af5f87" |132

| style="color:#ffffff;background:#af5faf;" title="#af5faf" |133

| style="color:#ffffff;background:#af5fd7;" title="#af5fd7" |134

| style="color:#ffffff;background:#af5fff;" title="#af5fff" |135

| style="color:#ffffff;background:#af8700;" title="#af8700" |136

| style="color:#ffffff;background:#af875f;" title="#af875f" |137

| style="color:#ffffff;background:#af8787;" title="#af8787" |138

| style="color:#ffffff;background:#af87af;" title="#af87af" |139

| style="color:#ffffff;background:#af87d7;" title="#af87d7" |140

| style="color:#ffffff;background:#af87ff;" title="#af87ff" |141

| style="color:#000000;background:#afaf00;" title="#afaf00" |142

| style="color:#000000;background:#afaf5f;" title="#afaf5f" |143

| style="color:#000000;background:#afaf87;" title="#afaf87" |144

| style="color:#000000;background:#afafaf;" title="#afafaf" |145

| style="color:#000000;background:#afafd7;" title="#afafd7" |146

| style="color:#000000;background:#afafff;" title="#afafff" |147

| style="color:#000000;background:#afd700;" title="#afd700" |148

| style="color:#000000;background:#afd75f;" title="#afd75f" |149

| style="color:#000000;background:#afd787;" title="#afd787" |150

| style="color:#000000;background:#afd7af;" title="#afd7af" |151

| style="color:#000000;background:#afd7d7;" title="#afd7d7" |152

| style="color:#000000;background:#afd7ff;" title="#afd7ff" |153

| style="color:#000000;background:#afff00;" title="#afff00" |154

| style="color:#000000;background:#afff5f;" title="#afff5f" |155

| style="color:#000000;background:#afff87;" title="#afff87" |156

| style="color:#000000;background:#afffaf;" title="#afffaf" |157

| style="color:#000000;background:#afffd7;" title="#afffd7" |158

| style="color:#000000;background:#afffff;" title="#afffff" |159

|-

| style="color:#ffffff;background:#d70000;" title="#d70000" |160

| style="color:#ffffff;background:#d7005f;" title="#d7005f" |161

| style="color:#ffffff;background:#d70087;" title="#d70087" |162

| style="color:#ffffff;background:#d700af;" title="#d700af" |163

| style="color:#ffffff;background:#d700d7;" title="#d700d7" |164

| style="color:#ffffff;background:#d700ff;" title="#d700ff" |165

| style="color:#ffffff;background:#d75f00;" title="#d75f00" |166

| style="color:#ffffff;background:#d75f5f;" title="#d75f5f" |167

| style="color:#ffffff;background:#d75f87;" title="#d75f87" |168

| style="color:#ffffff;background:#d75faf;" title="#d75faf" |169

| style="color:#ffffff;background:#d75fd7;" title="#d75fd7" |170

| style="color:#ffffff;background:#d75fff;" title="#d75fff" |171

| style="color:#ffffff;background:#d78700;" title="#d78700" |172

| style="color:#ffffff;background:#d7875f;" title="#d7875f" |173

| style="color:#ffffff;background:#d78787;" title="#d78787" |174

| style="color:#ffffff;background:#d787af;" title="#d787af" |175

| style="color:#ffffff;background:#d787d7;" title="#d787d7" |176

| style="color:#ffffff;background:#d787ff;" title="#d787ff" |177

| style="color:#000000;background:#d7af00;" title="#d7af00" |178

| style="color:#000000;background:#d7af5f;" title="#d7af5f" |179

| style="color:#000000;background:#d7af87;" title="#d7af87" |180

| style="color:#000000;background:#d7afaf;" title="#d7afaf" |181

| style="color:#000000;background:#d7afd7;" title="#d7afd7" |182

| style="color:#000000;background:#d7afff;" title="#d7afff" |183

| style="color:#000000;background:#d7d700;" title="#d7d700" |184

| style="color:#000000;background:#d7d75f;" title="#d7d75f" |185

| style="color:#000000;background:#d7d787;" title="#d7d787" |186

| style="color:#000000;background:#d7d7af;" title="#d7d7af" |187

| style="color:#000000;background:#d7d7d7;" title="#d7d7d7" |188

| style="color:#000000;background:#d7d7ff;" title="#d7d7ff" |189

| style="color:#000000;background:#d7ff00;" title="#d7ff00" |190

| style="color:#000000;background:#d7ff5f;" title="#d7ff5f" |191

| style="color:#000000;background:#d7ff87;" title="#d7ff87" |192

| style="color:#000000;background:#d7ffaf;" title="#d7ffaf" |193

| style="color:#000000;background:#d7ffd7;" title="#d7ffd7" |194

| style="color:#000000;background:#d7ffff;" title="#d7ffff" |195

|-

| style="color:#ffffff;background:#ff0000;" title="#ff0000" |196

| style="color:#ffffff;background:#ff005f;" title="#ff005f" |197

| style="color:#ffffff;background:#ff0087;" title="#ff0087" |198

| style="color:#ffffff;background:#ff00af;" title="#ff00af" |199

| style="color:#ffffff;background:#ff00d7;" title="#ff00d7" |200

| style="color:#ffffff;background:#ff00ff;" title="#ff00ff" |201

| style="color:#ffffff;background:#ff5f00;" title="#ff5f00" |202

| style="color:#ffffff;background:#ff5f5f;" title="#ff5f5f" |203

| style="color:#ffffff;background:#ff5f87;" title="#ff5f87" |204

| style="color:#ffffff;background:#ff5faf;" title="#ff5faf" |205

| style="color:#ffffff;background:#ff5fd7;" title="#ff5fd7" |206

| style="color:#ffffff;background:#ff5fff;" title="#ff5fff" |207

| style="color:#ffffff;background:#ff8700;" title="#ff8700" |208

| style="color:#ffffff;background:#ff875f;" title="#ff875f" |209

| style="color:#ffffff;background:#ff8787;" title="#ff8787" |210

| style="color:#ffffff;background:#ff87af;" title="#ff87af" |211

| style="color:#ffffff;background:#ff87d7;" title="#ff87d7" |212

| style="color:#ffffff;background:#ff87ff;" title="#ff87ff" |213

| style="color:#000000;background:#ffaf00;" title="#ffaf00" |214

| style="color:#000000;background:#ffaf5f;" title="#ffaf5f" |215

| style="color:#000000;background:#ffaf87;" title="#ffaf87" |216

| style="color:#000000;background:#ffafaf;" title="#ffafaf" |217

| style="color:#000000;background:#ffafd7;" title="#ffafd7" |218

| style="color:#000000;background:#ffafff;" title="#ffafff" |219

| style="color:#000000;background:#ffd700;" title="#ffd700" |220

| style="color:#000000;background:#ffd75f;" title="#ffd75f" |221

| style="color:#000000;background:#ffd787;" title="#ffd787" |222

| style="color:#000000;background:#ffd7af;" title="#ffd7af" |223

| style="color:#000000;background:#ffd7d7;" title="#ffd7d7" |224

| style="color:#000000;background:#ffd7ff;" title="#ffd7ff" |225

| style="color:#000000;background:#ffff00;" title="#ffff00" |226

| style="color:#000000;background:#ffff5f;" title="#ffff5f" |227

| style="color:#000000;background:#ffff87;" title="#ffff87" |228

| style="color:#000000;background:#ffffaf;" title="#ffffaf" |229

| style="color:#000000;background:#ffffd7;" title="#ffffd7" |230

| style="color:#000000;background:#ffffff;" title="#ffffff" |231

|-

|-

| colspan="36" |Grayscale colors

|-

| colspan="36" |

style="width:100%;text-align:center;font-weight:bold;"
style="color:#ffffff;background:#080808;" title="#080808" |232

| style="color:#ffffff;background:#121212;" title="#121212" |233

| style="color:#ffffff;background:#1c1c1c;" title="#1c1c1c" |234

| style="color:#ffffff;background:#262626;" title="#262626" |235

| style="color:#ffffff;background:#303030;" title="#303030" |236

| style="color:#ffffff;background:#3a3a3a;" title="#3a3a3a" |237

| style="color:#ffffff;background:#444444;" title="#444444" |238

| style="color:#ffffff;background:#4e4e4e;" title="#4e4e4e" |239

| style="color:#ffffff;background:#585858;" title="#585858" |240

| style="color:#ffffff;background:#626262;" title="#626262" |241

| style="color:#ffffff;background:#6c6c6c;" title="#6c6c6c" |242

| style="color:#ffffff;background:#767676;" title="#767676" |243

| style="color:#000000;background:#808080;" title="#808080" |244

| style="color:#000000;background:#8a8a8a;" title="#8a8a8a" |245

| style="color:#000000;background:#949494;" title="#949494" |246

| style="color:#000000;background:#9e9e9e;" title="#9e9e9e" |247

| style="color:#000000;background:#a8a8a8;" title="#a8a8a8" |248

| style="color:#000000;background:#b2b2b2;" title="#b2b2b2" |249

| style="color:#000000;background:#bcbcbc;" title="#bcbcbc" |250

| style="color:#000000;background:#c6c6c6;" title="#c6c6c6" |251

| style="color:#000000;background:#d0d0d0;" title="#d0d0d0" |252

| style="color:#000000;background:#dadada;" title="#dadada" |253

| style="color:#000000;background:#e4e4e4;" title="#e4e4e4" |254

| style="color:#000000;background:#eeeeee;" title="#eeeeee" |255

|}

To calculate the RGB Value of a code in the 6 x 6 x 6 color cube, the following code can be used:

  1. print a list of the 256-color red/green/blue values used by xterm.
  2. reference:
  3. https://github.com/ThomasDickey/ncurses-snapshots/blob/master/test/xterm-16color.dat
  4. https://github.com/ThomasDickey/xterm-snapshots/blob/master/XTerm-col.ad
  5. https://github.com/ThomasDickey/xterm-snapshots/blob/master/256colres.pl

print("colors 0-16 correspond to the ANSI and aixterm naming")

for code in range(0, 16):

if code > 8:

level = 255

elif code == 7:

level = 229

else:

level = 205

r = 127 if code == 8 else level if (code & 1) != 0 else 92 if code == 12 else 0

g = 127 if code == 8 else level if (code & 2) != 0 else 92 if code == 12 else 0

b = 127 if code == 8 else 238 if code == 4 else level if (code & 4) != 0 else 0

print(f"{code:3d}: {r:02X} {g:02X} {b:02X}")

print("colors 16-231 are a 6x6x6 color cube")

for red in range(0, 6):

for green in range(0, 6):

for blue in range(0, 6):

code = 16 + (red * 36) + (green * 6) + blue

r = red * 40 + 55 if red != 0 else 0

g = green * 40 + 55 if green != 0 else 0

b = blue * 40 + 55 if blue != 0 else 0

print(f"{code:3d}: {r:02X} {g:02X} {b:02X}")

print("colors 232-255 are a grayscale ramp, intentionally leaving out black and white")

code = 232

for gray in range(0, 24):

level = gray * 10 + 8

code = 232 + gray

print(f"{code:3d}: {level:02X} {level:02X} {level:02X}")

There has also been a similar but incompatible 88-color encoding using the same escape sequence, seen in {{code|rxvt}} and {{code|xterm-88color}}.{{cite web |url=https://invisible-island.net/ncurses/ncurses.faq.html#xterm_256color |title=Ncurses FAQ: Why not make "xterm" equated to "xterm-256color"? |author-first=Thomas E. |author-last=Dickey |accessdate=March 28, 2025}} It uses a 4×4×4 color cube.

== 24-bit ==

As "true color" graphic cards with 16 to 24 bits of color became common, applications began to support 24-bit colors. Terminal emulators supporting setting 24-bit foreground and background colors with escape sequences include Xterm, KDE's Konsole,{{cite web |url=https://quickgit.kde.org/?p=konsole.git&a=blob&f=tests%2Fcolor-spaces.pl |title=color-spaces.pl (a copy of 256colors2.pl from xterm dated 1999-07-11) |publisher=KDE |date=2006-12-06 }}{{Dead link|date=June 2020 |bot=InternetArchiveBot |fix-attempted=yes }}{{cite web |url=https://invent.kde.org/personalizedrefrigerator/konsole/-/blob/master/doc/user/README.moreColors |title=README.moreColors |publisher=KDE |date=2010-04-22 }} and iTerm, as well as all libvte based terminals,{{cite web |url=https://bugzilla.gnome.org/show_bug.cgi?id=704449 |title=libvte's bug report and patches: Support for 16 million colors |publisher=GNOME Bugzilla |date=2014-04-04 |access-date=2016-06-05}} including GNOME Terminal.{{cite web|url=https://askubuntu.com/questions/512525/how-to-enable-24bit-true-color-support-in-gnome-terminal|title=How to enable 24bit true color support in Gnome terminal?}}

ESC[38;2;{{angbr|r}};{{angbr|g}};{{angbr|b}}m Select RGB foreground color

ESC[48;2;{{angbr|r}};{{angbr|g}};{{angbr|b}}m Select RGB background color

The syntax is likely based on the ITU's T.416 Open Document Architecture (ODA) and interchange format: Character content architectures, which was adopted as ISO/IEC 8613-6 but ended up as a commercial failure.{{cn|date=February 2022}} The ODA version is more elaborate and thus incompatible:

  • The parameters after the '2' (r, g, and b) are optional and can be left empty.
  • Semicolons are replaced by colons, as above.
  • There is a leading "colorspace ID". The definition of the colorspace ID is not included in that specification, so it may be blank to represent the unspecified default. For CMYK color specifications, mintty interprets the colorspace ID parameter as specifying the maximum value which the channel values are given out of (e.g. 100 or 255).
  • In addition to the '2' value after 48 to specify a Red-Green-Blue format (and the '5' above for a 0-255 indexed color), there are alternatives of '0' for implementation-defined and '1' for transparent - neither of which have any further parameters; '3' specifies colors using a Cyan-Magenta-Yellow scheme, and '4' for a Cyan-Magenta-Yellow-Black one, the latter using the position marked as "unused" for the Black component.

ESC[38:2:{{angbr|Color-Space-ID}}:{{angbr|r}}:{{angbr|g}}:{{angbr|b}}:{{angbr|unused}}:{{angbr|CS tolerance}}:{{angbr|Color-Space}}m Select RGB foreground color

ESC[48:2:{{angbr|Color-Space-ID}}:{{angbr|r}}:{{angbr|g}}:{{angbr|b}}:{{angbr|unused}}:{{angbr|CS tolerance}}:{{angbr|Color-Space}}m Select RGB background color

where {{code|Color-Space}} indicates the Color-Space associated with the given tolerance: {{code|0}} for CIELUV or {{code|1}} for CIELAB.

The ITU-RGB variation is supported by xterm, with the colorspace ID and tolerance parameters ignored. The simpler scheme using semicolons is initially found in Konsole.{{rp|at=Can I set a color by its number?}}

== Unix environment variables relating to color support ==

Rather than using the color support in termcap and terminfo introduced in SVr3.2 (1987),{{cite manual |url=https://invisible-island.net/ncurses/man/curs_color.3x.html#h2-HISTORY |section=History |title=curs_color(3X) - manipulate terminal colors with curses }} the S-Lang library (version 0.99-32, June 1996) used a separate environment variable {{code|$COLORTERM}} to indicate whether a terminal emulator could use colors at all, and later added values to indicate if it supported 24-bit color.{{cite web |title=Midnight Commander: lib/tty/color-slang.c|website=Fossies |url=https://fossies.org/linux/mc/lib/tty/color-slang.c}}{{cite web |first=Thomas E. | last=Dickey |title=NCURSES — comments on S-Lang |url=https://invisible-island.net/ncurses/ncurses-slang.html |year=2017 |website=invisible-island.net}} This system, although poorly documented, became widespread enough for Fedora and RHEL to consider using it as a simpler and more universal detection mechanism compared to querying the now-updated libraries.{{cite web |title=Features/256 Color Terminals - Fedora Project Wiki |url=https://fedoraproject.org/wiki/Features/256_Color_Terminals#Scope |website=Fedora Project |archive-url=https://web.archive.org/web/20121004184942/https://fedoraproject.org/wiki/Features/256_Color_Terminals#Scope|archive-date=4 October 2012|url-status=live}}

Some terminal emulators (urxvt, konsole) set {{code|$COLORFGBG}} to report the color scheme of the terminal (mainly light vs. dark background). This behavior originated in S-Lang and is used by vim. Gnome-terminal refuses to add this behavior, as the syntax for the value is not agreed upon, the value cannot be changed upon a runtime change of the palette, and more "proper" xterm OSC 4/10/11 sequences already exist.{{cite web |title=Bug 733423 – Please set COLORFGBG environment variable for automatic color detection |url=https://bugzilla.gnome.org/show_bug.cgi?id=733423 |website=bugzilla.gnome.org}}

Operating System Command sequences<span class="anchor" id="OSC"></span>

Most Operating System Command sequences were defined by Xterm, but many are also supported by other terminal emulators. For historical reasons, Xterm can end the command with Bell character (0x07) as well as the standard {{code|ST}} (0x9C or 0x1B 0x5C). For example, Xterm allows the window title to be set by {{code|ESC ]0;this is the window title BEL}}.

A non-xterm extension is the hyperlink, {{code|ESC ]8;;link ST}} from 2017, used by VTE,{{cite web |last1=Koblinger |first1=Egmont |title=Hyperlinks (a.k.a. HTML-like anchors) in terminal emulators |url=https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda |website=GitHub Gists |language=en}}{{discuss|use of primary source for promotion}} iTerm2, and mintty,{{cite web |title=mintty/mintty: Control Sequences |url=https://github.com/mintty/mintty/wiki/CtrlSeqs#hyperlinks |website=GitHub |language=en}} among others.{{cite web |title=OSC 8 adoption in terminal emulators |url=https://github.com/Alhadis/OSC8-Adoption |last1=Koblinger |first1=Egmont |website=GitHub |language=en}}

The Linux console uses {{code|ESC ] P n rr gg bb}} to change the palette, which, if hard-coded into an application, may hang other terminals.{{cite web|url=https://man7.org/linux/man-pages/man4/console_codes.4.html|title=console_codes — Linux console escape and control sequences|work=Linux Programmer's Manual}} However, appending {{code|ST}} will be ignored by Linux and form a proper, ignorable sequence for other terminals.{{Citation needed|date=January 2017}}

Fs Escape sequences

If the {{code|ESC}} is followed by a byte in the range {{tt|0x60—0x7E}}, the escape sequence is of type {{code|Fs}}. This type is used for control functions individually registered with the ISO-IR registry.{{citation |mode=cs1 |title=ISO-IR: ISO/IEC International Register of Coded Character Sets To Be Used With Escape Sequences |publisher=ITSCJ/IPSJ |url=https://itscj.ipsj.or.jp/english/vbcqpr00000004qn-att/ISO-IR.pdf |access-date=12 May 2023}} A table of these is listed under ISO/IEC 2022.

Fp Escape sequences

If the {{code|ESC}} is followed by a byte in the range {{tt|0x30—0x3F}}, the escape sequence is of type {{code|Fp}}, which is set apart for up to sixteen private-use control functions.{{Rp|6.5.3}}

class="wikitable"

|+ Some type {{tt|Fp}} (private-use) escape sequences recognised by the VT100

AbbrNameEffect
id=DECSC

| {{nobr|{{tt|ESC 7}}}}

| {{tt|DECSC}}

| DEC Save Cursor

| Saves the cursor position, encoding shift state and formatting attributes.{{citation|mode=cs1 |url=https://vt100.net/docs/vt510-rm/DECSC.html |title=DECSC—Save Cursor |work=VT510 Video Terminal Programmer Information |author=Digital |author-link=Digital Equipment Corporation}}

id=DECRC

| {{tt|ESC 8}}

| {{tt|DECRC}}

| {{nobr|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

If the {{code|ESC}} is followed by a byte in the range {{tt|0x20—0x2F}}, the escape sequence is of type {{code|nF}}. Said byte is followed by any number of additional bytes in this range, and then a byte in the range {{tt|0x30-0x7E}}. These escape sequences are further subcategorised by the low two bits of the first byte, e.g. "type {{code|2F}}" for sequences where the first byte is {{tt|0x22}}; and by whether the final byte is in the range {{tt|0x30—0x3F}} indicating private use (e.g. "type {{code|2Fp}}") or not (e.g. "type {{code|2Ft}}").{{Rp|13.2.1}}

Most of the {{code|nFt}} sequences are for changing the current character set, and are listed in ISO/IEC 2022. Some others:

class="wikitable"

|+ Some type {{tt|0Ft}} (announcement) ANSI escape sequences{{Rp|15.2}}

AbbrNameEffect
id=ACS6

| {{tt|ESC SP F}}

| {{plainlist|style=font-family:monospace|

  • ACS6
  • S7C1T}}

| {{plainlist|

  • Announce Code Structure 6
  • {{nowrap|Send 7-bit C1 Control Character to the Host}}}}

| Makes the function keys send ESC + letter instead of 8-bit C1 codes.

id=ACS7

| {{nobr|{{tt|ESC SP G}}}}

| {{plainlist|style=font-family:monospace|

  • ACS7
  • S8C1T}}

| {{plainlist|

  • Announce Code Structure 7
  • {{nowrap|Send 8-bit C1 Control Character to the Host}}}}

| Makes the function keys send 8-bit C1 codes.

If the first byte is '#' the public sequences are reserved for additional ISO-IR registered individual control functions.{{Rp|6.5.2}} No such sequences are presently registered. Type {{code|3Fp}} sequences (which includes ones starting with '#') are available for private-use control functions.{{Rp|6.5.3}}

class="wikitable"

|+ Some type {{tt|3Fp}} (private-use) escape sequences recognised by the VT100

AbbrNameEffect
id=DECDHL

| {{tt|ESC # 3}}

| {{tt|DECDHL}}

| DEC Double-Height Letters, Top Half

| Makes the current line use characters twice as tall. This code is for the top half.{{citation |url=http://ascii-table.com/ansi-escape-sequences-vt-100.php |title=ANSI Escape sequences - VT100 / VT52 |access-date=21 August 2020 |archive-date=27 February 2009 |archive-url=https://web.archive.org/web/20090227051140/http://ascii-table.com/ansi-escape-sequences-vt-100.php |url-status=dead }}

id=DECDHL

| {{tt|ESC # 4}}

| {{tt|DECDHL}}

| {{nobr|DEC Double-Height Letters, Bottom Half}}

| Makes the current line use characters twice as tall. This code is for the bottom half.

id=DECSWL

| {{tt|ESC # 5}}

| {{tt|DECSWL}}

| DEC Single-Width Line

| Makes the current line use single-width characters, per the default behaviour.{{citation|mode=cs1 |url=https://vt100.net/docs/vt510-rm/DECSWL.html |title=DECSWL—Single-Width, Single-Height Line |work=VT510 Video Terminal Programmer Information |author=Digital |author-link=Digital Equipment Corporation}}

id=DECRC

| {{nobr|{{tt|ESC # 6}}}}

| {{tt|DECDWL}}

| {{nowrap|DEC Double-Width Line}}

| Makes the current line use double-width characters, discarding any characters in the second half of the line.{{citation|mode=cs1 |url=https://vt100.net/docs/vt510-rm/DECDWL.html |title=DECDWL—Double-Width, Single-Height Line |work=VT510 Video Terminal Programmer Information |author=Digital |author-link=Digital Equipment Corporation}}

Examples

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

{{code|CSI 32 m}} — This makes text green. The green may be a dark, dull green, so you may wish to enable Bold with the sequence {{code|CSI 1 m}} which would make it bright green, or combined as {{code|CSI 32 ; 1 m}}. Some implementations use the Bold state to make the character Bright.

{{code|CSI 0 ; 6 8 ; "DIR" ; 13 p}} — This reassigns 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 was sometimes used for ANSI bombs. 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.

{{code|CSI s}} — This saves the cursor position. Using the sequence {{code|CSI u}} will restore it to the position. Say the current cursor position is 7(y) and 10(x). The sequence {{code|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 {{code|CSI 20 ; 3 H}} or {{code|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 {{code|ESC 7}} / {{code|ESC 8}} instead which is more widely supported.

= 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 GNU sed command embellishes the output of the make command by displaying lines containing words starting with "WARN" in reverse video and words starting with "ERR" in bright yellow on a dark red background (letter case is ignored). The representations of the codes are highlighted.{{cite web |url=http://www.debian.org/doc/manuals/debian-reference/ch09.en.html#_colorized_shell_echo |title=Chapter 9. System tips|work=debian.org}}

make 2>&1 | sed -e 's/.*\bWARN.*/\x1b[7m&\x1b[0m/i' -e 's/.*\bERR.*/\x1b[93;41m&\x1b[0m/i'

The following Bash function flashes the terminal (by alternately sending reverse and normal video mode codes) until the user presses a key.{{cite web |title=VT100.net: Digital VT100 User Guide |url=http://vt100.net/docs/vt100-ug/chapter3.html |access-date=2015-01-19}}

flasher () { while true; do printf \\e[?5h; sleep 0.1; printf \\e[?5l; read -s -n1 -t1 && break; done; }

This can be used to alert a programmer when a lengthy command terminates, such as with {{code|make ; flasher}}{{thin space}}.{{cite web |title=bash – How to get a notification when my commands are done – Ask Different |url=http://apple.stackexchange.com/questions/9412/how-to-get-a-notification-when-my-commands-are-done |access-date=2015-01-19}}

printf \\033c

This will reset the console, similar to the command {{code|reset}} on modern Linux systems; however it should work even on older Linux systems and on other (non-Linux) UNIX variants.

= In C =

This following program creates a table of numbers from 0 to 109, each of which is displayed in the format specified by the Select Graphic Rendition escape sequence using that number as the graphic rendition code.

File:ANSI sample program output.png]]

  1. include

int main(void)

{

int i, j, n;

for (i = 0; i < 11; i++) {

for (j = 0; j < 10; j++) {

n = 10 * i + j;

if (n > 108) break;

printf("\033[%dm %3d\033[m", n, n);

}

printf("\n");

}

return 0;

}

Terminal input sequences

{{More citations needed|section|date=October 2023}}

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, sent from the terminal to the computer as though the user typed it.

When typing input on a terminal keypresses outside the normal main alphanumeric keyboard area can be sent to the host as ANSI sequences. For keys that have an equivalent output function, such as the cursor keys, these often mirror the output sequences. However, for most keypresses there isn't an equivalent output sequence to use.

There are several encoding schemes, and unfortunately most terminals mix sequences from different schemes, so host software has to be able to deal with input sequences using any scheme.

To complicate the matter, the VT terminals themselves have two schemes of input, normal mode and application mode that can be switched by the application.

(draft section)

-> char

-> esc

-> esc

-> Alt-keypress or keycode sequence

'[' -> Alt-[

'[' () -> keycode sequence, is a decimal

number and defaults to 1 (xterm)

'[' () (';') '~' -> keycode sequence, and

are decimal numbers and default to 1 (vt)

If the terminating character is '~', the first number must be present and is a

keycode number, the second number is an optional modifier value. If the terminating

character is a letter, the letter is the keycode value, and the optional number is

the modifier value.

The modifier value defaults to 1, and after subtracting 1 is a bitmap of modifier

keys being pressed: {{keypress|Meta|Ctrl|Alt|Shift}}. So, for example, <esc>[4;2~ is

{{keypress|Shift|End}}, <esc>[20~ is function key {{keypress|F9}}, <esc>[5C is {{keypress|Ctrl|Right}}.

In other words, the modifier is the sum of the following numbers:

class="wikitable"
Key pressed

! Number

! Comment

| 1

| always added, the rest are optional

Shift

| 1

|

(Left) Alt

| 2

|

Control

| 4

|

Meta

| 8

|

vt sequences:

[1~ - Home [16~ - [31~ - F17

[2~ - Insert [17~ - F6 [32~ - F18

[3~ - Delete [18~ - F7 [33~ - F19

[4~ - End [19~ - F8 [34~ - F20

[5~ - PgUp [20~ - F9 [35~ -

[6~ - PgDn [21~ - F10

[7~ - Home [22~ -

[8~ - End [23~ - F11

[9~ - [24~ - F12

[10~ - F0 [25~ - F13

[11~ - F1 [26~ - F14

[12~ - F2 [27~ -

[13~ - F3 [28~ - F15

[14~ - F4 [29~ - F16

[15~ - F5 [30~ -

xterm sequences:

[A - Up [K - [U -

[B - Down [L - [V -

[C - Right [M - [W -

[D - Left [N - [X -

[E - [O - [Y -

[F - End [1P - F1 [Z -

[G - Keypad 5 [1Q - F2

[H - Home [1R - F3

[I - [1S - F4

[J - [T -

<esc>[A to <esc>[D are the same as the ANSI output sequences. The <modifier> is normally omitted if no modifier keys are pressed, but most implementations always emit the <modifier> for {{keypress|F1|F4|chain=–}}. (draft section)

Xterm has a comprehensive documentation page on the various function-key and mouse input sequence schemes from DEC's VT terminals and various other terminals it emulates.{{cite web |title=XTerm Control Sequences (ctlseqs) |url=https://invisible-island.net/xterm/ctlseqs/ctlseqs.html |first1=Edward |last1=Moy |first2=Stephen |last2=Gildea |first3=Thomas |last3=Dickey |year=2019 |website=Invisible Island}} Thomas Dickey has added a lot of support to it over time;{{cite web |last1=Dickey |first1=Thomas |title=XTerm FAQ: Comparing versions, by counting controls |url=https://invisible-island.net/xterm/xterm.faq.html#compare_versions |website=Invisible Island |access-date=25 January 2020}} he also maintains a list of default keys used by other terminal emulators for comparison.{{cite web |year=2016 |last1=Dickey |first1=Thomas |title=Table of function-keys for XTerm and other Terminal Emulators |url=https://invisible-island.net/xterm/xterm-function-keys.html |website=Invisible Island |access-date=25 January 2020}}

  • 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 generate SS3 1; modifiers char when {{keypress|F1|F4|chain={{sp}}–{{sp}}}} are pressed with modifiers. The faulty behavior was copied from GNOME Terminal.{{Citation needed|date=December 2010}}
  • xterm replies CSI row ; column R if asked for cursor position and CSI 1 ; modifiers R if the {{keypress|F3}} key is pressed with modifiers, which collide in the case of row == 1. This can be avoided by using the ? private modifier as CSI ? 6 n, which will be reflected in the response as CSI ? row ; column R.
  • 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.{{clarify|date=November 2010}}
  • Konsole generates SS3 modifiers char when {{keypress|F1|F4|chain={{sp}}–{{sp}}}} are pressed with modifiers.{{clarify|date=November 2010}}
  • iTerm2 supports reporting additional keys via an enhanced CSI u mode.{{cite web |url=https://iterm2.com/documentation-csiu.html |title=CSI u - Documentation - iTerm2 - macOS Terminal Replacement |author= |website=iTerm2 |access-date=15 Aug 2023}}

See also

Notes

{{Notelist}}

References

{{Reflist|refs=

{{cite web |url=https://www.ecma-international.org/wp-content/uploads/ECMA-48_2nd_edition_august_1979.pdf |title=Standard ECMA-48: Control Functions for Character-Imaging I/O Devices |edition=Second |at=Brief History |date=August 1979 |institution=Ecma International}}

{{cite web |url=https://www.ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf |title=Standard ECMA-48: Control Functions for Coded Character Sets |edition=Fifth |date=June 1991 |institution=Ecma International}}

{{cite web |url=https://www.ecma-international.org/wp-content/uploads/ECMA-35_6th_edition_december_1994.pdf |title=Standard ECMA-35: Character Code Structure and Extension Techniques |edition=Sixth |date=1994 |institution=Ecma International}}

{{cite web |url=https://vt100.net/dec/vt_history |title=Digital's Video Terminals |publisher=VT100.net |author-first=Paul |author-last=Williams |date=2006 |access-date=2011-08-17 }}

}}