Skip to main content
ansicode

EL — Erase in line (\x1b[K)

Erase part or all of the current line.

Byte forms

Every common string-literal form so you can paste-and-search either direction.

\\x1b[\x1b[NK
\\033[\033[K
\\e[\e[K
ESC [ESC [ N K
hex1b 5b <N> 4b

Description

Final byte K. N = 0 erases from the cursor to the end of the line (the default — \x1b[K and \x1b[0K are equivalent), N = 1 erases from the start of the line up to and including the cursor, N = 2 erases the entire current line. The cursor itself is not moved by EL — emit \r (or \x1b[1G, CHA column 1) before \x1b[2K if you want both "erase the line and put the cursor at column 1". The erased cells are filled with the current background color, so a non-default sgr-bg-* will bleed across erased regions; that is the color-bleed pitfall under /pitfalls.

Universally supported — the support matrix above shows yes across every emulator including Linux console / fbcon and ConPTY — and has no major portability gotchas. The recurring trap is the cursor-stay-put behaviour: newcomers expect \x1b[2K to also home the cursor, then write their replacement text and end up appending to the right of the still-visible old text. The canonical in-place-progress idiom is \r\x1b[K not \x1b[2K, because \r\x1b[K (carriage return + erase to EOL) is one byte shorter, doesn't touch the part of the line you haven't yet rewritten, and cleanly handles the case where the next progress message is shorter than the previous one. Tools like git's progress bars, npm output, curl -#, and the busybox wget progress line all use this pattern. Background-color caveat: if you've set a non-default sgr-bg-* and then EL, the erased region fills with that bg color; \x1b[K\x1b[49m (erase, then reset bg) avoids the bleed.

There is no dedicated "close" — EL is a one-shot erase, not a toggle. The safe pattern for rebuilding a line is \r\x1b[K<new content>\n (CR + erase-to-EOL + write + LF) which survives short replacements, terminal resize, and tmux pane changes. For multi-line redraws combine cursor-move (CUU up N lines) with EL on each line and then move back. The complement on the vertical axis is erase-display (\x1b[J) — erase from cursor to end of screen / start of screen / whole screen. The terminfo caps for EL are el / el1 / el2 (clr_eol / clr_bol / clr_line); use tput el rather than the raw escape when writing shell scripts targeting an unknown $TERM.

Spec citation: ECMA-48 §8.3.41 (EL)

Examples

bash
printf 'progress... \r\033[Kdone\n'
python
import sys, time\nfor i in range(3): sys.stdout.write(f'\r\x1b[K{i}'); sys.stdout.flush(); time.sleep(0.2)\nprint()
go
fmt.Print("\r\x1b[Kclear line\n")
javascript
process.stdout.write('\r\x1b[Kclear line\n')
c
printf("\r\x1b[Kclear line\n");

Used in

Real-world tools that emit this sequence — anchors the bytes to commands you've already used.

  • bash, zsh readlinewipe prompt artefacts before repainting on every keystroke
  • npm, pnpm, yarn progressEL before each progress redraw so the new line replaces the old one
  • git clone, git fetch progress
  • ora, cli-spinners (Node)
  • fzf incremental query redraw

Frequently asked

Short answers to the questions developers actually search for this sequence.

What's the difference between \x1b[K and \x1b[2K?
\x1b[K (same as \x1b[0K) erases from cursor to end of line. \x1b[2K erases the entire line regardless of cursor position. \x1b[1K erases from start of line to cursor. None of them move the cursor — so a typical progress-bar update is \r\x1b[K<new line>: carriage-return to column 1, erase tail, print fresh content.
Why does my progress bar leave garbage on the right when the new line is shorter?
You wrote \r<new line> without erasing — the new line only overwrites as many columns as it has characters, so any trailing characters from the previous (longer) line stay on screen. The fix is \r\x1b[K<new line> — the EL erases everything to the right of the cursor first.

Terminal support

xterm
yes
Linux console (fbcon)
yes
macOS Terminal.app
yes
iTerm2
yes
Windows Terminal
yes
cmd.exe / ConPTY
yes
kitty
yes
alacritty
yes
WezTerm
yes
Ghostty
yes
GNOME Terminal
yes
Konsole
yes
tmux
yes
GNU screen
yes

Related sequences

In the family cookbook

CSI cookbook · 3. Erase — ED `J` and EL `K`