Clear Your Linux Terminal Instantly

If you’ve been using a terminal for a while, you already know how quickly that little window can fill up with all sorts of clutter. You’re also probably intimately familiar with the clear command and your muscle memory has muscles of its own by now. Clearing the terminal screen is a fundamental task and it is so refreshing to have a faster way to do that.

What does it mean to clear a terminal?

Clearing the terminal essentially means removing everything from the screen, moving it all out of sight, leaving you with a clean slate and a fresh prompt. It’s almost like starting over with a brand new terminal session. This can be especially useful when you want a fresh view, free from distractions.

To ask why we clear terminals is to ask why the leaves fall. In a world of distractions, where attention is the only real currency, it makes sense to want to help maintain focus and reduce that cognitive load associated with cluttered information. It’s a good habit to form when switching context or tasks.

The traditional way - clear

The traditional way to clear the terminal is by using the clear command. Typing “clear” and pressing Enter will clean up your terminal screen. However, if you’re in the middle of typing a long command, this can be inconvenient. You’d have to delete what you’ve written, clear the terminal, and then start over.

The faster way - Ctrl + L

By simply pressing Ctrl + L, you can instantly clear your terminal screen without disrupting your workflow. It is a simple, yet powerful keyboard shortcut that takes very little getting used to. Go ahead, give it a try! I’ve been using this instead of the clear command for years and haven’t looked back once.

How does it work?

Ctrl + L works by sending an ANSI escape code to the terminal, which tells it to clear the screen. Most modern terminal emulators and shells, such as Bash, Zsh, and others, recognize this shortcut and act accordingly. The Ctrl key here is simply associating the non-printable characters from ASCII code 1 - SOH with the printable characters from ASCII code 65 - A so then the letter L would then correspond to ASCII code 12 - FF. The shortcut sends a form feed character to the terminal, instructing it to redraw the screen from the current cursor position.

The scrollback buffer

When you use Ctrl + L, it clears the visible screen but not the scrollback buffer. This means you can still scroll up to see your previous commands. The scrollback buffer is a feature in terminal emulators (not the shell) that allows you to scroll back and view previous output that has scrolled off the screen.

The clear command tries to clear the scrollback buffer by default and it determines the best way to perform the operation based on your particular type of terminal emulator. You can see exactly what escape sequence it issues by cleverly redirecting its standard output to a file.

# Redirect clear's output to a text file:
clear > clear.txt

# Read the file + non-printing characters:
cat -v clear.txt

# Or just pipe it to check it inline:
clear | cat -v
# e.g. ^[[H^[[2J^[[3J

There is also reset, a less popular command which re-initializes the terminal by clearing the screen and resetting the terminal settings. It does a little bit more under the hood but it can take up about 1 second to run, which is why you may prefer tput reset or straight up just printf '\033\143' instead (on any xterm-based terminal). I’m only mentioning these because they can still make good aliases in lieu of pure awesomeness.

Customizing shortcuts

Depending on your terminal emulator, you might be able to set custom shortcuts for clearing the terminal. This can be handy if you have a specific workflow or if Ctrl + L is just not comfortable for you.

For example ⌘ + K on macOS clears both, but is of course handled by the terminal emulator itself, much like Ctrl + Shift + K on KDE’s Konsole. Most terminal emulators allow you to set shortcuts for clearing both the visible terminal screen and the scrollback buffer.

Honorable mention

Found this shiny little gem here on Bash 5.1 (Bullseye) according to which, one should be able to Ctrl + Alt + L and clear the terminal screen and scrollback buffer at the same time. Check to see if clear-display is being handled:

bind -p | grep clear
# e.g.
# "\e\C-l": clear-display
# "\C-l": clear-screen

If it doesn’t appear to be handled, you can attempt to set it up on your own:

  • for bash: add "\e\C-l": clear-display to your ~/.inputrc
  • for zsh: add bindkey -s "\e\C-l" "clear-screen" to your ~/.zshrc

This way, if you really do mind the scrollback buffer not being cleared by Ctrl + L you can just throw in an extra modifier and there you have it: Ctrl + Alt + L clears both the visual screen and scrollback buffer - unless your desktop environment decides to handle that keyboard shortcut for you and brutally terminate your session.

Conclusion

This was an example of a single keyboard shortcut that is interpreted as a control sequence by the terminal. There are over a dozen of these, some more useful than others and we’re going to cover them all in an exciting series of upcoming articles.