# Erase in Display (ED)

> Erase part or all of the screen.

- **Sequence:** `CSI Ps J`
- **Defaults:** Ps = 0
- **ECMA-48:** [§8.3.39 ED – Erase in Page, p. 41](https://archive.org/details/ecma-48-5th-edition-june-1991/page/n54/mode/1up)
- **DEC STD 070:** [§5.11 Erase in Display, p. 5-156](https://archive.org/details/bitsavers_decstandar0VideoSystemsReferenceManualDec91_74264381/page/n410/mode/1up)
- **VT220:** [§4.12 Erasing](https://vt100.net/docs/vt220-rm/chapter4.html#S4.12)
- **VT510:** [ED—Erase in Display](https://vt100.net/docs/vt510-rm/ED.html)

Erase some of the screen, chosen by `Ps`:

| `Ps` | Erases |
| --- | --- |
| `0` | from the cursor to the end of the screen, the cursor's cell included |
| `1` | from the start of the screen to the cursor, the cursor's cell included |
| `2` | the whole screen |
| `3` | the scrollback, which is not on the screen (an xterm extension) |

The cursor does not move. Erasing ignores the margins; it is the screen that
is erased, not the scrolling region.

An erased cell takes the current background color, set with [SGR](https://control-codes.page/csi/sgr/index.html.md),
rather than the default. That is *background color erase*, and is why
programs reset SGR before clearing the screen.

## Support

| Terminal | Version | Support | Notes |
| --- | --- | --- | --- |
| VT220 | [manual](https://vt100.net/docs/vt220-rm/), 2nd ed., 1984 | Yes | [§4.12 Erasing](https://vt100.net/docs/vt220-rm/chapter4.html#S4.12); `0` to `2`, as DEC defined it; `3` is xterm's |
| VT510 | [manual](https://vt100.net/docs/vt510-rm/), 1st ed., 1993 | Yes | [ED](https://vt100.net/docs/vt510-rm/ED.html); `0` to `2`, as DEC defined it; `3` is xterm's |
| xterm | patch 412, [xterm-411a](https://github.com/ThomasDickey/xterm-snapshots/tree/xterm-411a) | Yes | `3` follows the `eraseSavedLines` resource, on by default |
| 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 |
| pyte | 0.8.2, [run as a library](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/pyte.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) | Partly | fails ED-2 of the 3 cases that show it working |
| ConEmu | build 230724, its [documentation](https://conemu.github.io/en/AnsiEscapeCodes.html) | Yes | its list has `2` also move the cursor to the upper-left corner; [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) | Yes | passes 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

### ED-1: Below

Input, one step per line:

```text
\e#8      # fill with E
\e[2;2H
\e[J
```

Expected screen:

```text
|EEEE|
|E___|
|____|
cursor 2,2
```

### ED-2: Above

Input, one step per line:

```text
\e#8      # fill with E
\e[2;2H
\e[1J
```

Expected screen:

```text
|____|
|__EE|
|EEEE|
cursor 2,2
```

### ED-3: Everything

Input, one step per line:

```text
\e#8      # fill with E
\e[2;2H
\e[2J
```

Expected screen:

```text
|____|
|____|
|____|
cursor 2,2
```

### ED-4: With a background color

Input, one step per line:

```text
\e[44m    # blue background
\e[2J
```

Expected screen:

```text
|____|
|____|
attr 1,1 bg=4
attr 2,4 bg=4
```

---

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