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 [-T type] [-V] [-x] [-q]

PARAMETERS

-T type
    Specifies the terminal type to use instead of the value from the TERM environment variable. This is useful for testing or when TERM is set incorrectly.

-V
    Prints the version information of the clear command and exits.

-x
    This option, primarily effective for xterm-like terminals, clears the screen and also attempts to clear the terminal's scrollback buffer. This provides a completely blank slate, removing previous command output from view.

-q
    Enables quiet mode, suppressing any sound output that might occur on some terminals when clearing the screen.

DESCRIPTION

The clear command provides a portable and reliable way to clear your terminal screen. Instead of sending a hardcoded escape sequence, it consults the TERM environment variable to determine your current terminal type. It then looks up the appropriate "clear screen" (cl) capability in the terminfo database and sends that specific sequence to your terminal. This approach ensures that clear works correctly across a wide variety of terminal emulators and consoles, unlike simply typing Ctrl+L or using raw escape sequences which might not be universally effective. For modern xterm-compatible terminals, the -x option can also clear the scrollback buffer, providing a truly clean slate. Its primary purpose is to refresh the display, removing previous output and presenting a clean prompt.

CAVEATS

The effectiveness of clear heavily depends on the correct TERM environment variable setting and the availability of a matching entry in the terminfo database. If these are misconfigured, clear may fail or produce unexpected results. The -x option for clearing the scrollback buffer is not universally supported by all terminal types; it is most effective on xterm and compatible emulators. For a more comprehensive terminal reset (including modes, tabs, etc.), the reset command is often more suitable.

HOW IT WORKS

When clear is executed, it first reads the value of the TERM environment variable. It then uses this information to query the terminfo database for the specific escape sequence associated with the "clear screen" capability (often referred to as cl or clear). This sequence is then printed to standard output, which the terminal interprets as an instruction to clear its display.

ALTERNATIVE METHODS

Users often use Ctrl+L as a keyboard shortcut to clear the screen, which typically sends a form feed character (^L or \f). While this works for many terminals, it's not as robust or portable as clear. Another programmatic alternative is tput clear, which leverages the same terminfo database but is part of the tput utility. For a full terminal reset, including scrollback and various settings, reset is the go-to command.

HISTORY

The clear command is typically part of the ncurses (new curses) library utilities, which provide a programming interface for terminal control. Its functionality has existed for a long time within the Unix/Linux ecosystem, evolving from the older termcap (terminal capabilities) system to the more advanced terminfo system. Its purpose has consistently been to provide a robust and portable method for applications and users to clear the terminal display, abstracting away the complexities of different terminal escape sequences.

SEE ALSO

reset(1), tput(1), stty(1), terminfo(5), setterm(1)

Copied to clipboard