跳到主要内容
ansicode

CSI Ps i —— 媒体复制(MC)—— 启用打印 / 关闭打印 / 打印行

ECMA-48 §8.3.82 控制,历史上把终端输出转给所连打印机。今日仍存于 xterm/mlterm 的「打印屏幕」钩子、IBM-3270 模拟器透传,以及私有模式 `CSI ? 4 / 5 i` 自动打印开关(被 `expect` 风格自动化使用)。

字节形式

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

\\x1b[\x1b[0i / \x1b[4i / \x1b[5i / \x1b[?4i / \x1b[?5i
\\033[\033[5i (printer-on) \033[4i (printer-off)
\\e[\e[5i / \e[4i
ESC [ESC [ Ps i (Ps ∈ {0,1,4,5}) / ESC [ ? Ps i
hex1b 5b 35 69 (printer-on) / 1b 5b 34 69 (printer-off)

说明

**Media Copy**(MC)是 ECMA-48 把终端输出分流到次级流的机制 —— 历史上是所连的行式打印机或串口纸式打印机。标准参数: - `\x1b[0i`(或单写 `\x1b[i`)—— **打印当前页**。把当前屏内容作为一次性向打印机发送,由终端处理分页(换页、页尾)。xterm 通过 X 资源 `printerCommand` 指定的程序(通常是 `lpr`)执行。 - `\x1b[1i` —— **打印当前行**。`0i` 的单行版。 - `\x1b[4i` —— **关闭打印**(Media Copy 'deselect')。停止 `5i` 启动的流分发。其后输出仅去屏幕。 - `\x1b[5i` —— **开启打印**(Media Copy 'select')。开始把后续所有终端输出分发到打印机,直至匹配的 `4i`。常称*透明打印模式*。此期间屏幕无输出。 **DEC 私有前缀变体**(`\x1b[?Ps i`): - `\x1b[?1i` —— 打印光标所在行(单行,无换页)。 - `\x1b[?4i` —— 关 **自动打印**。 - `\x1b[?5i` —— 开 **自动打印**。每当光标越过右边距或 `\n` 触发换行时,该行也被发到打印机。 - `\x1b[?10i` / `\x1b[?11i` —— 打印合成显示(VT300+);打印所有字符包括收到的控制。 **2026 年为什么仍要紧** - **IBM 3270 / 5250 模拟器栈**(`x3270`、`wc3270`、`tn5270`)让 MC 透过宿主应用到达真实或虚拟 `LPT` 设备。老旧主机批处理仍预期 `\x1b[5i…\x1b[4i` 包裹打印数据。 - **xterm** + **mlterm** 在 X 资源设置了 `printerCommand` 时尊重 `0i` 与 `5i`/`4i` —— 现代等价为 `printerCommand: cat > /tmp/xterm.dump`。可用作屏幕捕获 / 调试而无需 `tmux capture-pane`。 - **`expect`** 对老系统跑脚本时用 `\x1b[5i` … `\x1b[4i` 作「捕获此块」标记,因为几乎没现代模拟器实现分发,字节会原样送到屏幕、被 expect 的模式匹配器看到。 **覆盖度**今日多为**静默**:**xterm** + **mlterm** = 完整(受 `printerCommand` 资源约束)。**Konsole** = 部分(解析,未显式接打印机时静默)。**Linux console** = 部分(连了 `lp` 守护时 `5i` / `4i` 工作)。**WezTerm** + **Kitty** + **Alacritty** + **iTerm2** + **gnome-terminal** + **Ghostty** + **Windows Terminal** + **cmd / ConPTY** + **macOS Terminal** = 静默(字节穿过解析器不生效)。除了 xterm + mlterm + 3270 路径外,不要依赖分发。

规范出处: ECMA-48 §8.3.82 (MC) / xterm-ctlseqs (CSI Ps i, CSI ? Ps i)

参数

Ps = 0打印当前页 —— 把当前屏发到打印机(一次性,含换页)。
Ps = 1打印当前行 —— 单行变体。
Ps = 4关闭打印(取消选择 Media Copy 流)。
Ps = 5开启打印(选择 Media Copy 流 —— 透明打印模式)。
? Ps = 1打印光标所在行(DEC,单行,无换页)。
? Ps = 4 / 5自动打印 关 / 开 —— 每次光标换行也送给打印机。

示例

bash
# xterm with printerCommand='cat > /tmp/screen.dump' — capture current screen.\nprintf '\\033[0i'\n# Bracketed print: every byte between 5i and 4i goes to the printer.\nprintf '\\033[5i'; cat report.txt; printf '\\033[4i'
python
import sys\n# expect-style capture marker — emits visible bytes on non-xterm.\nsys.stdout.write('\\x1b[5i')\nsys.stdout.flush()\n# (expect script greps for the literal bytes; xterm diverts them silently)
go
// Auto-print every line as it scrolls (DEC private).\nfmt.Print(\"\\x1b[?5i\")     // auto-print on\ndefer fmt.Print(\"\\x1b[?4i\")  // auto-print off on exit
javascript
// 3270 emulator print envelope around report data.\nprocess.stdout.write('\\x1b[5i' + reportData + '\\x1b[4i');
c
/* Print cursor line, no form-feed. */\nprintf(\"\\x1b[?1i\");\nfflush(stdout);

终端支持

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

相关序列