To clear the entire terminal screen in Linux, macOS, or Windows (including WSL), use the command clear—it formats the entire terminal window like a fresh start. For more control (like removing hidden commands or resetting scrollback), use cmd + L (macOS/Linux) or type Ctrl + K (common across systems). If these don’t work, terminal emulators like iTerm2 (macOS) or Alacritty (cross-platform) support powerful reset commands.
What You'll Need
- A terminal emulator installed (built-in on Linux/macOS, or apps like Windows Terminal, iTerm2, or VS Code integrated terminal).
- Basic command-line familiarity (though no prior knowledge is needed).
- Admin/root privileges only required for advanced scrollback removal (see Tips section).
Step-by-Step
-
Basic Clear (most terminals):
Type
clearand press Enter. This erases the entire screen and starts a new prompt at the top-left corner. Works in all Unix-like terminals (Linux, macOS) and Windows Terminal with Linux subsystem (WSL). -
Alternative: Clear to Top (macOS/iTerm2 shortcut):
Press
⌘ + L(Command + L) on macOS or Linux. This removes everything below the current cursor position without affecting the scrollback buffer. Note: This doesn’t work in Windows Terminal unless configured via preferences. -
Force Reset (Linux only):
In terminal emulators like GNOME Terminal, Konsole, or Alacritty, type:
printf '\033[H\033[2J'and press Enter. This sends ANSI escape codes to move the cursor to the home position (top-left) and clear the screen. Works in most Linux terminals, including WSL. The codes are ANSI-standard; some older terminals may ignore them. -
Remove Scrollback Buffer (advanced):
Open your terminal’s settings (e.g., Preferences > Profiles > Text in iTerm2) and look for:
- Scrollback size (set to
0to disable history persistence). - Clear on startup (enable to reload terminal completely).
- Scrollback size (set to
-
Windows Terminal Workaround:
If
cleardoesn’t work in Windows Terminal, try:- Open the integrated terminal in VS Code (File > New Terminal).
- Type
cls(Windows) orclear(WSL).
clsfor native Windows; WSL usesclear.
Common Mistakes
-
Using
Ctrl + CorCtrl + D: These interrupt commands or exit the shell, not clear the screen. Useclearinstead. -
Typing
reset: This resets terminal attributes (foreground/background colors) but leaves the screen unchanged. Combine it withclearfor a full reset:reset;clear -
Ignoring terminal emulator quirks: Some emulators (e.g., PuTTY) use different escape sequences. Check their documentation for
clearequivalents. -
Overlooking hidden shells: If you’re in a subshell (e.g.,
bash -i), clearing won’t affect the parent shell. Runexitorreturnfirst.
Tips
-
Permanent Workaround: Add this to your shell config file (
~/.bashrc,~/.zshrc, or~/.profile):function clear { clear; fi }Then typeclearforever. MacOS/Linux shortcuts won’t conflict. - macOS/iTerm2 Pro Tip: Enable Clear on startup in preferences to avoid manual clearing after every session.
-
Scrollback Recovery: If you lose commands, check your shell’s history file (e.g.,
~/.bash_history) and reload it:history;source ~/.bash_history -
Color Reset: To clear colors *and* the screen (e.g., after a long script), use:
printf '\033[2J\033[3J'\033[3J clears color attributes. -
Admin Privileges: To empty the scrollback buffer system-wide (Linux only), run:
sudo mv ~/.bash_history ~/.bash_history.bakThen recreate the file. Warning: This deletes all terminal history.
Troubleshooting
-
If
clearfails:- Check if your terminal emulator supports it (e.g.,
cat /dev/ttylists the current terminal type). - Try
echo -e "\e[H\e[J"(ANSI escape codes) as a fallback. - For Windows Terminal, ensure the
clearoption is enabled in settings: Settings > Profiles > Text > Clear on startup.
- Check if your terminal emulator supports it (e.g.,
-
If the screen stays blank after clearing:
- Check for terminal mode conflicts (e.g., non-standard terminal modes).
- Restart the terminal emulator or reboot the system.
-
If you need to clear *both* the screen *and* the command history:
- Type
history -cto clear the shell history. - Then use
clearor ANSI escape codes.
- Type
Frequently Asked Questions
Can I clear the terminal without losing my current command?
Yes. If the cursor is at the start of a line,⌘ + L (macOS) or Ctrl + L (Linux) clears only what’s below it. Otherwise, use clear to reset the entire screen.
How do I clear a terminal in Windows PowerShell?
PowerShell usescls like CMD, but it doesn’t support clear. Use:
Clear-Host (PowerShell built-in) or Set-Content -Path $null -Value '' (force-clear).
Why does clear not work in some terminals?
Some terminals (e.g., old DOS boxes or web terminals) ignore clear. Try printf '\033[2J' (ANSI escape) or check your terminal’s manual for vendor-specific commands.
How can I automatically clear the terminal on startup?
Add this to your shell config file:clear (for bash/zsh)
or
printf '\033[H\033[2J' (for ANSI-compliant terminals).
What if I accidentally cleared my shell history?
Restore it withhistory -r ~/.bash_history (Linux/macOS). For permanent clearing, back up the file first:
cp ~/.bash_history ~/.bash_history.bak