# Carriage Return (CR)

> Move the cursor to the left margin of the current line.

- **Sequence:** `CR`
- **ECMA-48:** [§8.3.15 CR – Carriage Return, p. 35](https://archive.org/details/ecma-48-5th-edition-june-1991/page/n48/mode/1up)
- **DEC STD 070:** [§5.4.4 Carriage Return, p. 5-58](https://archive.org/details/bitsavers_decstandar0VideoSystemsReferenceManualDec91_74264381/page/n312/mode/1up)
- **VT220:** [§4.2 Control Characters](https://vt100.net/docs/vt220-rm/chapter4.html#S4.2)
- **VT510:** [4.2 Control Characters](https://vt100.net/docs/vt510-rm/chapter4.html#S4.2)

Move the cursor to the left margin of the line it is on. With no left margin
set that is the first column. Nothing is erased, which is how a progress bar
redraws itself in place.

With left and right margins enabled (`CSI ? 69 h`), the cursor goes to the
left margin if it is at or right of it. A cursor already left of the left
margin goes to the first column instead.

A carriage return cancels any pending wrap, as DEC STD 070 asks
([§D.6.1, p. D-14](https://archive.org/details/bitsavers_decstandar0VideoSystemsReferenceManualDec91_74264381/page/n1200/mode/1up)) and xterm's
[`CarriageReturn`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/cursor.c#L345-L369) does: the next character is written
at the start of this line, not the next one.

## Support

| Terminal | Version | Support | Notes |
| --- | --- | --- | --- |
| VT220 | [manual](https://vt100.net/docs/vt220-rm/), 2nd ed., 1984 | Yes | [§4.2 Control Characters](https://vt100.net/docs/vt220-rm/chapter4.html#S4.2) |
| VT510 | [manual](https://vt100.net/docs/vt510-rm/), 1st ed., 1993 | Yes | [Table 4–1](https://vt100.net/docs/vt510-rm/chapter4.html#T4-1) |
| xterm | patch 412, [xterm-411a](https://github.com/ThomasDickey/xterm-snapshots/tree/xterm-411a) | Yes |  |
| 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) | Partly | fails CR-3 of the 4 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) | Partly | fails CR-3 of the 4 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) | Partly | fails CR-3 of the 4 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) | Partly | fails CR-3 of the 4 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 CR-3 of the 4 cases that show it working |
| rxvt-unicode | 9.31, [run in a VM](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/urxvt.json) | Partly | fails CR-3 of the 4 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 4 cases that show it working |

## Validation

### CR-1: Back to the start

Input, one step per line:

```text
ABC
\r
X
```

Expected screen:

```text
|XBC__|
cursor 1,2
```

### CR-2: Cancels a pending wrap

Input, one step per line:

```text
ABCDE     # fills the line; a wrap is pending
\r
X
```

Expected screen:

```text
|XBCDE|
|_____|
cursor 1,2
```

### CR-3: To the left margin

Input, one step per line:

```text
\e[?69h   # allow left and right margins
\e[3;5s   # margins at columns 3 and 5
\e[1;4H
\r
X
```

Expected screen:

```text
|__X__|
```

### CR-4: Left of the left margin

Input, one step per line:

```text
\e[?69h   # allow left and right margins
\e[3;5s   # margins at columns 3 and 5
\e[1;2H
\r
X
```

Expected screen:

```text
|X____|
```

---

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