跳到主要内容
ansicode

XTMODKEYS — 修改键盘上报模式(CSI > Pp ; Pv m)

运行时切换 xterm 的 modifyKeyboard / modifyCursorKeys / modifyFunctionKeys / modifyOtherKeys 资源 —— 是 Ctrl+字母、Alt+字母 等键位精准上报的基础。

字节形式

涵盖所有常见的字符串字面量写法,方便正反查找。

\\x1b[\x1b[><Pp>;<Pv>m
\\033[\033[>4;2m
\\e[\e[>4;2m
ESC [ESC [ > Pp ; Pv m
hex1b 5b 3e ... 6d

说明

前缀 `>` 把末字节 `m` 从 SGR(设置图形属性)切换为 XTMODKEYS —— 一个运行时可改的视图,用于查看 / 修改 xterm 控制带修饰键上报方式的四个资源。首个参数 `Pp` 选择资源(`0` = modifyKeyboard,`1` = modifyCursorKeys,`2` = modifyFunctionKeys,`3` = modifyKeypadKeys,保留;`4` = modifyOtherKeys),`Pv` 设置取值。最有用的是 **modifyOtherKeys**(`Pp=4`):`Pv=0` 时 xterm 把 Ctrl+字母只上报为对应的 C0 字节(Ctrl+I === Tab、Ctrl+M === Enter);`Pv=1` 时把无歧义键的修饰组合以 CSI 27 ~ 上报;`Pv=2` 时上报**每一个**带修饰的可打印键 —— 让 vim / emacs / kakoune 之类的编辑器能区分 Ctrl+I 与 Tab、Ctrl+H 与 Backspace,并直接绑定 Alt+字母而不必再依赖传统的 ESC-前缀技巧。重置用 `\x1b[>Pp m`(省略 `Pv` 恢复默认)或 `\x1b[>4;0m`(关闭 modifyOtherKeys)。kitty 自家的 CSI u 键盘协议(另一种序列)功能更全,新代码建议优先采用;XTMODKEYS 则是数百万既有 xterm 风格 TUI 的事实兼容路径。

规范出处: xterm-ctlseqs (XTMODKEYS)

参数

Pp资源选择:`0` modifyKeyboard、`1` modifyCursorKeys、`2` modifyFunctionKeys、`4` modifyOtherKeys。
Pv资源的新取值。modifyOtherKeys 时:`0` 关闭、`1` 仅无歧义时上报修饰、`2` 始终上报修饰。

示例

bash
# Enable full modifyOtherKeys — every Ctrl+letter reports distinctly:\nprintf '\033[>4;2m'\n# (run from inside a TUI that handles CSI 27 ~ replies; raw shells will show garbage)
python
import sys\nsys.stdout.write('\x1b[>4;2m')   # enable modifyOtherKeys 2\n# remember to send '\x1b[>4;0m' on exit to restore default
go
fmt.Print("\x1b[>4;1m")   // modifyOtherKeys mode 1
javascript
process.stdout.write('\x1b[>4;2m')   // enable full modify-other-keys
c
printf("\x1b[>4;2m");   /* opt in to fully-modified key reports */\natexit_send("\x1b[>4;0m");  /* opt out at exit */

终端支持

xterm
支持
Linux console (fbcon)
不支持
macOS Terminal.app
不支持
iTerm2
支持
Windows Terminal
支持
cmd.exe / ConPTY
不支持
kitty
支持
alacritty
支持
WezTerm
支持
Ghostty
支持
GNOME Terminal
部分
Konsole
支持
tmux
不支持
GNU screen
不支持

相关序列