Skip to main content
ansicode

ED — Erase in display (\x1b[2J clear screen)

Erase part or all of the screen.

Byte forms

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

\\x1b[\x1b[NJ
\\033[\033[2J
\\e[\e[2J
ESC [ESC [ N J
hex1b 5b <N> 4a

Description

Erase in Display. Final byte J. Parameter N controls scope: 0 = cursor → end of screen (default), 1 = beginning of screen → cursor (inclusive), 2 = entire visible screen, 3 = entire screen + scrollback buffer (xterm extension since v333, 2006). The cursor itself does NOT move — ED erases cells in place. The canonical 'clear screen' idiom is \x1b[2J\x1b[H: erase + home in one buffer flush. ECMA-48 §8.3.39 specifies parameters 0–2; the 3 value (scrollback erase) is xterm-private and what GNU clear -x emits when the terminfo entry advertises the E3 capability (modern xterm-256color, kitty, alacritty, wezterm, ghostty, gnome-terminal, konsole all carry it).

Two traps. (1) Default-parameter trap: \x1b[J defaults to N = 0 ('erase from cursor to end of screen'), NOT 'erase zero cells / no-op'. Programs that emit \x1b[J 'just to be safe' silently wipe everything below the current cursor row — see csi-default-params for the per-command default table. (2) Mode 3 is not universal: Linux console / fbcon ignores \x1b[3J (no scrollback buffer exists at the kernel level); macOS Terminal.app supports it; conhost.exe (legacy cmd) doesn't have a separate scrollback to erase; ConPTY on Win10 1809+ forwards the byte unchanged. Behaviour inside alt-screen is subtle: \x1b[2J erases the alt buffer's visible area while leaving the primary buffer untouched (DECSET 1049 preserved it separately) — exiting alt-screen via \x1b[?1049l restores the primary buffer in full, regardless of how aggressively you ED'd inside (see alt-screen / crlf-under-alt-screen). Prefer \x1b[2J\x1b[H over the legacy \x1bc (RIS — ris-reset); RIS does a full hardware reset including SGR / charset / palette and is overkill if you just want a fresh visible area.

ED leaves the cursor where it was — pair with CUP (\x1b[H) to position the cursor after the erase (see cursor-position). For per-line erasure use EL (erase-line, final byte K) which has the same default-parameter trap. To 'reset everything but keep the user's terminal sane', prefer DECSTR (decstr-soft-reset\x1b[!p) over RIS; it resets character sets, modes, margins, and SGR without trashing the scrollback or palette. For animated TUIs, scope ED to the rows you actually changed — sending \x1b[2J every frame causes visible flicker on slower remote terminals.

Spec citation: ECMA-48 §8.3.39 (ED)

Examples

bash
printf '\033[2J\033[H'
python
print('\x1b[2J\x1b[H', end='')
go
fmt.Print("\x1b[2J\x1b[H")
javascript
process.stdout.write('\x1b[2J\x1b[H')
c
printf("\x1b[2J\x1b[H");

Used in

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

  • clear(1), reset(1)POSIX clear is essentially this sequence
  • htop, top, btop initial paint
  • tput clear
  • vim :redraw!, neovim equivalents

Frequently asked

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

Why doesn't \x1b[2J clear my scrollback?
\x1b[2J clears the visible screen only — scrollback lives in a separate buffer. Use \x1b[3J (xterm extension) to also clear scrollback, or pair with \x1b[H to move cursor to top first: printf '\033[H\033[2J\033[3J' is the full equivalent of the clear command on most modern terminals.
What's the difference between \x1b[0J, \x1b[1J, \x1b[2J?
\x1b[0J (default, also \x1b[J) erases from cursor to end of screen. \x1b[1J erases from start of screen to cursor. \x1b[2J erases the entire visible screen. None of them move the cursor — pair with \x1b[H if you want the cursor at top-left after the clear. Scrollback is preserved by all three; use \x1b[3J to wipe scrollback too.

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 · 1. The envelope — `\x1b[` … `<final-byte>`