# Bracketed Paste (?2004)

> Mark pasted text, so that a program can tell it from typing.

- **Sequence:** `CSI ? 2004 h/l`
- **xterm:** [Bracketed Paste Mode](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)

With `CSI ? 2004 h` set, the terminal puts `CSI 200 ~` before anything
pasted into it and `CSI 201 ~` after, so a program can tell a paste from
typing: a shell need not run each pasted line as it arrives, and an editor
need not auto-indent pasted code. `CSI ? 2004 l` stops it. It is xterm's
([`dpmodes`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/charproc.c#L7955-L7957),
[the paste itself](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/button.c#L2835-L2837)).

The brackets are only as safe as the paste between them: a pasted
`CSI 201 ~` would end the bracket early. A default xterm removes ESC from
a paste before sending it, along with BS, DEL, ENQ, EOT, NUL and the
characters the terminal driver treats specially, unless its
`allowPasteControls` resource is set
([`removeControls`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/button.c#L2582-L2692),
[`DEF_DISALLOWED_PASTE_CONTROLS`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/main.h#L142-L144)).

It is off to begin with, [DECSTR](https://control-codes.page/csi/decstr/index.html.md) leaves it alone, and [RIS](https://control-codes.page/esc/ris/index.html.md)
turns it off
([`ReallyReset`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/charproc.c#L14523-L14525)).
A paste comes from the window, not from the data a case writes, so the cases
check what DECRQM says. libghostty-vt passes them.

## Support

| Terminal | Version | Support | Notes |
| --- | --- | --- | --- |
| VT220 | [manual](https://vt100.net/docs/vt220-rm/), 2nd ed., 1984 | No | not in the manual |
| VT510 | [manual](https://vt100.net/docs/vt510-rm/), 1st ed., 1993 | No | not in the manual; not in [Table 5-6](https://vt100.net/docs/vt510-rm/DECRPM.html) |
| xterm | patch 412, [xterm-411a](https://github.com/ThomasDickey/xterm-snapshots/tree/xterm-411a) | Yes |  |
| libghostty-vt | [0f171f6](https://github.com/ghostty-org/ghostty/tree/0f171f650a1cbbd6df4a2573b6b0c7de57cc6d90) | Yes |  |
| kitty | 0.49.1, [run in a VM](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/kitty.json) | Yes | passes the 3 cases that show it working |
| foot | 1.28.0, [run in a VM](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/foot.json) | Yes | passes the 3 cases that show it working |
| xterm.js headless | 6.0.0, [run as a library](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/xtermjs.json) | Yes | passes the 3 cases that show it working |
| alacritty_terminal | 0.26.0, [run as a library](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/alacritty.json) | Yes | passes the 3 cases that show it working |
| ConEmu | build 230724, its [documentation](https://conemu.github.io/en/AnsiEscapeCodes.html) | Yes | [ANSI escape codes](https://conemu.github.io/en/AnsiEscapeCodes.html) |
| rxvt-unicode | 9.31, [run in a VM](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/urxvt.json) | Partly | fails PASTE-4 of the 3 cases that show it working |
| VTE | 0.84.1, [run as a library](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/vte.json) | Yes | passes the 3 cases that show it working |

## Validation

### PASTE-1: Off to begin with

Input, one step per line:

```text
\e[?2004$p
```

Expected screen:

```text
|____|
reply \e[?2004;2$y
```

### PASTE-2: Set and reset

Input, one step per line:

```text
\e[?2004h
\e[?2004$p
\e[?2004l
\e[?2004$p
```

Expected screen:

```text
|____|
reply \e[?2004;1$y\e[?2004;2$y
```

### PASTE-3: DECSTR leaves it on

Input, one step per line:

```text
\e[?2004h
\e[!p
\e[?2004$p
```

Expected screen:

```text
|____|
reply \e[?2004;1$y
```

### PASTE-4: RIS turns it off

Input, one step per line:

```text
\e[?2004h
\ec
\e[?2004$p
```

Expected screen:

```text
|____|
reply \e[?2004;2$y
```

---

This is the Markdown version of <https://control-codes.page/modes/paste/>. On that page every validation case runs live in libghostty-vt, the terminal emulation core of Ghostty, compiled to WebAssembly.

The validation cases are written in the notation described in <https://control-codes.page/notation/index.html.md>.
