# 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)
- **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.

## 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>.
