# Select Graphic Rendition (SGR)

> Set the colors and attributes of the characters written after it.

- **Sequence:** `CSI Pm m`
- **Defaults:** Pm = 0
- **ECMA-48:** [§8.3.117 SGR – Select Graphic Rendition, p. 61](https://archive.org/details/ecma-48-5th-edition-june-1991/page/n74/mode/1up)
- **DEC STD 070:** [§5.6.2 Select Graphic Rendition, p. 5-103](https://archive.org/details/bitsavers_decstandar0VideoSystemsReferenceManualDec91_74264381/page/n357/mode/1up)
- **VT510:** [SGR—Select Graphic Rendition](https://vt100.net/docs/vt510-rm/SGR.html)

Set how the characters written from now on look. `Pm` is a list of
parameters, applied in order, so one SGR can set several things at once.
`CSI m` with no parameters is `CSI 0 m`, which resets everything.

| Parameter | Sets | Undone by |
| --- | --- | --- |
| `1` | bold | `22` |
| `2` | faint | `22` |
| `3` | italic | `23` |
| `4` | underline | `24` |
| `5` | blink | `25` |
| `7` | inverse | `27` |
| `8` | invisible | `28` |
| `9` | strikethrough | `29` |
| `30`–`37`, `90`–`97` | foreground, from the 16 basic colors | `39` |
| `40`–`47`, `100`–`107` | background, likewise | `49` |
| `38;5;N`, `48;5;N` | color `N` of the 256-color palette | `39`, `49` |
| `38;2;R;G;B`, `48;2;R;G;B` | a direct RGB color | `39`, `49` |

The 256-color and direct forms come from ITU T.416, which separates their
parts with colons: `38:2::R:G:B`, with an empty field for a color space
nobody uses. Terminals accept both. Colons also carry underline styles, as
`4:2` double, `4:3` curly, `4:4` dotted and `4:5` dashed.

Attributes belong to the characters written, not to the cells: moving the
cursor does not change them, and writing over a cell replaces its attributes
with the current ones.

## Validation

### SGR-1: Bold and a color

Input, one step per line:

```text
\e[1;31m
A
\e[0m
B
```

Expected screen:

```text
|AB___|
attr 1,1 bold fg=1
attr 1,2 plain
```

### SGR-2: Undoing one attribute

Input, one step per line:

```text
\e[1;3;4m # bold, italic, underline
A
\e[23m    # not italic
B
```

Expected screen:

```text
|AB___|
attr 1,1 bold italic underline
attr 1,2 bold -italic underline
```

### SGR-3: 256 colors and direct color

Input, one step per line:

```text
\e[38;5;208m
A
\e[38;2;90;180;250m
B
\e[48:2::200:100:0m
C
```

Expected screen:

```text
|ABC__|
attr 1,1 fg=208
attr 1,2 fg=#5ab4fa
attr 1,3 fg=#5ab4fa bg=#c86400
```

### SGR-4: Underline styles

Input, one step per line:

```text
\e[4:3m
A
\e[4:2m
B
\e[24m
C
```

Expected screen:

```text
|ABC__|
attr 1,1 underline=curly
attr 1,2 underline=double
attr 1,3 -underline
```

### SGR-5: Inverse

Input, one step per line:

```text
\e[32;7m
A
```

Expected screen:

```text
|A____|
attr 1,1 inverse fg=2
```

---

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