# Cursor Backward (CUB)

> Move the cursor left n columns.

- **Sequence:** `CSI Pn D`
- **Defaults:** Pn = 1
- **ECMA-48:** [§8.3.18 CUB – Cursor Left, p. 36](https://archive.org/details/ecma-48-5th-edition-june-1991/page/n49/mode/1up)
- **DEC STD 070:** [§5.4.4 Cursor Backward, p. 5-47](https://archive.org/details/bitsavers_decstandar0VideoSystemsReferenceManualDec91_74264381/page/n301/mode/1up)
- **VT220:** [§4.7 Cursor Positioning](https://vt100.net/docs/vt220-rm/chapter4.html#S4.7)
- **VT510:** [CUB—Cursor Backward](https://vt100.net/docs/vt510-rm/CUB.html)

Move the cursor `Pn` columns to the left. A `0` is taken as `1`. The cursor
stops at the left margin if it started at or right of it, and at the first
column otherwise. It never moves to another line.

If a wrap is pending, the wrap is cancelled, as DEC STD 070 asks
([§D.6.1, p. D-13](https://archive.org/details/bitsavers_decstandar0VideoSystemsReferenceManualDec91_74264381/page/n1199/mode/1up)) and xterm's
[`CursorBack`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/cursor.c#L121-L204) does, and the cursor moves from the last
column, so `CSI D` after filling a line leaves the cursor on the second-last
column. (With xterm's reverse wraparound mode on, the pending wrap instead
uses up one of the `Pn` columns; that mode is off by default.)

The other three directions are [CUU](https://control-codes.page/csi/cuu/index.html.md) (`A`, up), [CUD](https://control-codes.page/csi/cud/index.html.md) (`B`, down) and [CUF](https://control-codes.page/csi/cuf/index.html.md)
(`C`, right), and they work the same way.

## Validation

### CUB-1: Left n columns

Input, one step per line:

```text
ABCD
\e[3D
X
```

Expected screen:

```text
|AXCD_|
```

### CUB-2: Stops at the first column

Input, one step per line:

```text
ABC
\e[99D
X
```

Expected screen:

```text
|XBC__|
```

### CUB-3: From a pending wrap

Input, one step per line:

```text
ABCDE     # fills the line; a wrap is pending
\e[D
X
```

Expected screen:

```text
|ABCXE|
|_____|
```

### CUB-4: Stops at the left margin

*A known difference: libghostty-vt does not pass this case.*

Input, one step per line:

```text
\e[?69h   # allow left and right margins
\e[2;4s   # margins at columns 2 and 4
\e[1;4H
\e[9D
X
```

Expected screen:

```text
|_X___|
```

libghostty-vt stops at the first column rather than the left margin. With
reverse wraparound off, which is the default, it clamps the move to column 1
and does not look at the margins; only its reverse-wraparound path does.
DEC STD 070 is explicit that the cursor "will not move beyond the Left
Margin" when it starts at or inside it, xterm stops there, and esctest2's
`test_CUB_StopsAtLeftMarginInScrollRegion` checks for it.

---

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