首页 > 其他分享 >ANSI 转义序列(ANSI Escape Sequences)

ANSI 转义序列(ANSI Escape Sequences)

时间:2024-04-06 16:35:02浏览次数:13  
标签:keypad -- Sequences ANSI color ESC Escape 224 光标

本文来自 Github Gist from "fnky/ANSI.md"

下面是笔者翻译版本。持续更新中。

ANSI 转义序列

标准 Esc 代码以 Escape 为前缀:

  • Ctrl 快捷键: ^[
  • 八进制: \033
  • Unicode: \u001b
  • 十六进制: \x1B
  • 十进制: 27

后面跟着命令,有时用左方括号([)分隔,称为控制序列引导码(CSI),后面可选地跟着可选的参数和命令本身。

参数通过分号(;)分隔,例如:

\x1b[1;31m # 设置样式为粗体,红字。

序列

  • ESC - 以 ESC\x1B) 开头的序列
  • CSI - 控制序列引导码:以 ESC [ 或 CSI(\x9B)开头的序列
  • DCS - 设备控制字串:以 ESC P 或 DCS (\x90)开头的序列
  • OSC - 操作系统命令:以 ESC ] 或 OSC(\x9D)开头的序列

序列和参数之间的空格应该被忽略。它们的存在是为了提高可读性。

普通 ASCII 码

名字 十进制 八进制 十六进制 C 风格转义 Ctrl 快捷键 描述
BEL 7 007 0x07 \a ^G 终端响铃
BS 8 010 0x08 \b ^H 退格
HT 9 011 0x09 \t ^I 横向制表符(Tab)
LF 10 012 0x0A \n ^J 换行
VT 11 013 0x0B \v ^K 纵向制表符(Tab)
FF 12 014 0x0C \f ^L 换页 (即:New page NP)
CR 13 015 0x0D \r ^M 回车
ESC 27 033 0x1B \e* ^[ 字符 Esc
DEL 127 177 0x7F <none> <none> 字符 Delete

注意:有些控制转义序列,比如 \e 表示 ESC,并不能保证在所有语言和编译器中都能正常工作。建议使用十进制、八进制或十六进制表示作为转义码。

注意Ctrl 快捷键 表示方式只是将形如 ASCII 码 \(1\) 等的不可打印字符与 ASCII 码 \(65\)(“A”)等的可打印字符关联起来。ASCII 码 \(1\) 是 ^A(Ctrl-A),而 ASCII 码 \(7\)(BEL) 是 ^G(Ctrl-G)。这是一种常见的表示和输入方式。历史上,它们来自于 VT 系列终端之一。

光标控制

转义序列码 描述
ESC[H 将光标移回 \((0,0)\)
ESC[{line};{column}H <br> ESC[{line};{column}f moves cursor to line #, column #
ESC[#A 光标上移 # 行
ESC[#B 光标下移 # 行
ESC[#C 光标右移 # 列
ESC[#D 光标左移 # 列
ESC[#E 光标下移 # 行,移到行首
ESC[#F 光标上移 # 行,移到行首
ESC[#G 移动光标至列 #
ESC[6n 求光标位置 (以 ESC[#;#R 形式汇报)
ESC M 光标上移一行,按需可滚动
ESC 7 保存光标位置(DEC)
ESC 8 将光标恢复到上次保存的位置(DEC)
ESC[s 保存光标位置(SCO)
ESC[u 将光标恢复到上次保存的位置(SCO)

注意:有些序列,比如保存和恢复光标,是私有序列,没有标准化。虽然一些终端仿真器(即 xterm 和 derived)同时支持 SCO 和 DEC 序列,但它们可能具有不同的功能。因此,建议使用 DEC 序列。

擦除功能

转义序列码 描述
ESC[J 清屏(同 ESC[0J)此处可能有误,应为 ESC[2J
ESC[0J 从光标擦到屏尾
ESC[1J 从光标擦到屏首
ESC[2J 擦除整个屏幕
ESC[3J 擦除保存的行(?)
ESC[K 整行擦除(same as ESC[0K)此处可能有误,应为 ESC[2K
ESC[0K 从光标擦到行尾
ESC[1K 从光标擦到行首
ESC[2K 擦除整个行

注意:擦除行不会移动光标,这意味着光标将停留在该行的原位。你可以在擦除行后使用 \r,将光标返回到当前行开始处。

颜色 / 图形模式

转义序列码 对应的重置序列 描述
ESC[1;34;{...}m 设置单元格(?)的图形模式,用分号分隔 (;).
ESC[0m 重置(消除)所有颜色和图形
ESC[1m ESC[22m 粗体
ESC[2m ESC[22m 虚化(暗淡)
ESC[3m ESC[23m 斜体
ESC[4m ESC[24m 下划线
ESC[5m ESC[25m 闪烁
ESC[7m ESC[27m 反向
ESC[8m ESC[28m 隐形
ESC[9m ESC[29m 删除线

注意:一些终端可能不支持上述模式。

注意:虚化和粗体模式都用 ESC[22m 序列重置。ESC[21m 序列是双下划线模式下的非指定序列,仅在某些终端中工作,并用 ESC[24m 重置。

颜色码

大多数终端支持 8 色和 16 色,以及 256(8位)色。这些颜色由用户设置,但通常具有定义好的含义。

8-16 Colors

颜色名 前景色(字)码 背景色码
30 40
31 41
绿 32 42
33 43
34 44
品红 35 45
36 46
37 47
默认 39 49
重置 0 0

注意: 重置 色重置所有颜色、字体效果,默认 色只重置颜色。

大多数终端除了基本的 \(8\) 种颜色外,还支持“明亮”或“大胆”的颜色。它们有自己的一套代码,与正常的颜色镜像,但在代码中有一个额外的 ;1

# 设置样式为粗体,红色字(前景色)。
\x1b[1;31mHello
# 设置样式为暗淡的白色字(前景色),红色背景。
\x1b[2;37;41mWorld

支持 aixterm 规范 的终端提供明亮版本的 ISO 颜色,不需要使用 bold 修饰符:

颜色名 前景(字)色码 背景色码
亮黑 90 100
亮红 91 101
亮绿 92 102
亮黄 93 103
亮蓝 94 104
亮品红 95 105
亮青 96 106
亮白 97 107

256 色

下面的转义码告诉终端使用给定的颜色 ID:

转义序列码 描述
ESC[38;5;{ID}m 设置前景色
ESC[48;5;{ID}m 设置背景色

其中的 {ID} 应该用下面颜色表中从 \(0\) 到 \(255\) 的颜色索引替换:

256 Color table

该表从最初的 \(16\) 种颜色(\(0\sim 15\))开始。

接下来的 216 种颜色 \((16-231)\) 或由偏移 \(16\) 的 3bpc RGB 值组成,打包成一个值。

The final 24 colors (232-255) are grayscale starting from a shade slighly lighter than black, ranging up to shade slightly darker than white.

Some emulators interpret these steps as linear increments (256 / 24) on all three channels, although some emulators may explicitly define these values.

RGB Colors

More modern terminals supports Truecolor (24-bit RGB), which allows you to set foreground and background colors using RGB.

These escape sequences are usually not well documented.

ESC Code Sequence Description
ESC[38;2;{r};{g};{b}m Set foreground color as RGB.
ESC[48;2;{r};{g};{b}m Set background color as RGB.

注意 that ;38 and ;48 corresponds to the 16 color sequence and is interpreted by the terminal to set the foreground and background color respectively. Where as ;2 and ;5 sets the color format.

Screen Modes

Set Mode

ESC Code Sequence Description
ESC[={value}h Changes the screen width or type to the mode specified by value.
ESC[=0h 40 x 25 monochrome (text)
ESC[=1h 40 x 25 color (text)
ESC[=2h 80 x 25 monochrome (text)
ESC[=3h 80 x 25 color (text)
ESC[=4h 320 x 200 4-color (graphics)
ESC[=5h 320 x 200 monochrome (graphics)
ESC[=6h 640 x 200 monochrome (graphics)
ESC[=7h Enables line wrapping
ESC[=13h 320 x 200 color (graphics)
ESC[=14h 640 x 200 color (16-color graphics)
ESC[=15h 640 x 350 monochrome (2-color graphics)
ESC[=16h 640 x 350 color (16-color graphics)
ESC[=17h 640 x 480 monochrome (2-color graphics)
ESC[=18h 640 x 480 color (16-color graphics)
ESC[=19h 320 x 200 color (256-color graphics)
ESC[={value}l Resets the mode by using the same values that Set Mode uses, except for 7, which disables line wrapping. The last character in this escape sequence is a lowercase L.

Common Private Modes

These are some examples of private modes, which are not defined by the specification, but are implemented in most terminals.

ESC Code Sequence Description
ESC[?25l make cursor invisible
ESC[?25h make cursor visible
ESC[?47l restore screen
ESC[?47h save screen
ESC[?1049h enables the alternative buffer
ESC[?1049l disables the alternative buffer

Refer to the XTerm Control Sequences for a more in-depth list of private modes defined by XTerm.

注意: While these modes may be supported by the most terminals, some may not work in multiplexers like tmux.

Keyboard Strings

ESC[{code};{string};{...}p

Redefines a keyboard key to a specified string.

The parameters for this escape sequence are defined as follows:

  • code is one or more of the values listed in the following table. These values represent keyboard keys and key combinations. When using these values in a command, you must type the semicolons shown in this table in addition to the semicolons required by the escape sequence. The codes in parentheses are not available on some keyboards. ANSI.SYS will not interpret the codes in parentheses for those keyboards unless you specify the /X switch in the DEVICE command for ANSI.SYS.
  • string is either the ASCII code for a single character or a string contained in quotation marks. For example, both 65 and "A" can be used to represent an uppercase A.

IMPORTANT: Some of the values in the following table are not valid for all computers. Check your computer's documentation for values that are different.

List of keyboard strings

Key Code SHIFT+code CTRL+code ALT+code
F1 0;59 0;84 0;94 0;104
F2 0;60 0;85 0;95 0;105
F3 0;61 0;86 0;96 0;106
F4 0;62 0;87 0;97 0;107
F5 0;63 0;88 0;98 0;108
F6 0;64 0;89 0;99 0;109
F7 0;65 0;90 0;100 0;110
F8 0;66 0;91 0;101 0;111
F9 0;67 0;92 0;102 0;112
F10 0;68 0;93 0;103 0;113
F11 0;133 0;135 0;137 0;139
F12 0;134 0;136 0;138 0;140
HOME (num keypad) 0;71 55 0;119 --
UP ARROW (num keypad) 0;72 56 (0;141) --
PAGE UP (num keypad) 0;73 57 0;132 --
LEFT ARROW (num keypad) 0;75 52 0;115 --
RIGHT ARROW (num keypad) 0;77 54 0;116 --
END (num keypad) 0;79 49 0;117 --
DOWN ARROW (num keypad) 0;80 50 (0;145) --
PAGE DOWN (num keypad) 0;81 51 0;118 --
INSERT (num keypad) 0;82 48 (0;146) --
DELETE (num keypad) 0;83 46 (0;147) --
HOME (224;71) (224;71) (224;119) (224;151)
UP ARROW (224;72) (224;72) (224;141) (224;152)
PAGE UP (224;73) (224;73) (224;132) (224;153)
LEFT ARROW (224;75) (224;75) (224;115) (224;155)
RIGHT ARROW (224;77) (224;77) (224;116) (224;157)
END (224;79) (224;79) (224;117) (224;159)
DOWN ARROW (224;80) (224;80) (224;145) (224;154)
PAGE DOWN (224;81) (224;81) (224;118) (224;161)
INSERT (224;82) (224;82) (224;146) (224;162)
DELETE (224;83) (224;83) (224;147) (224;163)
PRINT SCREEN -- -- 0;114 --
PAUSE/BREAK -- -- 0;0 --
BACKSPACE 8 8 127 (0)
ENTER 13 -- 10 (0
TAB 9 0;15 (0;148) (0;165)
NULL 0;3 -- -- --
A 97 65 1 0;30
B 98 66 2 0;48
C 99 66 3 0;46
D 100 68 4 0;32
E 101 69 5 0;18
F 102 70 6 0;33
G 103 71 7 0;34
H 104 72 8 0;35
I 105 73 9 0;23
J 106 74 10 0;36
K 107 75 11 0;37
L 108 76 12 0;38
M 109 77 13 0;50
N 110 78 14 0;49
O 111 79 15 0;24
P 112 80 16 0;25
Q 113 81 17 0;16
R 114 82 18 0;19
S 115 83 19 0;31
T 116 84 20 0;20
U 117 85 21 0;22
V 118 86 22 0;47
W 119 87 23 0;17
X 120 88 24 0;45
Y 121 89 25 0;21
Z 122 90 26 0;44
1 49 33 -- 0;120
2 50 64 0 0;121
3 51 35 -- 0;122
4 52 36 -- 0;123
5 53 37 -- 0;124
6 54 94 30 0;125
7 55 38 -- 0;126
8 56 42 -- 0;126
9 57 40 -- 0;127
0 48 41 -- 0;129
- 45 95 31 0;130
= 61 43 --- 0;131
[ 91 123 27 0;26
] 93 125 29 0;27
92 124 28 0;43
; 59 58 -- 0;39
' 39 34 -- 0;40
, 44 60 -- 0;51
. 46 62 -- 0;52
/ 47 63 -- 0;53
` 96 126 -- (0;41)
ENTER (keypad) 13 -- 10 (0;166)
/ (keypad) 47 47 (0;142) (0;74)
* (keypad) 42 (0;144) (0;78) --
- (keypad) 45 45 (0;149) (0;164)
+ (keypad) 43 43 (0;150) (0;55)
5 (keypad) (0;76) 53 (0;143) --

Resources

标签:keypad,--,Sequences,ANSI,color,ESC,Escape,224,光标
From: https://www.cnblogs.com/chargedcreeper/p/-/ANSI

相关文章

  • CF1924D Balanced Subsequences 题解
    先判掉\(k>\min(n,m)\)的情况。首先有个明显的计算最长合法括号子序列的贪心方法:初始一个值为\(0\)的计数器,从左到右枚举每个字符,遇到(时将计数器加一,遇到)时如果计数器不为\(0\)就减一然后将答案加一。考虑绘制它的折线图。初始时纵坐标为\(0\),从左到右枚举每个字符......
  • RHCE(ansible语句)
    一:when语句1:基础了解 说到底,还是有多个表达式来组成一个判断语句,很多种的判断语句1、为什么需要判断语句:有的时候play的结果需要依赖于变量,fact或者是前一个任务的执行结果,或者基于上一个task执行返回的结果而决定如何执行后续的task,这个时候就需要条件的判断了,一个很简单的......
  • ansible-2:playbook
    一、回顾ansible命令执行过程(重)1.加载自己的配置文件默认为/etc/ansible/ansible.cfg2.加载自己对应的模块文件,如command3.通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件4.......
  • [hdu6647]Bracket Sequences on Tree 解题报告
    oj:https://gxyzoj.com/d/hzoj/p/3575因为自己的脑残原因,调了8个小时啊!!!切入正题Part1假定1为根,可以发现,如果u的两棵子树同构,则他们遍历的顺序不影响答案所以,就可以将子树按哈希值分类,这道题就变成了一个可重复组合问题,设\(f_i\)表示以1为根时i的方案数,\(a_i\)表示某一种哈......
  • Ansible获取Cisco IOS版本导出至CSV
    1.inventory和密码加密参考AnsiblePlaybook备份Juniper及Cisco设备2.创建playbook-name:getciscoswitchesversionhosts:cisco_switchesgather_facts:falsevars_files:-pass.ymlvars:ansible_password:'{{cisco_pass}}'output_path:&qu......
  • Linux架构27 Ansible变量, 定义变量的方式, 变量注册, facts缓存
    Ansible变量一、变量概述变量提供了便捷的方式来管理Ansibleplaybook的每一个项目中的动态值,比如nginx-1.6.3这个软件包的版本,在其他地方或许会反复使用,那么如果讲此值设置为变量,然后再在其他的playbook中调用,会方便许多。如此一来还方便维护,减少维护的成本。二、定义变量的......
  • ansible维护
     参考:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive_module.html1,检测网络-hosts:allgather_facts:nobecome:yestasks:-name:Installtraceroutepackage:name:"{{item}}"state:present......
  • ansible脚本
    -hosts:allgather_facts:nobecome:yestasks:-name:Installtraceroutepackage:name:"{{item}}"state:presentwith_items:-traceroute-name:checkipshell:"{{item}}"register:......
  • [ZJCTF 2019]NiZhuanSiWei
    [ZJCTF2019]NiZhuanSiWei审题看到可以传入file,text,和password三个参数。知识点php伪协议,反序列化解题传入text,看到有file_get_content函数,这个函数表示读取$text文件里的值,返回字符串。所以要传入text的为一个文件值,所以使用data协议写入参数。text=data://text/plain......
  • 第三十七天:Ansible playbook变量
    Playbook中同样也支持变量变量名:仅能由字母、数字和下划线组成,且只能以字母开头变量定义:variable=valuevariable:value变量调用方式:通过{{variable_name}}调用变量,且变量名前后建议加空格,有时用"{{variable_name}}"才生效变量来源:1.ansible的setupfacts远程......