# Set Palette, the Linux Console Way (OSC P)

> Change a palette color, as the Linux console does.

- **Sequence:** `OSC P n rrggbb 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)
- **Specification:** [console_codes(4), Linux manual pages](https://man7.org/linux/man-pages/man4/console_codes.4.html)

`OSC P n rrggbb ST` sets color `n`, one hex digit from `0` to `f`, to the
color `rrggbb`. It is the Linux console's, which has only sixteen colors;
[OSC 4](https://control-codes.page/osc/palette/index.html.md) does the same for the 256 of the palette, and
[OSC R](https://control-codes.page/osc/linux-reset-palette/index.html.md) resets them. iTerm2 also accepts it,
though its [Proprietary Escape Codes](https://iterm2.com/documentation-escape-codes.html) page advises against it, and
extends `n` past `f` to its other colors: `g` and `h` for the default
foreground and background, `i` for bold, `j` and `k` for the selection, `l`
and `m` for the cursor.

The Linux console's own definition, in
[`console_codes(4)`](https://man7.org/linux/man-pages/man4/console_codes.4.html), has no terminator: `ESC ] P` takes exactly
seven hex digits after it, and `ESC ] R` none at all. Every other terminal
reads them as OSCs, which run until ST or BEL, so sent the console's way, one
of these swallows whatever follows it until something ends the string.

A default xterm ignores both: a letter it does not know where the number
should be makes the sequence malformed
([`do_osc`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/misc.c#L4013-L4041)). libghostty-vt ignores them too.

## Validation

### OSC-P-1: It does not change a color

Input, one step per line:

```text
\e]P1ff0000\e\\
\e]4;1;?\a
```

Expected screen:

```text
|______|
reply \e]4;1;rgb:cdcd/0000/0000\a
```

### OSC-P-2: Nor iTerm2's foreground

Input, one step per line:

```text
\e]Pg4040ff\e\\  # the foreground, iTerm2's way
\e]10;?\a
```

Expected screen:

```text
|______|
reply \e]10;rgb:0000/0000/0000\a
```

### OSC-P-3: Without a terminator, it swallows what follows

Input, one step per line:

```text
\e]P1ff0000  # the console's way
AB
```

Expected screen:

```text
|______|
```

---

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