clear Command Cheat Sheet
clear provides a clean slate in your terminal window. While simple on the surface, understanding how it interacts with the scrollback buffer and different terminal emulators is useful for power users.
Synopsis
clear [options]
Description
clear clears your screen if this is possible. It looks in the environment for the terminal type and then in the terminfo database to figure out how to clear the screen.
Basic Usage
Standard Clear
clear
Moves the prompt to the top of the screen. In most modern graphical terminal emulators (GNOME Terminal, iTerm2, Alacritty), the previous output is pushed up into the scrollback buffer. You can still scroll up to see it.
Keyboard Shortcut
Ctrl + L
Equivalent to typing clear in bash, zsh, and many other shells. This is a readline library function (clear-screen).
Clearing Scrollback
By default, clear explicitly does not clear the scrollback buffer (the history you scroll up to see).
The "Real" Clear
To clear the screen and the scrollback buffer (so you can't scroll up):
# For some terminals
clear && printf '\e[3J'
Alternatively, use the terminal emulator's built-in shortcut:
- macOS (Terminal/iTerm): Cmd + K
- GNOME Terminal: Ctrl + L (often just clears screen), check View menu.
Using reset
If your terminal is corrupted (weird characters, broken encoding) and clear doesn't help:
reset
This re-initializes the terminal, clears the screen, and often clears the scrollback. It might take a second to run.
Options
clear ignores any command-line parameters that naturally appear in the logic of clearing the screen, but accepts standard GNU options.
clear -x (ncurses extension):
Does not attempt to clear the terminal's scrollback buffer using the extended E3 capability.
clear -x
(This keeps the scroll position but moves prompt to top).
Alternatives and Aliases
Create a Super Clear
Add this to your .bashrc or .zshrc to clear everything:
alias cls='clear && printf "\e[3J"'
Python One-Liner
If clear command is missing (rare):
python3 -c 'import os; os.system("clear")'
Scripting Use Cases
Interactive Menus
Clear screen before showing a menu options.
#!/bin/bash
while true; do
clear
echo "1. Show Disk Usage"
echo "2. Show Memory"
echo "q. Quit"
read -p "Choose: " choice
case $choice in
1) df -h; read -p "Press Enter...";;
2) free -m; read -p "Press Enter...";;
q) break;;
esac
done
Watch Command
watch automatically clears the screen before updating.
watch -n 1 "date"
Technical Details
- TERM env var:
clearrelies on the$TERMvariable (e.g.,xterm-256color) to look up the correct escape sequences interminfo. - Exit Status: Returns 0 on success, 1 if terminal type is unknown.
Tips
- Muscle Memory: Learn
Ctrl+L. It is much faster than typingclear. - Binary Mess: If you accidentally
cata binary file and your terminal shows gibberish, typereset(even if you can't see what you are typing) and press Enter. - Preserve Context: Sometimes you don't want to clear. Use
Entera few times to visually separate output without hiding history.