How to clear the terminal screen for real in case of Linux?

Most of us who have been using Linux, particularly Ubuntu or CentOS, normally use clear command or the shortcut key combination Ctrl+L to clear the terminal screen. But, it doesn’t actually clear the screen, it only fools the user into thinking the screen has been cleared. You can still see the output from the previous commands when you scroll up using the mouse. This makes life difficult when you are actually working with whole lot of text.

Various soultions available on the internet like escape code, are only variations of clear command.
So, whats the way to actually clear the screen instead of  just adding new lines?

Well, i recently came across a command which actually solves our purpose.

printf “\033c” is the command you have been looking for until now. It actually clears the terminal screen and provides you the functionality similiar to CLS command in case of CMD.EXE on DOS.

But what exactly is this command and how does it actually work?

\033  ==  \x1B == 27 == ESC

So, this becomes <ESC>c which is the VT-XXX escape sequence for “Full Reset (RIS)”. Almost all the terminals we use these days are VT compatible, but if you ever find yourself using some weird terminal, this might not work in that case. printf is a bash builtin and builtins have precedence over other separate binaries.

We have yet another command, reset, which clears up the terminal screen but the previous commands can be accessed through arrow keys. The one disadvantage of this command is that it seems to be a bit slower, probably because it does not just emit ESC c but it is obviously more portable as compared to our previous command.

reset is also handy for those cases where your terminal gets badly mangled because you killed something and it left your terminal in a mangled state. Ever get into a state where your prompt shows up but not your typing, and when you hit enter the new prompt shows up next to the previous prompt rather than below it? Reset command fixes that along with clearing the scroll-back buffer. It also works great for CYGWIN :)

So, these commands are just awesome for the purpose of clearing the terminal screen. Simply, cleaner than any bash script and save the hassles of creating aliases.


open source 5322076162546865344

Post a Comment

  1. Printf is not a bash builtin; printf is.

    printf '\ec' (why use \033?) clears the screen and the terminal buffer, but on every terminal I've tried the arrow keys still retrieve previous commands, just as with reset.

  2. "Printf" instead of "printf", sorry for the silly mistake. And did i mention, it clears both scrollback and terminal buffer....??




Recent Posts


Join Us