# Reverse Index (RI)

> Move the cursor up one line, scrolling down at the top margin.

- **Sequence:** `ESC M`
- **ECMA-48:** [§8.3.104 RI – Reverse Line Feed, p. 55](https://archive.org/details/ecma-48-5th-edition-june-1991/page/n68/mode/1up)
- **DEC STD 070:** [§5.4.3 Reverse Index, p. 5-36](https://archive.org/details/bitsavers_decstandar0VideoSystemsReferenceManualDec91_74264381/page/n290/mode/1up)
- **VT510:** [4.2 Control Characters](https://vt100.net/docs/vt510-rm/chapter4.html#S4.2)

The mirror image of a [line feed](https://control-codes.page/c0/lf/index.html.md): move the cursor up one line,
keeping its column. If the cursor is on the top margin, the lines of the
scrolling region move down by one instead: the bottom line leaves the region,
and a blank line appears at the top.

This is how a full-screen program scrolls backwards, a line at a time,
without redrawing what is already on the screen.

## Validation

### RI-1: Up one line

Input, one step per line:

```text
\e[3;2H
\eM
X
```

Expected screen:

```text
|_____|
|_X___|
|_____|
```

### RI-2: Scrolls down at the top

Input, one step per line:

```text
A\r\nB\r\nC
\e[1;1H
\eM
X
```

Expected screen:

```text
|X____|
|A____|
|B____|
```

### RI-3: Within a scrolling region

Input, one step per line:

```text
1\r\n2\r\n3\r\n4
\e[2;3r   # region is lines 2 and 3; homes the cursor
\e[2;1H
\eM
X
```

Expected screen:

```text
|1____|
|X____|
|2____|
|4____|
```

---

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