How the Linux Clear Command Affects Terminal Buffer
The Linux clear command is standard utility used to
declutter the command-line interface, but its effect on the underlying
terminal buffer depends heavily on modern implementation standards and
terminal emulator support. While it visually resets the active screen,
its interaction with the scrollback buffer—the history of previous
inputs and outputs—ranges from temporarily hiding data off-screen to
permanently purging memory, determined by the specific control sequences
sent to the terminal.
The Two Components of Terminal Display Memory
To understand how clear functions, you must distinguish
between two areas of terminal display memory:
- The Active Viewport: The currently visible grid of rows and columns displayed inside the terminal window.
- The Scrollback Buffer: The reserved memory that stores lines that have scrolled off the top of the active viewport, allowing you to scroll up and review command history.
Traditional Behavior: Clearing the Viewport Only
Historically, the clear command read the terminal's
capabilities from the terminfo database and emitted the
standard ANSI escape sequence ESC [ H ESC [ 2 J (or
\033[H\033[2J).
\033[Hmoves the cursor to the top-left corner (home position).\033[2Jclears the entire active viewport.
Under this traditional behavior, the terminal buffer is not deleted.
The existing output is merely pushed out of view into the scrollback
buffer. If you scroll up using a mouse wheel or keyboard shortcuts (like
Shift + PageUp), all previous terminal interactions remain
visible and intact.
Modern Behavior: Wiping the Scrollback Buffer
Modern versions of the clear utility (distributed as
part of the ncurses package) include the E3
capability. When executed, it sends an extended escape sequence:
ESC [ 3 J (or \033[3J).
\033[3Jexplicitly commands the terminal emulator to delete all lines stored in the scrollback buffer.
In modern Linux distributions running standard terminal emulators
(such as GNOME Terminal, Alacritty, or Kitty), running
clear erases both the active viewport and the scrollback
buffer entirely. Once executed, you cannot scroll up to view prior
commands or output; the data is cleared from the terminal application's
memory.
Preserving Scrollback with
clear -x
If you want to clear the visible screen without destroying the
scrollback buffer on modern systems, use the -x flag:
clear -xThe -x modifier instructs the command not to invoke the
E3 capability, limiting the reset operation strictly to the
visible viewport (\033[2J) and leaving the scrollback
history accessible.
Emulator and Multiplexer Variations
The actual effect on the buffer ultimately relies on how the running terminal program interprets these escape sequences:
- Terminal Emulators: Most graphical terminal
emulators support
\033[3Jand immediately discard their scrollback memory. - Terminal Multiplexers (
tmux,screen): Multiplexers maintain independent internal scrollback buffers. Runningclearinside atmuxpane will clear the visible pane and the pane's immediate viewport, buttmux's internal copy-mode buffer may require a dedicated multiplexer command (such asclear-history) to be fully wiped.