ANSI 转义序列
实例
终端标题
以下脚本可以使终端在 3 秒钟内更改标题为 test。
echo -ne "\e]0;test\a" >&2 && sleep 3
把 \e]0;
改为 \e]2;
也可以。
如果使用 fish,要把 \e
改成 \x1b
,因为 fish 不支持这个转义。
光标控制
显示/隐藏光标
\e[?25l
- 隐藏光标
\e[?25h
- 显示光标
清屏
\e[2J
- 清屏,将光标置于末行
\e[2J\e[0;0f
- 清屏,然后将光标置于首行
备用屏幕
Alternate Screen:
\e[?1049h
或tput smcup
- 切换至 alt screen
\e[?1049l
或tput rmcup
- 从 alt screen 返回
移动光标
\e[nA
- 上移n行,n默认为1
\e[nB
- 下移n行,n默认为1
\e[nC
- 右移n行,n默认为1
\e[nD
- 左移n行,n默认为1
\e[x;yf
- 将光标置于
x
行y
列[1]
改变光标形状
在 Linux tty 下,使用 \e[?nc
可改变光标形状。 n
为 0 时似乎是默认,为 1 时不可见,为 2 时为下划线,6 时为方块,2~6 之间的为“不同厚度下划线”。
在 xterm 兼容终端模拟器下,使用 \e[n q
来改变光标形状。已知的值如下:[2]
值 | 形状 | 闪烁 |
---|---|---|
0 | 默认 | |
1 | 块状 | Y |
2 | N | |
3 | 下划线 | Y |
4 | N | |
5 | 竖线 | Y |
6 | N |
查询光标位置
发送 \e[6n
,会返回一个包含光标位置位置的字符串,如 ^[[15;1R
[3]。在 zsh 中可以如下获得光标位置:
__cursor_pos () {
local pos
exec {tty}<>/dev/tty
echo -n '\e[6n' >&$tty; read -rsdR pos <&$tty
exec {tty}>&-
[[ $pos =~ '([0-9]+);([0-9]+)$' ]]
print $match[1] $match[2]
}
颜色
各种对颜色的处理:[4]
# Manipulate colors 0-256
# \033]4;{index};{color}\007
# Change color 7 to #FFFFFF
\033]4;7;#FFFFFF\007
# Change color 14 to #333333
\033]4;14;#333333\007
# Manipulate special colors.
# 10 = foreground, 11 = background, 12 = cursor foregound
# 13 = mouse foreground, 708 = terminal border background
# \033]{index};{color}\007
# Change the terminal foreground to #FFFFFF
\033]10;#FFFFFF\007
# Change the terminal background to #000000
\033]11;#000000\007
# Change the terminal cursor to #FFFFFF
\033]12;#FFFFFF\007
# Change the terminal border background to #000000
\033]708;#000000\007
改变光标颜色
通常终端(xterm 和 rxvt 等)下,使用序列 "\033]12;#ff6565\007"
来改变光标颜色。也可以使用颜色名 "\033]12;grey\007"
或者短的颜色值 '\033]12;#fa0\007'
。在 screen 下这样使用: '\033P\033]12;#ff65ff\007\033\\'
。[5]tmux 下使用这样的序列: '\033Ptmux;\033\033]12;red\007\033\\'
。[6]
文本操作
\e[K
- 清除到行尾
剪贴板
序列是 \e]52;Pc;Pd\e\\
,其中 Pc
取值为 c p s 0-7
,分别代表剪贴板、主选择区、(也是主选择区)、八个 cut buffer。Pd
是数据的 base64 编码。如果 Pd
为 ?
那么是获取剪贴板的内容。返回格式相同。
xterm 在设置 X 资源 allowWindowOps
为 true
时支持。gnome-terminal 不支持。mosh 只在 git 版本支持设置[7],并不支持读取。tmux 理论上可以开启,但是测试未成功。
另见 Copying to clipboard from tmux and Vim using OSC 52 - The Terminal Programmer。
色彩
另见 ANSI escape code - Wikipedia § Colors 和 True Colour (16 million colours) support in various terminal applications and terminals。
NONE | \033[m |
RED | \033[31m |
GREEN | \033[32m |
YELLOW | \033[33m |
BLUE | \033[34m |
PURPLE | \033[35m |
CYAN | \033[36m |
WHITE | \033[37m |
多项可将数字合起来写,中间用分号隔开
Gnome 终端 | xterm | |
---|---|---|
\033[0m | 关闭所有属性 | |
\033[1m | 加粗 | 加亮加粗 |
\033[2m | 暗色 | 无效果 |
\033[3m | 无效果 | 无效果 |
\033[4m | 下划线 | |
\033[5m | 无效果 | 闪烁(不稳定) |
\033[6m | 无效果 | |
\033[7m | 反显 | |
\033[8m | 隐藏 | |
\033[9m | 删除线 | 无效果 |
参见
外部链接
- 转义序列文档
- Xterm Colors,在 xterm 中的颜色名称。
- Bring Colors to the Windows Console with Python « Burgaud.com,在Windows控制台上使用彩色
- Vim Control Sequence Example
- Some Useful Terminal Escape Sequences - Yuxin's Blog
- [https://dgl.cx/2023/09/ansi-terminal-security "�[31m"?! ANSI Terminal security in 2023 and finding 10 CVEs]
参考资料
- ↑ http://www.vt100.net/docs/vt510-rm/CUP
- ↑ jszakmeister/vim-togglecursor: Toggle the cursor shape in the terminal for Vim.
- ↑ http://www.vt100.net/docs/vt510-rm/CPR
- ↑ Change your terminal’s background color dynamically using escape sequences
- ↑ change terminal cursor color to reflect vi mode - pointfree
- ↑ http://sourceforge.net/mailarchive/message.php?msg_id=27195550
- ↑ support osc 52 clipboard copy integration. · mobile-shell/mosh@4b240ac