ANSI转义序列是带信号的标准编码,用于控制视频文本终端和终端模拟器上的光标位置、颜色、字体样式和其他选项。这些字节序列(大多数以ASCII转义字符和括号字符开头)会嵌入到文本中。终端将这些序列解释为命令,而不是逐字显示的文本(这就导致在终端交互时看不到这些,但采用一些收集脚本收集这些终端的命令反馈时,收集回来的数据中会看到大量这些编码)。
控制字符
C0
Hex | 字符 | 描述 |
\x00 | \0 | Null |
\x05 | ENQ 触发应答消息 | |
\x07 | \a | Bell 响铃,当你在terminal中做某些无效操作的时候,返回\x07 |
\x08 | \b | BackSpace 退格键(发送退格键,if 在行首返回\x07 else 返回\x08\x1b[K,具体含义见后续) |
\x09 | \t | Horizontal Tab 横向跳至下一个制表位,若后面没有制表位,则跳至行尾 |
\x0a | \n | Line Feed 光标下移一行(终端中表现为换行) |
\x0b | \v | Vertical Tab 竖向跳至下一个制表位(终端中表现为换行) |
\x0c | \f | Form Feed 光标跳转至下一页头部(终端中表现为换行) |
\x0d | \r | Carriage Return 回车换行 |
\x0e | SO 激活 G1 字符集 | |
\x0f | SI 激活 G0 字符集 | |
\0x11 | \q | DC1 继续传送 |
\0x13 | \s | DC3 忽略除XOFF 和 XON 以外的一切字符 |
\x18 | CAN 实质 Ctrl+X 中断转义序列/进程 | |
\x1a | \z | SUB(EOF) 实质 Ctrl+Z 中断转义序列/进程 |
\x1b | \e | Escape 开始一个新的转义序列 |
\x7f | DEL 忽略 |
缩写/字符 | OCT(八进制) | DEC(十进制) | HEX(十六进制) | 转义表示 | 解释 |
NUL(null) | 00 | 0 | 0x00 | 空字符 | |
SOH(start of headline) | 01 | 1 | 0x01 | 标题开始 | |
STX(start of text) | 02 | 2 | 0x02 | 正文开始 | |
ETX(end of text) | 03 | 3 | 0x03 | 正文结束 | |
EOT(end of transmission) | 04 | 4 | 0x04 | 传输结束 | |
ENQ(enquiry) | 05 | 5 | 0x05 | 请求 | |
ACK(acknowledge) | 06 | 6 | 0x06 | 收到通知 | |
BEL(bell) | 07 | 7 | 0x07 | \a | 响铃 |
BS(backspace) | 010 | 8 | 0x08 | \b | 退格 |
HT(horizontal tab) | 011 | 9 | 0x09 | \t | 水平制表符 |
LF(NL line feed,new line) | 012 | 10 | 0x0A | \n | 换行建 |
VT(vertical tab) | 013 | 11 | 0x0B | \v | 垂直制表符 |
FF(NP form feed,new page) | 014 | 12 | 0x0C | \f | 换页键 |
CR(carriage return) | 015 | 13 | 0x0D | \r | 回车键 |
SO(shift out) | 016 | 14 | 0x0E | 不用切换 | |
SI(shift in) | 017 | 15 | 0x0F | 启用切换 | |
DLE(data link escape) | 020 | 16 | 0x10 | 数据链路转义 | |
DC1(device control 1) | 021 | 17 | 0x11 | 设备控制1 | |
DC2(device control 2) | 022 | 18 | 0x12 | 设备控制2 | |
DC3(device control 3) | 023 | 19 | 0x13 | 设备控制3 | |
DC4(device control 4) | 024 | 20 | 0x14 | 设备控制4 | |
NAK(negative acknowledge) | 025 | 21 | 0x15 | 拒绝接收 | |
SYN(synchronous idle) | 026 | 22 | 0x16 | 同步空闲 | |
ETB(end of trans. Block) | 027 | 23 | 0x17 | 结束传输块 | |
CAN(cancel) | 030 | 24 | 0x18 | 取消 | |
EM(end of medium) | 031 | 25 | 0x19 | 媒介结束 | |
SUB(substitute) | 032 | 26 | 0x1A | 代替 | |
ESC(escape) | 033 | 27 | 0x1B | \e | 换码(溢出) |
FS(file separator) | 034 | 28 | 0x1C | 文件分隔符 | |
GS(group separator) | 035 | 29 | 0x1D | 分组符 | |
RS(record separator) | 036 | 30 | 0x1E | 记录分隔符 | |
US(unit separator) | 037 | 31 | 0x1F | 单元分隔符 | |
DEL(delete) | 0177 | 127 | 0x7f | 删除 |
C1
C1 | Abbr | Name | Effect |
ESC N | 0x8E | SS2 | Single Shift Two |
ESC O | 0x8F | SS3 | Single Shift Three |
ESC P | 0x90 | DCS | Device Control String |
ESC [ | 0x9B | CSI | Control Sequence Introducer |
ESC \ | 0x9C | ST | String Terminator |
ESC ] | 0x9D | OSC | Operating System Command |
ESC X | 0x98 | SOS | Start of String |
ESC ^ | 0x9E | PM | Privacy Message |
ESC _ | 0x9F | APC | Application Program Command |
CSI (Control Sequence Introducer) sequences
ANSI转义序列中以 ESC [ 开头的叫作 Control Sequence Introducer,简写为 CSI。以 CSI 开头的指令有很多,大致可分四类:光标移动指令、清屏指令、字符渲染(Graphic Rendition)指令和终端控制指令。
CSI序列由ESC [、若干个(包括0个)“参数字节”、若干个“中间字节”,以及一个“最终字节”组成。各部分的字符范围如下:
组成部分 | 字符范围 | ASCII |
参数字节 | 0x30–0x3F | 0–9:;<=>? |
中间字节 | 0x20–0x2F | 空格、!"#$%&’()*+,-./ |
最终字节 | 0x40–0x7E | @A–Z[]^_`a–z{ |
部分CSI编码:
Abbr | Name | Effect | |
CSI n A | CUU | Cursor Up | Moves the cursor n (default 1) cells in the given direction. If the cursor is already at the edge of the screen, this has no effect. |
CSI n B | CUD | Cursor Down | |
CSI n C | CUF | Cursor Forward | |
CSI n D | CUB | Cursor Back | |
CSI n E | CNL | Cursor Next Line | Moves cursor to beginning of the line n (default 1) lines down. (not ANSI.SYS) |
CSI n F | CPL | Cursor Previous Line | Moves cursor to beginning of the line n (default 1) lines up. (not ANSI.SYS) |
CSI n G | CHA | Cursor Horizontal Absolute | Moves the cursor to column n (default 1). (not ANSI.SYS) |
CSI n ; m H | CUP | Cursor Position | Moves the cursor to row n, column m. The values are 1-based, and default to 1 (top left corner) if omitted. A sequence such as CSI ;5H is a synonym for CSI 1;5H as well as CSI 17;H is the same as CSI 17H and CSI 17;1H |
CSI n J | ED | Erase in Display | Clears part of the screen. If n is 0 (or missing), clear from cursor to end of screen. If n is 1, clear from cursor to beginning of the screen. If n is 2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS). If n is 3, clear entire screen and delete all lines saved in the scrollback buffer (this feature was added for xterm and is supported by other terminal applications). |
CSI n K | EL | Erase in Line | Erases part of the line. If n is 0 (or missing), clear from cursor to the end of the line. If n is 1, clear from cursor to beginning of the line. If n is 2, clear entire line. Cursor position does not change. |
CSI n S | SU | Scroll Up | Scroll whole page up by n (default 1) lines. New lines are added at the bottom. (not ANSI.SYS) |
CSI n T | SD | Scroll Down | Scroll whole page down by n (default 1) lines. New lines are added at the top. (not ANSI.SYS) |
CSI n ; m f | HVP | Horizontal Vertical Position | Same as CUP, but counts as a format effector function (like CR or LF) rather than an editor function (like CUD or CNL). This can lead to different handling in certain terminal modes.[5]: Annex A |
CSI n m | SGR | Select Graphic Rendition | Sets colors and style of the characters following this code |
CSI 5i | AUX Port On | Enable aux serial port usually for local serial printer | |
CSI 4i | AUX Port Off | Disable aux serial port usually for local serial printer | |
CSI 6n | DSR | Device Status Report | Reports the cursor position (CPR) by transmitting ESC[n;mR, where n is the row and m is the column.) |
SGR (Select Graphic Rendition) parameters
部分SGR编码:ESC*
* | Note | |
0 | Reset or normal | All attributes off |
1 | Bold or increased intensity | As with faint, the color change is a PC (SCO / CGA) invention. |
2 | Faint, decreased intensity, or dim | May be implemented as a light font weight like bold. |
3 | Italic | Not widely supported. Sometimes treated as inverse or blink. |
4 | Underline | Style extensions exist for Kitty, VTE, mintty and iTerm2. |
5 | Slow blink | Sets blinking to less than 150 times per minute |
6 | Rapid blink | MS-DOS ANSI.SYS, 150+ per minute; not widely supported |
7 | Reverse video or invert | Swap foreground and background colors; inconsistent emulation |
8 | Conceal or hide | Not widely supported. |
9 | Crossed-out, or strike | Characters legible but marked as if for deletion. |
10 | Primary (default) font | |
11–19 | Alternative font | Select alternative font n − 10 |
20 | Fraktur (Gothic) | Rarely supported |
21 | Doubly underlined; or: not bold | Double-underline per ECMA-48, but instead disables bold intensity on several terminals, including in the Linux kernel's console before version 4.17. |
22 | Normal intensity | Neither bold nor faint; color changes where intensity is implemented as such. |
23 | Neither italic, nor blackletter | |
24 | Not underlined | Neither singly nor doubly underlined |
25 | Not blinking | Turn blinking off |
26 | Proportional spacing | ITU T.61 and T.416, not known to be used on terminals |
27 | Not reversed | |
28 | Reveal | Not concealed |
29 | Not crossed out | |
30–37 | Set foreground color | |
38 | Set foreground color | Next arguments are 5;n or 2;r;g;b |
39 | Default foreground color | Implementation defined (according to standard) |
40–47 | Set background color | |
48 | Set background color | Next arguments are 5;n or 2;r;g;b |
49 | Default background color | Implementation defined (according to standard) |
50 | Disable proportional spacing | T.61 and T.416 |
51 | Framed | Implemented as "emoji variation selector" in mintty. |
52 | Encircled | |
53 | Overlined | |
54 | Neither framed nor encircled | |
55 | Not overlined | |
58 | Set underline color | Not in standard; implemented in Kitty, VTE, mintty, and iTerm2. Next arguments are 5;n or 2;r;g;b. |
59 | Default underline color | Not in standard; implemented in Kitty, VTE, mintty, and iTerm2. |
60 | Ideogram underline or right side line | Rarely supported |
61 | Ideogram double underline, or double line on the right side | |
62 | Ideogram overline or left side line | |
63 | Ideogram double overline, or double line on the left side | |
64 | Ideogram stress marking | |
65 | No ideogram attributes | Reset the effects of all of 60–64 |
73 | Superscript | Implemented only in mintty |
74 | Subscript | |
75 | Neither superscript nor subscript | |
90–97 | Set bright foreground color | Not in standard; originally implemented by aixterm |
100–107 | Set bright background color |
其他的还有很多序列: