Another Way to Clear the Terminal Screen

Just when I thought I knew everything…

I saw this on the It’s FOSS (https://itsfoss.com) tip of the day.

In a terminal window, you don’t have to type clear to clear the screen. You can just press Ctrl+L. It clears the screen, AND you won’t lose your scrollback. Just rotate the mouse wheel and the previous contents scroll down.

Ah well, a little humility is good for the soul, I guess…

2 Likes

Good to know, thanks for the tip! I always love a good keyboard shortcut. :slight_smile:

I don’t see this on the “It’s FOSS” website, but does this only apply to mate-terminal (since this is categorized under MATE Desktop) or does it also work on gnome-terminal, konsole, etc.?

EDIT BY AUTHOR: I should have tested before asking. It does work on konsole. Not sure if it works on the other terminal emulators.

2 Likes

It works pretty much everywhere, because it’s not the terminal doing it. Ctrl+L comes from your shell. Bash, zsh and fish all bind it to clear screen, so it works the same in mate-terminal, gnome-terminal, konsole, xterm, even over SSH. The terminal app doesn’t matter.

That’s also why the scrollback sticks around. Plain clear on most modern systems wipes the scrollback too. If you like typing it out but want to keep your history, clear -x clears the screen and leaves the scrollback alone.

One more for the toolbox: if you ever cat a binary file by accident and your terminal starts spitting out garbage characters, type reset and hit enter (even if you can’t see what you’re typing). It puts the terminal back to normal.

4 Likes

Question: Does using “clear” wipe out the history of commands that would otherwise be preserved in .bash_history, or is the capture of the command history immune to the effects of the “clear” command?

1 Like

Nope, your history is safe. That one’s on me, I said “history” in my last post when I meant scrollback. Totally different things.

clear and Ctrl+L only mess with what’s on your screen. Your command history lives in ~/.bash_history and doesn’t care what you do to the screen. Hit the up arrow or run history after a clear and it’s all still there.

If you actually want to wipe it:

history -c && history -w

The first part clears this session, the second writes that over the file. Just history -c by itself only clears what’s in memory.

Little bonus: bash only saves your history when you close the terminal. So if you’ve got a few open at once, they can stomp on each other. history -a saves it right then if you don’t want to lose anything.

3 Likes