DECRQSS — 请求选区或设置(DCS $ q ... ST)
请求终端报告任一 SGR / 模式 / 边距 / 光标形状当前值 —— DCS 通用查询。
字节形式
涵盖所有常见的字符串字面量写法,方便正反查找。
\\x1b[
\x1bP$q<P>\x1b\\\\033[
\033P$q<P>\033\\\\e[
\eP$q<P>\e\\ESC [
ESC P $ q <P> ESC \hex
1b 50 24 71 ... 1b 5c说明
Request Selection or Setting。中间字节 $(0x24)、末字节 q(0x71)的 DCS 帧;体 <P> 指明要查询的设置。常见探测:\x1bP$qm\x1b\\ 查询当前 SGR 属性(终端以 \x1bP1$rm\x1b\\ 回报当前颜色/样式栈);\x1bP$q r\x1b\\(字面 $ q SP r)查询 DECSTBM 滚动区域上下边距;\x1bP$q" p\x1b\\ 查询 DECSCL 一致性级别;\x1bP$q" q\x1b\\ 查询 DECSCA 字符保护;\x1bP$q q\x1b\\(SP q)查询当前 DECSCUSR 光标形状。回复同样是 DCS 帧 —— 首字节 1(有效且支持)或 0(请求无效),随后 $r、所请求的设置字节、ST。现代程序在启动探测时使用 DECRQSS,以便退出时恢复终端的原有状态 —— vim 的 t_RV 自动检测、bat 的语法主题选择、kitty kitten themes 都依赖它。
规范出处: xterm-ctlseqs (DECRQSS, DCS $ q Pt ST)
示例
# Ask for the current SGR attributes; reply lands on stdin in raw mode.\nstty -echo raw min 0 time 5\nprintf '\033P$qm\033\\'; IFS= read -r -t 1 -d '\\' REPLY; stty sane\necho "DECRQSS m reply: $REPLY"import sys; sys.stdout.write('\x1bP$qm\x1b\\\\') # reply arrives on stdinfmt.Print("\x1bP$qm\x1b\\\\") // request current SGRprocess.stdout.write('\x1bP$qm\x1b\\\\') // request current SGR; read process.stdinprintf("\x1bP$qm\x1b\\\\"); fflush(stdout); /* read DCS reply from stdin */在哪里用到
实际会发出该序列的工具——把抽象字节锚定到你已经用过的命令上。
- vim, neovim — terminal capability probe (`t_RV`, `&background` detection)vim 启动时发 `\x1bP$qm\x1b\\` 读取当前 SGR 状态,发 `\x1bP$q q\x1b\\`(`SP q`)读取光标形状 —— 因此 `:q!` 退出后,光标回到你原来的竖线 / 方块 / 下划线,而不是 vim 硬编码的默认值
- kitty `kitten themes` and `kitten icat` probe`themes` kitten 在改动运行时配置前用 DECRQSS 读取当前 SGR 调色板和光标形状参数 —— `kitten themes solarized-light` 切换能干净地翻转 SGR 映射,而不会覆盖自定义的闪烁方块光标
- bat — auto-theme picker for `--theme=auto``bat --theme=auto` 用 `\x1bP$qm\x1b\\` 探测当前 SGR;若没有明确的默认背景信号,再用 OSC 11 + DECRQSS `" p`(一致性级别)决定切浅色或深色语法主题,而不是从 `$TERM` 猜测
- tmux passthrough wrapper (`allow-passthrough on`)tmux 默认剥离 DCS —— 启用 `allow-passthrough on` 后,tmux 内的 vim 可把 `\x1bP$qm\x1b\\` 传给外层终端,并把应答路由回来;内层编辑器看到的是*外层*终端的状态,而非 tmux 合成的(tmux 没有光标形状概念)
- Ghostty / WezTerm / xterm feature-detection scripts`terminal-capabilities.sh`、`vim-terminfo-probe` 等 shell 助手在会话开始时用 DECRQSS 走遍每条 SGR / DECSCUSR / DECSTBM 设置,把应答写入 `~/.cache/terminal-state.json`;下游程序读取该文件而非重新探测 —— 把吵闹的多轮往返握手变成一次缓存读
常见问题
针对这条序列,开发者真正会去搜索的问题的简短回答。
- 如何安全地探测终端当前背景色?
- 用 OSC 11 查询(
\x1b]11;?\x07)—— 返回背景色,形如rgb:RRRR/GGGG/BBBB。DECRQSS 本身查询 SGR / 模式状态,不查 RGB。查询/应答模式:写入查询,对 stdin 设 100–200 ms 读超时,解析应答(\x1b]11;rgb:1e1e/1e1e/1e1e\x07或以\x1b\\结束),再算亮度挑出与输出对比足够的前景色。不支持 OSC 11 的终端 —— 较老的 xterm、2021 前的 Apple Terminal.app、Linux console —— 会静默丢弃查询并触发超时;该情况下回落到$COLORFGBG(部分 shell / 终端会设)或假定暗主题。 - 哪些终端会响应 DECRQSS?
- xterm(首创者)、kitty、WezTerm、foot、Alacritty、Ghostty、Windows Terminal ≥ 1.21 都会对 DECRQSS 的标准子集作出应答:SGR(
\x1bP$qm\x1b\\)、光标形状(\x1bP$q q\x1b\\)、DECSTBM 滚动区域和少量 DEC 私有模式。Apple Terminal.app、gnome-terminal、konsole、Linux console / fbcon、screen 4.x *不*实现 DECRQSS —— 查询被静默丢弃。务必设读超时(标准 100–200 ms),并备好基于$TERM的硬编码回退表;切勿假定应答必至。
终端支持
- xterm
- 支持
- Linux console (fbcon)
- 不支持
- macOS Terminal.app
- 不支持
- iTerm2
- 部分
- Windows Terminal
- 部分
- cmd.exe / ConPTY
- 不支持
- kitty
- 支持
- alacritty
- 不支持
- WezTerm
- 支持
- Ghostty
- 支持
- GNOME Terminal
- 部分
- Konsole
- 部分
- tmux
- 部分
- GNU screen
- 不支持
| xterm | Linux console (fbcon) | macOS Terminal.app | iTerm2 | Windows Terminal | cmd.exe / ConPTY | kitty | alacritty | WezTerm | Ghostty | GNOME Terminal | Konsole | tmux | GNU screen |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 支持 | 不支持 | 不支持 | 部分 | 部分 | 不支持 | 支持 | 不支持 | 支持 | 支持 | 部分 | 部分 | 部分 | 不支持 |
相关序列
在家族食谱中
DCS 食谱 · 3. 向终端提问 —— DECRQSS、terminfo cap、DECRQTSR、DECRQUPSS、光标样式