LinuxCommandLibrary

clear

Clear the terminal screen

TLDR

Clear the screen

$ clear
copy

Clear the screen but keep the terminal's scrollback buffer (equivalent to pressing in Bash)
$ clear -x
copy

Indicate the type of terminal to clean (defaults to the value of the environment variable $TERM)
$ clear -T [type_of_terminal]
copy

Display the version of ncurses used by clear
$ clear -V
copy

SYNOPSIS

clear [-x]

PARAMETERS

-x
    Prevents clearing the scrollback buffer, affecting only the visible screen area. Useful for retaining command history.

DESCRIPTION

The clear command is a simple yet essential Unix utility that erases all content from the terminal screen, repositioning the cursor to the top-left corner. It leverages the terminal's capabilities via the terminfo database (or termcap in older systems) to issue the appropriate escape sequences, such as CSI 2 J (clear entire screen) and CSI H (cursor home) for ANSI-compatible terminals. This ensures a blank display without scrolling artifacts.

By default, clear attempts to clear both the visible screen and the scrollback buffer in modern terminal emulators like xterm, GNOME Terminal, or tmux, using operating system commands or escape sequences (e.g., OSC 3 for scrollback). This behavior prevents users from scrolling back to previous output, providing a true fresh start.

It's commonly used interactively to declutter the screen during long sessions, in shell scripts to improve readability, or bound to hotkeys (e.g., Ctrl+L in bash). The command is extremely lightweight, with negligible performance impact, and is available on virtually all POSIX-compliant systems. Its output depends on the $TERM environment variable; mismatches can lead to incomplete clearing.

CAVEATS

Scrollback clearing relies on terminal emulator support (e.g., xterm, kitty); fails silently on basic terminals without scrollback. Incorrect $TERM may cause partial clears or garbage output.

EXAMPLES

clear
Clears screen and scrollback buffer.

clear -x
Clears visible screen only.

BEHIND THE SCENES

Queries terminfo for clear capability; defaults to ANSI \E[2J\E[H or equivalent.

HISTORY

Originated in early UNIX (pre-4.3BSD); POSIX.1-2008 standardized. Evolved with ncurses/terminfo for better terminal handling; minimal changes over decades due to simplicity.

SEE ALSO

reset(1), tput(1)

Copied to clipboard