ANSI escape code

ANSI转义序列是带信号的标准编码,用于控制视频文本终端和终端模拟器上的光标位置、颜色、字体样式和其他选项。这些字节序列(大多数以ASCII转义字符和括号字符开头)会嵌入到文本中。终端将这些序列解释为命令,而不是逐字显示的文本(这就导致在终端交互时看不到这些,但采用一些收集脚本收集这些终端的命令反馈时,收集回来的数据中会看到大量这些编码)。

控制字符

C0

Hex字符描述
\x00\0Null
\x05ENQ 触发应答消息
\x07\aBell 响铃,当你在terminal中做某些无效操作的时候,返回\x07
\x08\bBackSpace 退格键(发送退格键,if 在行首返回\x07 else 返回\x08\x1b[K,具体含义见后续)
\x09\tHorizontal Tab 横向跳至下一个制表位,若后面没有制表位,则跳至行尾
\x0a\nLine Feed 光标下移一行(终端中表现为换行)
\x0b\vVertical Tab 竖向跳至下一个制表位(终端中表现为换行)
\x0c\fForm Feed 光标跳转至下一页头部(终端中表现为换行)
\x0d\rCarriage Return 回车换行
\x0eSO 激活 G1 字符集
\x0fSI 激活 G0 字符集
\0x11\qDC1 继续传送
\0x13\sDC3 忽略除XOFF 和 XON 以外的一切字符
\x18CAN 实质 Ctrl+X 中断转义序列/进程
\x1a\zSUB(EOF) 实质 Ctrl+Z 中断转义序列/进程
\x1b\eEscape 开始一个新的转义序列
\x7fDEL 忽略
缩写/字符OCT(八进制)DEC(十进制)HEX(十六进制)转义表示解释
NUL(null)0000x00空字符
SOH(start of headline)0110x01标题开始
STX(start of text)0220x02正文开始
ETX(end of text)0330x03正文结束
EOT(end of transmission)0440x04传输结束
ENQ(enquiry)0550x05请求
ACK(acknowledge)0660x06收到通知
BEL(bell)0770x07\a响铃
BS(backspace)01080x08\b退格
HT(horizontal tab)01190x09\t水平制表符
LF(NL line feed,new line)012100x0A\n换行建
VT(vertical tab)013110x0B\v垂直制表符
FF(NP form feed,new page)014120x0C\f换页键
CR(carriage return)015130x0D\r回车键
SO(shift out)016140x0E不用切换
SI(shift in)017150x0F启用切换
DLE(data link escape)020160x10数据链路转义
DC1(device control 1)021170x11设备控制1
DC2(device control 2)022180x12设备控制2
DC3(device control 3)023190x13设备控制3
DC4(device control 4)024200x14设备控制4
NAK(negative acknowledge)025210x15拒绝接收
SYN(synchronous idle)026220x16同步空闲
ETB(end of trans. Block)027230x17结束传输块
CAN(cancel)030240x18取消
EM(end of medium)031250x19媒介结束
SUB(substitute)032260x1A代替
ESC(escape)033270x1B\e换码(溢出)
FS(file separator)034280x1C文件分隔符
GS(group separator)035290x1D分组符
RS(record separator)036300x1E记录分隔符
US(unit separator)037310x1F单元分隔符
DEL(delete)01771270x7f删除

C1

C1AbbrNameEffect
ESC N0x8ESS2Single Shift Two
ESC O0x8FSS3Single Shift Three
ESC P0x90DCSDevice Control String
ESC [0x9BCSIControl Sequence Introducer
ESC \0x9CSTString Terminator
ESC ]0x9DOSCOperating System Command
ESC X0x98SOSStart of String
ESC ^0x9EPMPrivacy Message
ESC _0x9FAPCApplication Program Command

CSI (Control Sequence Introducer) sequences

ANSI转义序列中以 ESC [ 开头的叫作 Control Sequence Introducer,简写为 CSI。以 CSI 开头的指令有很多,大致可分四类:光标移动指令、清屏指令、字符渲染(Graphic Rendition)指令和终端控制指令。

CSI序列由ESC [、若干个(包括0个)“参数字节”、若干个“中间字节”,以及一个“最终字节”组成。各部分的字符范围如下:

组成部分字符范围ASCII
参数字节0x30–0x3F0–9:;<=>?
中间字节0x20–0x2F空格、!"#$%&’()*+,-./
最终字节0x40–0x7E@A–Z[]^_`a–z{

部分CSI编码:

AbbrNameEffect 
CSI n ACUUCursor UpMoves 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 BCUDCursor Down
CSI n CCUFCursor Forward
CSI n DCUBCursor Back
CSI n ECNLCursor Next LineMoves cursor to beginning of the line n (default 1) lines down. (not ANSI.SYS)
CSI n FCPLCursor Previous LineMoves cursor to beginning of the line n (default 1) lines up. (not ANSI.SYS)
CSI n GCHACursor Horizontal AbsoluteMoves the cursor to column n (default 1). (not ANSI.SYS)
CSI n ; m HCUPCursor PositionMoves 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 JEDErase in DisplayClears 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 KELErase in LineErases 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 SSUScroll UpScroll whole page up by n (default 1) lines. New lines are added at the bottom. (not ANSI.SYS)
CSI n TSDScroll DownScroll whole page down by n (default 1) lines. New lines are added at the top. (not ANSI.SYS)
CSI n ; m fHVPHorizontal Vertical PositionSame 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 mSGRSelect Graphic RenditionSets colors and style of the characters following this code
CSI 5i AUX Port OnEnable aux serial port usually for local serial printer
CSI 4i AUX Port OffDisable aux serial port usually for local serial printer
CSI 6nDSRDevice Status ReportReports 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 
0Reset or normalAll attributes off
1Bold or increased intensityAs with faint, the color change is a PC (SCO / CGA) invention.
2Faint, decreased intensity, or dimMay be implemented as a light font weight like bold.
3ItalicNot widely supported. Sometimes treated as inverse or blink.
4UnderlineStyle extensions exist for Kitty, VTE, mintty and iTerm2.
5Slow blinkSets blinking to less than 150 times per minute
6Rapid blinkMS-DOS ANSI.SYS, 150+ per minute; not widely supported
7Reverse video or invertSwap foreground and background colors; inconsistent emulation
8Conceal or hideNot widely supported.
9Crossed-out, or strikeCharacters legible but marked as if for deletion.
10Primary (default) font 
11–19Alternative fontSelect alternative font n − 10
20Fraktur (Gothic)Rarely supported
21Doubly underlined; or: not boldDouble-underline per ECMA-48,  but instead disables bold intensity on several terminals, including in the Linux kernel's console before version 4.17.
22Normal intensityNeither bold nor faint; color changes where intensity is implemented as such.
23Neither italic, nor blackletter 
24Not underlinedNeither singly nor doubly underlined
25Not blinkingTurn blinking off
26Proportional spacingITU T.61 and T.416, not known to be used on terminals
27Not reversed 
28RevealNot concealed
29Not crossed out 
30–37Set foreground color 
38Set foreground colorNext arguments are 5;n or 2;r;g;b
39Default foreground colorImplementation defined (according to standard)
40–47Set background color 
48Set background colorNext arguments are 5;n or 2;r;g;b
49Default background colorImplementation defined (according to standard)
50Disable proportional spacingT.61 and T.416
51FramedImplemented as "emoji variation selector" in mintty.
52Encircled
53Overlined 
54Neither framed nor encircled 
55Not overlined 
58Set underline colorNot in standard; implemented in Kitty, VTE, mintty, and iTerm2. Next arguments are 5;n or 2;r;g;b.
59Default underline colorNot in standard; implemented in Kitty, VTE, mintty, and iTerm2.
60Ideogram underline or right side lineRarely supported
61Ideogram double underline, or double line on the right side
62Ideogram overline or left side line
63Ideogram double overline, or double line on the left side
64Ideogram stress marking
65No ideogram attributesReset the effects of all of 60–64
73SuperscriptImplemented only in mintty
74Subscript
75Neither superscript nor subscript
90–97Set bright foreground colorNot in standard; originally implemented by aixterm
100–107Set bright background color

其他的还有很多序列:

OSC (Operating System Command) sequences

Fs Escape sequences

nF Escape sequences

Terminal input sequences

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值