Clear a terminal screen for real

LinuxUbuntuTerminalGnome TerminalKonsole

Linux Problem Overview


Using the clear command on the terminal only fools the user into thinking the screen has been cleared...you can still see output from the previous commands when you scroll using the mouse. This makes life difficult when you are drowning in a tsunami of text.

Various solutions (escape code etc.) which can be found on the Internet are only variations of what the clear command already does.

So how do you clear the contents of a terminal in Linux for real?

Linux Solutions


Solution 1 - Linux

Use the following command to do a clear screen instead of merely adding new lines ...

printf "\033c"

yes that's a 'printf' on the bash prompt.

You will probably want to define an alias though...

alias cls='printf "\033c"'
Explanation

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

So this becomes <ESC>c which is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.

Edit

Here are a few other ways of doing it...

printf "\ec" #\e is ESC in bash
echo -en "\ec" #thanks @Jonathon Reinhart.
# -e    Enable interpretation of of backslash escapes
# -n    Do not output a new line

KDE

The above does not work on the KDE console (called Konsole) but there is hope! Use the following sequence of commands to clear the screen and the scroll-back buffer...

clear && echo -en "\e[3J"

Or perhaps use the following alias on KDE...

alias cls='clear && echo -en "\e[3J"'

I got the scroll-back clearing command from here.

Solution 2 - Linux

Try reset. It clears up the terminal screen but the previous commands can be accessed through arrow or whichever key binding you have.

Solution 3 - Linux

tput reset

That will do the trick!

Solution 4 - Linux

None of the answers I read worked in PuTTY, so I found a comment on this article:

In the settings for your connection, under "Window->Behavior" you'll find a setting "System Menu Appears on ALT alone". Then CTRL + L, ALT, l (that's a lower case L) will scroll the screen and then clear the scrollback buffer.

(relevant to the OP because I am connecting to an Ubuntu server, but also apparently relevant no matter what your server is running.)

Solution 5 - Linux

My favorite human friendly command for this is:

reset

Tested on xterm and VT100. It also helps after an abnormal program termination. Keeps the command buffer, so up-arrow will cycle through previous commands.

Solution 6 - Linux

The following link will explain how to make that alias permanent so you don't have to keep typing it in.

https://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias

These are the steps detailed at that link.

  1. vim ~/.bashrc or gedit ~/.bashrc or what ever text editor you like
  2. put alias cls='printf "\033c"' at the bottom of the file
  3. save and exit
  4. . ~/.bashrc (and yes there should be a space between . and ~)
  5. now check to see if everything worked!

I take no credit for this information just passing it along.

Solution 7 - Linux

  1. Clean the visible screen

     clear 
    
  2. Clean screen and clear buffer

     clear && clear 
    
  3. Clean and 1-sec delay

     reset
    
  4. Clean without 1-sec delay

     tput reset
    

Solution 8 - Linux

Just to add that tmux scroll buffer does not clear with clear, reset or printf. You need to :clear-history. See link.

Solution 9 - Linux

I know the solution employing printing of new lines isn't much supported, but if all else fails, why not? Especially where one is operating in an environment where someone else is likely to be able to see the screen, yet not able to keylog. One potential solution then, is the following alias:

alias c="printf '\r\n%.0s' {1..50}"

Then, to "clear" away the current contents of the screen (or rather, hide them), just type c+Enter at the terminal.

Solution 10 - Linux

With KDE and Ubuntu 12.04 LTS and the "Konsole" terminal, none of the posted answers work. However, pressing default keyboard shortcut CTRL+Shift+X does work! Source:

https://bugs.kde.org/show_bug.cgi?id=288913

Solution 11 - Linux

echo -e "\e[3J"

This works in Linux Machines

Solution 12 - Linux

Compile this app.

#include <iostream>
#include <cstring>

int main()
{
  char str[1000];
  memset(str, '\n', 999);
  str[999] = 0;
  std::cout << str << std::endl;
  return 0;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAutodidactView Question on Stackoverflow
Solution 1 - LinuxAutodidactView Answer on Stackoverflow
Solution 2 - Linuxvpit3833View Answer on Stackoverflow
Solution 3 - Linuxuser1348669View Answer on Stackoverflow
Solution 4 - LinuxTecBratView Answer on Stackoverflow
Solution 5 - LinuxelbedoitView Answer on Stackoverflow
Solution 6 - LinuxN1mr0dView Answer on Stackoverflow
Solution 7 - LinuxMir Rahed UddinView Answer on Stackoverflow
Solution 8 - Linuxdz902View Answer on Stackoverflow
Solution 9 - LinuxJWLView Answer on Stackoverflow
Solution 10 - LinuxThe111View Answer on Stackoverflow
Solution 11 - LinuxNithinView Answer on Stackoverflow
Solution 12 - LinuxMaxView Answer on Stackoverflow