ECH — Erase Character (CSI Pn X)
Erase N cells in place at the cursor — same shape as DCH but the rest of the line does NOT shift left.
Byte forms
Every common string-literal form so you can paste-and-search either direction.
\x1b[PnX\033[PnX\e[PnXESC [ Pn X1b 5b <Pn> 58Description
Erase Character. Final byte X (0x58) replaces Pn cells starting at the cursor (default 1) with blank (SP, 0x20) cells carrying the current SGR background. The cursor does not move and the rest of the line is NOT shifted — this is the key distinction from DCH (\x1b[Pn P). Used when an app wants to clear a fixed-width field (input prompt, table cell, progress bar segment) without affecting surrounding content. ECH is bounded by the right margin (DECSTBM column-margins, if active); cells past the right margin are not touched. Terminfo cap: ech (parameterised; no ech1 because the no-shift semantics make the parameterised form universally needed).
Spec citation: ECMA-48 §8.3.38 (ECH)
Examples
# Print a 10-char field, position at col 1, erase 5 chars in place (rest of line preserved).\nprintf '0123456789\033[1G\033[5X'import sys; sys.stdout.write('\x1b[5X') # clear 5 cells in placefmt.Print("\x1b[5X")process.stdout.write('\x1b[5X')printf("\x1b[5X");Used in
Real-world tools that emit this sequence — anchors the bytes to commands you've already used.
- vim / neovim `:redraw!` per-cell repaintWhen `'lazyredraw'` is off, vim's screen repaint emits ECH for spans where the new cell content is blank with the same SGR background as the prior row — cheaper than DCH because the line width doesn't change so the right edge doesn't need a redraw pass
- bash readline prompt redraw on history-backbash readline emits ECH for the prior prompt's trailing width during `Up`-arrow history recall when the new line is the same length, avoiding the `\x1b[K` full-EOL clear cost — visible in `bind -p | grep delete-char` traces and the `READLINE_DEBUG` log
- htop column-cell updatehtop redraws CPU / memory bar fills using ECH per cell rather than the whole row — cells whose value didn't change keep their prior render; cells whose value changed get a one-shot ECH+rewrite, keeping the per-frame byte cost proportional to the changed-cell count
- less / more status-line update on file-position changeless ≥ 600 redraws its bottom-line `:` status using ECH for the byte-count and percent-loaded slots when the file scrolls — keeps the status redraw under 30 bytes per keystroke vs ~80 for a `\x1b[K` + full reprint
- terminfo `ech` capability (`ech1` is intentionally absent)The `ech` cap is parameterised in every modern terminfo entry — there is no `ech1` shortcut because the no-shift semantics make the count argument universally needed; `tput ech 5` resolves to `\x1b[5X` on `xterm-256color` and `tmux-256color`
Frequently asked
Short answers to the questions developers actually search for this sequence.
- What's the difference between ECH (
\x1b[5X) and DCH (\x1b[5P)? - ECH (Erase Character — final byte
X, 0x58) *overwrites*Pncells starting at the cursor with SP, leaving the row's overall width unchanged. DCH (Delete Character — final byteP, 0x50) *removes*Pncells and shifts the trailing row content left byPn. Pick ECH when you need to clear a fixed-width field (input prompt, table cell, progress-bar segment) without disturbing what's after it; pick DCH when the row content past the cursor should genuinely move left (e.g. adelete-charin a line editor). Both leave the cursor in place; both honour the current SGR background colour for the blanked cells. - Why doesn't
\x1b[K(EL) cover what ECH does? - EL (Erase in Line) clears *to end of line*, *from start of line*, or the *whole row* depending on the mode byte (
0/1/2) — it has no fixed-width count. ECH clears exactlyPncells. Use EL when redrawing a prompt and you want any leftover tail ("old prompt was longer than new prompt") gone; use ECH when you have a precise width to wipe without affecting what follows. The common idiom in prompt redrawing is ECH the known-width prefix, then\x1b[Kas a tail safety in case the new content is shorter than the previous render.
Terminal support
- xterm
- yes
- Linux console (fbcon)
- yes
- macOS Terminal.app
- yes
- iTerm2
- yes
- Windows Terminal
- yes
- cmd.exe / ConPTY
- partial
- kitty
- yes
- alacritty
- yes
- WezTerm
- yes
- Ghostty
- yes
- GNOME Terminal
- yes
- Konsole
- yes
- tmux
- yes
- GNU screen
- yes
| xterm | Linux console (fbcon) | macOS Terminal.app | iTerm2 | Windows Terminal | cmd.exe / ConPTY | kitty | alacritty | WezTerm | Ghostty | GNOME Terminal | Konsole | tmux | GNU screen |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| yes | yes | yes | yes | yes | partial | yes | yes | yes | yes | yes | yes | yes | yes |
Related sequences
In the family cookbook
CSI cookbook · 6. Insert / delete / cursor shape — IL / DL / ICH / DCH / ECH + DECSCUSR