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 codes could change the color of text and move the cursor so text could be drawn anywhere on the screen.
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.
ANSI stands for "American National Standards Institute".
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 character, a 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 , 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.<!-- Document at http://www.retroarchive.org/hardware/heathkit/H19CODES.DOC shows that you "Enter ANSI mode" with ESC <, but does not document the result --><!-- H-19 Esc < switches into ANSI mode, requiring a proprietary ANSI extended escape code to return to VT52 (Heathkit) mode. ~~~~ --> 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.
ECMA-48 has been updated several times and is currently at its 5th edition, from 1991. It is also adopted by ISO and IEC as standard ISO/IEC 6429. 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
thumb|The Xterm terminal emulator
In 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 significant amounts of Unix software, as it was easier to use than the termcap libraries.
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 and as an early PDF-like document storage format, the Open Document Architecture.
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.<!-- INT 10h/AH=0Eh --> 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 . 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 and Maximus5's ConEmu enabled ANSI escape sequences. Software such as the Python colorama package 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. 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 . Windows Terminal, introduced in 2019, supports the sequences by default. Since Windows 11 22H2 and Windows Terminal 1.15, Windows Terminal replaces Windows Console as the default.
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)
|-
! ^ !!C0!!Abbr!!C escape sequence!!Name!!Effect
|- id=BEL
| || 0x07 || || <code>\a</code> || Bell || Makes an audible noise.
|- id=BS
| || 0x08 || || <code>\b</code> || Backspace || Moves the cursor left (but may "backwards wrap" if cursor is at start of line).
|- id=HT
| || 0x09 || || <code>\t</code> || Tab || Moves the cursor right to next tab stop.
|- id=LF
| || 0x0A || || <code>\n</code> || Line Feed || Moves to next line, scrolls the display up if at bottom of the screen. Usually does not move horizontally, though programs should not rely on this.
|- id=FF
| || 0x0C || || <code>\f</code> || Form Feed || Move a printer to top of next page. Usually does not move horizontally, though programs should not rely on this. Effect on video terminals varies.
|- id=CR
| || 0x0D || || <code>\r</code> || || Moves the cursor to column zero.
|- id=ESC
| || 0x1B || || <code>\x1B</code>, <code>\033</code> || Escape || Starts 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).
|- id=PM
|
|
|
| Privacy Message
|- id=APC
|
|
|
|
|}
<!-- Parked a number of anchors here which are used in redirects to this page but are not yet discussed inhere. Move them a better place when the corresponding info gets incorporated here at a later stage -->
Control Sequence Introducer commands
For Control Sequence Introducer, or CSI, commands, the (written as , or in several programming languages) is followed by any number (including none) of "parameter bytes" in the range 0x30–0x3F (ASCII <code>0–9:;<=>?</code>), then by any number of "intermediate bytes" in the range 0x20–0x2F (ASCII space and ), then finally by a single "final byte" in the range 0x40–0x7E (ASCII <code>@A–Z[\]^_`a–z{|}~</code><!--Note: can't use due to the | in the content.-->). In vertical split screen mode, instead used to set (as <code>CSI <var>n</var> ; <var>n</var> s</code>) or reset left and right margins.
|- id=RCP
| style="font-family:monospace;" | CSI u
|
|
| Restores the cursor position/state in SCO console mode.
|-
| style="font-family:monospace;" | CSI ? 25 h
|
|
| Shows the cursor, from the VT220.
|-
| style="font-family:monospace;" | CSI ? 25 l
|
|
| Hides the cursor.
|-
| style="font-family:monospace;" | CSI ? 1004 h
|
|
| Enable reporting focus. Reports whenever terminal emulator enters or exits focus as and , 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. In bracketed paste mode, text pasted into the terminal will be surrounded by and ; programs running in the terminal should not treat characters bracketed by those sequences as commands (Vim, for example, does not treat them as commands). From xterm
|-
| style="font-family:monospace;" | CSI ? 2004 l
|
|
| Turn off bracketed paste mode.
|}
<span class="anchor" id="SGR"></span><span class="anchor" id="SGR parameters"></span> Select Graphic Rendition parameters
The control sequence <code>CSI m</code>, named Select Graphic Rendition (SGR), sets display attributes. Several attributes can be set in the same sequence, separated by semicolons. Each display attribute remains in effect until a following occurrence of SGR resets it.
|-
|
| Faint, decreased intensity, or dim
|<!-- dim (with a saturated color). --> May be implemented as a light font weight like bold.
|-
|
| Italic
| Not widely supported. Sometimes treated as inverse or blink.
|-
|
| Slow blink
| Sets blinking to less than 150 times per minute
|-
|
| Rapid blink
| MS-DOS ANSI.SYS, 150+ per minute; not widely supported
|-
|
| Reverse video or invert
| Swap foreground and background colors.
|-
|
| Conceal or hide
| Not widely supported.
|-
|
| Crossed-out, or strike
| Characters legible but marked as if for deletion. Not supported in Terminal.app.
|-
|
| Primary (default) font
|
|-
|
| Alternative font
| Select alternative font
|-
|
| Fraktur (Gothic)
| Rarely supported
|-
|
| Doubly underlined; or: not bold
| Double-underline per ECMA-48,
|-
|
| Normal intensity
| Neither bold nor faint; color changes where intensity is implemented as such.
|-
|
| Neither italic, nor blackletter
|
|-
|
| Not underlined
| Neither singly nor doubly underlined
|-
|
| Not blinking
|Turn blinking off
|-
|
| Proportional spacing
| ITU T.61 and T.416, not known to be used on terminals
|-
|
| Not reversed
|
|-
|
| Reveal
| Not concealed
|-
|
| Not crossed out
|
|-
|
| Set foreground color
|
|-
|
| Set foreground color
| Next arguments are or
|-
|
| Default foreground color
| Implementation defined (according to standard)
|-
|
| Set background color
|
|-
|
| Set background color
| Next arguments are or
|-
|
| Default background color
| Implementation defined (according to standard)
|-
|
| Disable proportional spacing
| T.61 and T.416
|-
|
| Framed
| rowspan=2| Implemented as "emoji variation selector" in mintty.
|-
|
| Encircled
|-
|
| Overlined
| Not supported in Terminal.app
|-
|
| Neither framed nor encircled
|
|-
|
| Not overlined
|
|-
|
| Set underline color
| Not in standard; implemented in Kitty, VTE, mintty, and iTerm2. PowerShell 7 is unaffected.
! Visual Studio Code
! Windows 10<br/>Console
! Terminal.app
! PuTTY
! mIRC
! xterm
! Ubuntu
!Eclipse Terminal
|-
| || || Black
| colspan="4" style="background: #000; color: white" | 0,0,0
| style="background: #0c0c0c; color: white" | 12,12,12
| colspan="4" style="background: #000; color: white" | 0,0,0
| style="background: #010101; color: white" | 1,1,1
| style="background: #000; color: white" | 0,0,0
|-
| || || Red
| style="background: #C40000; color: white" | 196,0,0
| colspan="2" style="background: #800000; color: white" | 128,0,0
| style="background: #cd3131; color: white" | 205, 49, 49
| style="background: #c50f1f; color: white" | 197,15,31
| style="background: #990000; color: white" | 153,0,0
| style="background: #bb0000; color: white" | 187,0,0
| style="background: #7f0000; color: white" | 127,0,0
| style="background: #cd0000; color: white" | 205,0,0
| style="background: #de382b; color: white" | 222,56,43
| style="background: #cd0000; color: white" | 205,0,0
|-
| || || Green
| style="background: #00C400; color: white" | 0,196,0
| colspan="2" style="background: #008000; color: white" | 0,128,0
| style="background: #0dbc79; color: white" | 13, 188, 121
| style="background: #13a10e; color: white" | 19,161,14
| style="background: #00A600; color: white" | 0,166,0
| style="background: #00bb00; color: white" | 0,187,0
| style="background: #009300; color: white" | 0,147,0
| style="background: #00cd00; color: white" | 0,205,0
| style="background: #39b54a; color: black" | 57,181,74
| style="background: #00cd00; color: white" | 0,205,0
|-
| || || Yellow
| style="background: #C47E00; color: white" | 196,126,0
| style="background: #808000; color: white" | 128,128,0
| style="background: #eeedf0; color: black" | 238,237,240
| style="background: #e5e510; color: black" | 229, 229, 16
| style="background: #c19c00; color: white" | 193,156,0
| style="background: #999900; color: white" | 153,153,0
| style="background: #bbbb00; color: black" | 187,187,0
| style="background: #fc7f00; color: white" | 252,127,0
| style="background: #cdcd00; color: black" | 205,205,0
| style="background: #ffc706; color: black" | 255,199,6
| style="background: #cdcd00; color: black" | 205,205,0
|-
| || || Blue
| style="background: #0000C4; color: white" | 0,0,196
| colspan="2" style="background: #000080; color: white" | 0,0,128
| style="background: #2472c8; color: white" | 36, 114, 200
| style="background: #0037da; color: white" | 0,55,218
| style="background: #0000B2; color: white" | 0,0,178
| style="background: #0000bb; color: white" | 0,0,187
| style="background: #00007f; color: white" | 0,0,127
| style="background: #0000ee; color: white" | 0,0,238
| style="background: #006fb8; color: white" | 0,111,184
| style="background: #0000ee; color: white" | 0,0,238
|-
| || || Magenta
| style="background: #C400C4; color: white" | 196,0,196
| style="background: #800080; color: white" | 128,0,128
| style="background: #012456; color: white" | 1,36,86
| style="background: #bc3fbc; color: white" | 188, 63, 188
| style="background: #881798; color: white" | 136,23,152
| style="background: #B200B2; color: white" | 178,0,178
| style="background: #bb00bb; color: white" | 187,0,187
| style="background: #9c009c; color: white" | 156,0,156
| style="background: #cd00cd; color: white" | 205,0,205
| style="background: #762671; color: white" | 118,38,113
| style="background: #cd00cd; color: white" | 205,0,205
|-
| || || Cyan
| style="background: #00C4C4; color: white" | 0,196,196
| colspan="2" style="background: #008080; color: white" | 0,128,128
| style="background: #11a8cd; color: white" | 17, 168, 205
| style="background: #3a96dd; color: white" | 58,150,221
| style="background: #00A6B2; color: white" | 0,166,178
| style="background: #00bbbb; color: black" | 0,187,187
| style="background: #009393; color: white" | 0,147,147
| style="background: #00cdcd; color: black" | 0,205,205
| style="background: #2cb5e9; color: black" | 44,181,233
| style="background: #00cdcd; color: black" | 0,205,205
|-
| || || White
| style="background: #C4C4C4; color: black" | 196,196,196
| colspan="2" style="background: #c0c0c0; color: black" | 192,192,192
| style="background: #e5e5e5; color: black" | 229, 229, 229
| style="background: #cccccc; color: black" | 204,204,204
| style="background: #BFBFBF; color: black" | 191,191,191
| style="background: #bbbbbb; color: black" | 187,187,187
| style="background: #d2d2d2; color: black" | 210,210,210
| style="background: #e5e5e5; color: black" | 229,229,229
| style="background: #cccccc; color: black" | 204,204,204
| style="background: #e5e5e5; color: black" | 229,229,229
|-
| || ||
| style="background: #4E4E4E; color: white" | 78,78,78
| colspan="2" style="background: #808080; color: black" | 128,128,128
| style="background: #666666; color: white" | 102, 102, 102
| style="background: #767676; color: white" | 118,118,118
| style="background: #666666; color: white" | 102,102,102
| style="background: #555555; color: white" | 85,85,85
| style="background: #7f7f7f; color: white" | 127,127,127
| style="background: #7f7f7f; color: white" | 127,127,127
| style="background: #808080; color: black" | 128,128,128
| style="background: #000000; color: white" | 0,0,0
|-
| || || Bright Red
| style="background: #DC4E4E; color: white" | 220,78,78
| colspan="2" style="background: #ff0000; color: white" | 255,0,0
| style="background: #f14c4c; color: white" | 241, 76, 76
| style="background: #e74856; color: white" | 231,72,86
| style="background: #E60000; color: white" | 230,0,0
| style="background: #ff5555; color: white" | 255,85,85
| colspan="4" style="background: #ff0000; color: white" | 255,0,0
|-
| || || Bright Green
| style="background: #4EDC4E; color: black" | 78,220,78
| colspan="2" style="background: #00ff00; color: black" | 0,255,0
| style="background: #23d18b; color: white" | 35, 209, 139
| style="background: #16c60c; color: black" | 22,198,12
| style="background: #00D900; color: black" | 0,217,0
| style="background: #55ff55; color: black" | 85,255,85
| style="background: #00fc00; color: black" | 0,252,0
| colspan="3" style="background: #00ff00; color: black" | 0,255,0
|-
| || || Bright Yellow
| style="background: #F3F34E; color: black" | 243,243,78
| colspan="2" style="background: #ffff00; color: black" | 255,255,0
| style="background: #f5f543; color: black" | 245, 245, 67
| style="background: #f9f1a5; color: black" | 249,241,165
| style="background: #E5E500; color: black" | 230,230,0
| style="background: #ffff55; color: black" | 255,255,85
| colspan="4" style="background: #ffff00; color: black" | 255,255,0
|-
| || || Bright Blue
| style="background: #4E4EDC; color: white" | 78,78,220
| colspan="2" style="background: #0000ff; color: white" | 0,0,255
| style="background: #3b8eea; color: white" | 59, 142, 234
| style="background: #3b78ff; color: white" | 59,120,255
| style="background: #0000FF; color: white" | 0,0,255
| style="background: #5555ff; color: white" | 85,85,255
| style="background: #0000fc; color: white" | 0,0,252
| style="background: #5c5cff; color: white" | 92,92,255
| style="background: #0000ff; color: white" | 0,0,255
| style="background: #5c5cff; color: white" | 92,92,255
|-
| || || Bright Magenta
| style="background: #F34EF3; color: black" | 243,78,243
| colspan="2" style="background: #ff00ff; color: white" | 255,0,255
| style="background: #d670d6; color: white" | 214, 112, 214
| style="background: #b4009e; color: white" | 180,0,158
| style="background: #E600E6; color: white" | 230,0,230
| style="background: #ff55ff; color: white" | 255,85,255
| colspan="4" style="background: #ff00ff; color: white" | 255,0,255
|-
| || || Bright Cyan
| style="background: #4EF3F3; color: black" | 78,243,243
| colspan="2" style="background: #00ffff; color: black" | 0,255,255
| style="background: #29b8dB; color: white" | 41, 184, 219
| style="background: #61d6d6; color: black" | 97,214,214
| style="background: #00E6E6; color: black" | 0,230,230
| style="background: #55ffff; color: black" | 85,255,255
| colspan="4" style="background: #00ffff; color: black" | 0,255,255
|-
| || || Bright White
| colspan="3" style="background: #FFFFFF; color: black" | 255,255,255
| style="background: #e5e5e5; color: black" |
| style="background: #f2f2f2; color: black" | 242,242,242
| style="background: #E6E6E6; color: black" | 230,230,230
| colspan="5" style="background: #ffffff; color: black" | 255,255,255
|}
</div>
8-bit
As 256-color lookup tables became common on graphic cards, escape sequences were added to select from a pre-defined set of 256 colors:
ESC[38;5;m Select foreground color where n is a number from the table below
ESC[48;5;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. uses ":" as separator characters instead:
ESC[38:5:m Select foreground color where n is a number from the table below
ESC[48:5:m Select background color<!--
def fg(triad): return 'ffffff' if (triad[2:4] < 'a0') else '000000'
standards = ('000000 800000 008000 808000 000080 800080 008080 c0c0c0 ' +
'808080 ff0000 00ff00 ffff00 0000ff ff00ff 00ffff ffffff').split(' ')
rgbs = '00 5f 87 af d7 ff'.split(' ')
greys = '08 12 1c 26 30 3a 44 4e 58 62 6c 76 80 8a 94 9e a8 b2 bc c6 d0 da e4 ee'.split(' ')
fgs = '000000 ffffff'.split(' ')
code = 0
outs = [[] for i in range(3)]
out = '\n| style="color:#%s;background:#%s;" title="#%s" |%s'
for i in range(len(standards)):
if (i == len(standards) / 2.0): outs[0].append('\n| style="width:1em;"| ')
triad = standards[i]
outs[0].append(out % (fgs[i < len(standards) / 2], triad, triad,
code if (code > 9) else ' %d ' % code))
code += 1
for r in range(len(rgbs)):
for g in range(len(rgbs)):
for b in range(len(rgbs)):
triad = rgbs[r] + rgbs[g] + rgbs[b]
outs[1].append(out % (fgs[g < len(rgbs) / 2], triad, triad, code))
code += 1
outs[1].append('\n|-\n')
for i in range(len(greys)):
triad = greys[i] * 3
outs[2].append(out % (fgs[i < len(greys) / 2], triad, triad, code))
code += 1
file_out = open(__file__[:__file__.rfind('.')] + '.htm', 'w')
file_out.write(\
{| 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;"
|-
%s
|-
|}
|-
| colspan="36" | 216 colors
|-
%s
|-
| colspan="36" | Grayscale colors
|-
| colspan="36" |
{| style="width:100%%;text-align:center;font-weight:bold;"
|-
%s
|-
|}
|} %
(.join(outs[0]), .join(outs[1]), .join(outs[2])))
file_out.close()
-->
{| class="collapsible" style="text-align:center;font-size:80%;width:100%;background:#f6f6f6;cursor:default;" cellpadding="0" cellspacing="1"
|-
! colspan="36" | 256-color mode
|- style="font-style=italic"
| colspan="36" | Foreground: ESC[38;5;#m — Background: ESC[48;5;#m
|-
| colspan="36" |
{| style="width:100%;text-align:center;font-weight:bold;"
|- style="font-weight:normal;height:2.5em;vertical-align:bottom;"
| colspan="8" | Standard colors
| style="width:1em;" |
| colspan="8" | High-intensity colors
|-
| 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
|}
|- style="height:2.5em;vertical-align:bottom"
| 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" |
{| style="width:100%;text-align:center;font-weight:bold;"
|- style="font-weight:normal;height:2.5em;vertical-align:bottom"
| colspan="24" | Grayscale colors
|-
| 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 values of the colors in the table above, the following Python script can be used:
<syntaxhighlight lang="python" line>
- print a list of the 256-color red/green/blue values used by xterm.
- reference:
- https://github.com/ThomasDickey/ncurses-snapshots/blob/master/test/xterm-16color.dat
- https://github.com/ThomasDickey/xterm-snapshots/blob/master/XTerm-col.ad
- https://github.com/ThomasDickey/xterm-snapshots/blob/master/256colres.pl
print("colors 0-15 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")
for gray in range(0, 24):
level = gray * 10 + 8
code = 232 + gray
print(f"{code:3d}: {level:02X} {level:02X} {level:02X}")
</syntaxhighlight>
There has also been a similar but incompatible 88-color encoding using the same escape sequence, seen in and . 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, and iTerm, as well as all libvte based terminals, including GNOME Terminal.
ESC[38;2;;;m Select RGB foreground color
ESC[48;2;;;m Select RGB background color
This syntax, initially implemented in XTerm, is based on a reading of the ISO/IEC 8613-6 specification, specifically that SGR 38 / SGR 48 followed by the parameter "2" can specify a "direct color" in RGB space.. As the first widely-used implementation, this specification of RGB values using semicolon separators is widely supported by terminal emulators that include 24-bit color support.
As with the 8-bit color codes, there is a very similar specification of 24-bit color in the ITU's T.416 Open Document Architecture (ODA) and interchange format: Character content architectures,
Unix environment variables relating to color support
Rather than using the color support in termcap and terminfo introduced in SVr3.2 (1987), the S-Lang library (version 0.99-32, June 1996) used a separate environment variable to indicate whether a terminal emulator could use colors at all, and later added values to indicate if it supported 24-bit color. 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.
Some terminal emulators (urxvt, Konsole) set to report the color scheme of the terminal (mainly light vs. dark background). This behavior originated in S-Lang
It has become conventional to use the environment variable to disable colors unconditionally.
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 (0x9C or 0x1B 0x5C). iTerm2, among others.
The Linux console uses to change the palette, which, if hard-coded into an application, may hang other terminals. However, appending will be ignored by Linux and form a proper, ignorable sequence for other terminals.
Fs Escape sequences
If the is followed by a byte in the range , the escape sequence is of type . This type is used for control functions individually registered with the ISO-IR registry. A table of these is listed under ISO/IEC 2022.
Fp Escape sequences
If the is followed by a byte in the range , the escape sequence is of type , which is set apart for up to sixteen private-use control functions.
|- id=DECDHL
|
|
|
| Makes the current line use characters twice as tall. This code is for the bottom half.
<span style="white-space:pre-wrap;">make 2>&1 | sed -e 's/.*\bWARN.*/<span style="border:1px solid grey;border-radius:5px;color:white;background:black;font-weight:bolder;">\x1b[7m</span>&<span style="border:1px solid grey;border-radius:5px;">\x1b[0m</span>/i' -e 's/.*\bERR.*/<span style="border:1px solid grey;border-radius:5px;color:yellow;background:maroon;font-weight:bolder;">\x1b[93;41m</span>&<span style="border:1px solid grey;border-radius:5px;">\x1b[0m</span>/i'</span>
The following Bash function flashes the terminal (by alternately sending reverse and normal video mode codes) until the user presses a key.
It can be used to alert a programmer when a lengthy command terminates, such as with .
<span style="border:1px solid grey;border-radius:5px;color:white;background:black;font-weight:bolder;">'\e[?5h'</span> <span style="border:1px solid grey;border-radius:5px;font-weight:bolder;">'\e[?5l'</span>
The following command will reset the console, similar to the command 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.
right|thumb|Output of example program on [[Gnome Terminal]]<syntaxhighlight lang="c" line="1">
- include <stdio.h>
int main(void)
{
int row, col, n;
for (row = 0; row < 11; row++) {
for (col = 0; col < 10; col++) {
n = 10 * row + col;
if (n > 109) break;
printf("\033[%dm %3d\033[m", n, n);
}
printf("\n");
}
return 0;
}
</syntaxhighlight>
Terminal input sequences
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)
<pre>
<char> -> char
<esc> -> esc
<esc> <esc> -> Alt-esc
<esc> <char> -> Alt-keypress or keycode sequence
<esc> '[' -> Alt-[
<esc> '[' (<modifier>) <char> -> keycode sequence, <modifier> is a decimal
number and defaults to 1 (xterm)
<esc> '[' (<keycode>) (';'<modifier>) '~' -> keycode sequence, <keycode> and <modifier>
are decimal numbers and default to 1 (vt)
</pre>
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: . So, for example, <code><esc>[4;2~</code> is
, <code><esc>[20~</code> is function key , <code><esc>[5C</code> is .
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
|
|}
<pre>
vt sequences:
<esc>[1~ - Home <esc>[16~ - <esc>[31~ - F17
<esc>[2~ - Insert <esc>[17~ - F6 <esc>[32~ - F18
<esc>[3~ - Delete <esc>[18~ - F7 <esc>[33~ - F19
<esc>[4~ - End <esc>[19~ - F8 <esc>[34~ - F20
<esc>[5~ - PgUp <esc>[20~ - F9 <esc>[35~ -
<esc>[6~ - PgDn <esc>[21~ - F10
<esc>[7~ - Home <esc>[22~ -
<esc>[8~ - End <esc>[23~ - F11
<esc>[9~ - <esc>[24~ - F12
<esc>[10~ - F0 <esc>[25~ - F13
<esc>[11~ - F1 <esc>[26~ - F14
<esc>[12~ - F2 <esc>[27~ -
<esc>[13~ - F3 <esc>[28~ - F15
<esc>[14~ - F4 <esc>[29~ - F16
<esc>[15~ - F5 <esc>[30~ -
xterm sequences:
<esc>[A - Up <esc>[K - <esc>[U -
<esc>[B - Down <esc>[L - <esc>[V -
<esc>[C - Right <esc>[M - <esc>[W -
<esc>[D - Left <esc>[N - <esc>[X -
<esc>[E - <esc>[O - <esc>[Y -
<esc>[F - End <esc>OP - F1 <esc>[Z -
<esc>[G - Keypad 5 <esc>OQ - F2
<esc>[H - Home <esc>OR - F3
<esc>[I - <esc>OS - F4
<esc>[J - <esc>[T -
</pre>
<code><esc>[A</code> to <code><esc>[D</code> are the same as the ANSI output sequences. The <code><modifier></code> is normally omitted if no modifier keys are pressed, but most implementations always emit the <code><modifier></code> for . (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. Thomas Dickey has added a lot of support to it over time; he also maintains a list of default keys used by other terminal emulators for comparison.
- On the Linux console, certain function keys generate sequences of the form <code>CSI [ char</code>. The CSI sequence should terminate on the <code>[</code>.
- Old versions of Terminator generate <code>SS3 1; modifiers char</code> when are pressed with modifiers. The faulty behavior was copied from GNOME Terminal.
- xterm replies <code>CSI row ; column R</code> if asked for cursor position and <code>CSI 1 ; modifiers R</code> if the key is pressed with modifiers, which collide in the case of <code>row == 1</code>. This can be avoided by using the ? private modifier as <code>CSI ? 6 n</code>, which will be reflected in the response as <code>CSI ? row ; column R</code>.
- many terminals prepend <code>ESC</code> to any character that is typed with the alt key down. This creates ambiguity for uppercase letters and symbols <code>@[\]^_</code>, which would form C1 codes.
- Konsole generates <code>SS3 modifiers char</code> when are pressed with modifiers.
- Some terminals, including iTerm2 and kitty, support reporting additional keys via an enhanced CSI u mode.
See also
- ANSI art
- Control character
- ISO/IEC JTC 1/SC 2
- C0 and C1 control codes
Notes
References
External links
- Standard ECMA-48, Control Functions For Coded Character Sets. (5th edition, June 1991), European Computer Manufacturers Association, Geneva 1991 (also published by ISO and IEC as standard ISO/IEC 6429)
- vt100.net DEC Documents
- Xterm / Escape Sequences
- AIXterm / Escape Sequences
- A collection of escape sequences for terminals that are vaguely compliant with ECMA-48 and friends.
- ITU-T Rec. T.416 (03/93) Information technology – Open Document Architecture (ODA) and interchange format: Character content architectures
