# Clipboard (OSC 52)

> Let a program set or read the clipboard, even over ssh.

- **Sequence:** `OSC 52 ; Pc ; Pd ST`
- **ECMA-48:** [§8.3.89 OSC – Operating System Command, p. 51](https://archive.org/details/ecma-48-5th-edition-june-1991/page/n64/mode/1up)
- **xterm:** [Operating System Commands](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)

`OSC 52 ; Pc ; Pd ST` puts `Pd`, which is base64, on the clipboard. `Pc`
names which: `c` the clipboard, `p` the primary selection, `q` the
secondary, `s` the selection xterm is configured to use, `0` to `7` X's cut
buffers; left empty, it means `s 0`. `Pd` of `?` asks for the contents
instead, and the answer is `OSC 52 ; Pc ; base64 ST`. Anything else that is
not base64 clears it. Because it travels in the terminal's own data stream,
it works where nothing else can reach the clipboard, from a program on the
far side of an ssh connection.

It is xterm's, and a default xterm does not do it. Reading the clipboard
lets any program that writes to the terminal see whatever was last copied,
and writing it lets one replace what you are about to paste, so both are
among the *window operations* that are off unless the `allowWindowOps`
resource is set: `SetSelection` and `GetSelection` are in the default
`disallowedWindowOps`
([`main.h`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/main.h#L150-L175),
[`ManipulateSelectionData`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/misc.c#L3320-L3421)).
Such a terminal consumes the sequence and does nothing.

libghostty-vt parses the sequence and hands it to its host through the
`CLIPBOARD_WRITE` and `CLIPBOARD_READ` callbacks; with none installed, it
does nothing either. Whether to install them, and whether to ask the person
at the keyboard first, is the host's choice, as allowing them is in xterm.
The runner installs neither, matching a default xterm, so the cases pass.
Ghostty itself has a `clipboard-read` and a `clipboard-write` setting for
the same choice.

## Validation

### OSC52-1: Setting the clipboard changes nothing on screen

Input, one step per line:

```text
A
\e]52;c;SGVsbG8=\a   # "Hello"
B
```

Expected screen:

```text
|AB____|
cursor 1,3
reply none
```

### OSC52-2: A question is not answered

Input, one step per line:

```text
\e]52;c;?\a
\e]52;;?\e\\
```

Expected screen:

```text
|______|
reply none
```

---

This is the Markdown version of <https://control-codes.page/osc/clipboard/>. 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>.
