Clear a terminal screen for real

230,919

Solution 1

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

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

tput reset

That will do the trick!

Solution 4

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

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.

Share:
230,919
Sandeep Datta
Author by

Sandeep Datta

Updated on January 25, 2022

Comments

  • Sandeep Datta
    Sandeep Datta over 2 years

    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?

    • Sandeep Datta
      Sandeep Datta about 13 years
      I'd categorize this as "software tools commonly used by programmers" (mentioned in the FAQ as valid).
    • SourceSeeker
      SourceSeeker about 13 years
      What you're really asking is "How can I clear the terminal's scroll-back buffer?" which is independent of the shell (Bash) or Ubuntu.
    • jcollum
      jcollum about 11 years
      @spiderplant0 probably because AskUbuntu is the right place for this -- at this time. Didn't exist when this was asked, so it got closed as off topic, even though that isn't the case.
    • Ra_
      Ra_ about 8 years
      That's a more general question, affecting not only Ubuntu or bash, as @Dennis noted. I'd change the topic "Clear the Ubuntu bash screen for real" --> "Clear a terminal screen for real"
    • Toby Speight
      Toby Speight almost 6 years
      There are many different terminal types with which you can run Bash (the term "bash terminal" is meaningless). "Clearing" isn't applicable to all of them - sometimes, the nearest approximation is to tear the paper and bin/shred/burn/destroy the bit you don't want.
    • Sandeep Datta
      Sandeep Datta almost 6 years
      Good point, updated the question.
    • sdfsdf
      sdfsdf almost 6 years
      reset, tput reset, and printf "\033c" do not work for me media.giphy.com/media/EEyLnBuIzVc9c82xoV/giphy.gif
    • Dreams
      Dreams almost 4 years
      For mac, this works like a charm : stackoverflow.com/a/2198403/4915693
    • user202729
      user202729 over 2 years
  • Sandeep Datta
    Sandeep Datta about 13 years
    Thanks! But it clears every thing including the prompt. See my answer for a solution which doesn't do this.
  • Laurence Gonsalves
    Laurence Gonsalves about 13 years
    @SDX2000 Both clear the prompt, and then the shell generates a new one. The one disadvantage to reset is that it seems to be a bit slower (probably because it does more than just emit ESC c) but it's more portable.
  • Laurence Gonsalves
    Laurence Gonsalves about 13 years
    This is actually terminal specific. "\033c" is ESC c which is the VT-XXX escape sequence for "Full Reset (RIS)". Almost all terminals people actually use these days are VT compatible, but if you ever find yourself using a weird terminal, this might not work. @vpit3833's answer is more likely to work assuming TERM is set correctly.
  • Sandeep Datta
    Sandeep Datta about 13 years
    @Laurence thanks for the clarification...I tried reset but it was so slow that I assumed the prompt was cleared along with everything else. I will still prefer to use ESC c since I will probably never use any terminal other than the one Ubuntu uses. Though reset may come in handy someday when I am debugging a remote machine through the serial port etc.
  • Laurence Gonsalves
    Laurence Gonsalves about 13 years
    @SDX2000 reset is also handy for those cases where your terminal gets badly mangled because you killed something (or catted a binary file) and it left your term 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 fixes that. That's actually all I ever use it for... I've never had a need/desire to clear my scroll-back buffer.
  • SourceSeeker
    SourceSeeker about 13 years
    printf is a Bash builtin (it's true that it's also a separate binary, but builtins have precedence and most modern shells have printf).
  • nhed
    nhed about 13 years
    @SDX2000 Does this clear your scroll buffer? Wasn't that a requirement?? BTW if this is to monitor a chatty program's output, my favorite way to do that is to run (or tail) in emacs, then I created a binding that marks and kills the whole buffer.
  • nhed
    nhed about 13 years
    @SDX2000 OK ... I know you specified Ubuntu, and I assumed that these would behave similar on all "modern" terminal emulators. I initially tested on my MAC's terminal and it did not reset there, but it did reset on my Centos Linux.
  • SourceSeeker
    SourceSeeker about 13 years
    Yeah, I wouldn't rely on this if security is at stake. It would be safer to close the terminal and open a new one.
  • Sandeep Datta
    Sandeep Datta about 13 years
    @Dennis Williamson 1st comment: Ok I have edited my answer to remove the reference to the separate binary. 2nd comment: Yes agreed!
  • pbfy0
    pbfy0 over 11 years
    Sorry, but this isn't the best solution. There are better methods than spewing 999 newlines
  • Aditya M P
    Aditya M P over 10 years
    Awesome, cleaner than any bash script. Easier too with no need for an alias. Very fast on my system. Thank you.
  • Optimized Coder
    Optimized Coder over 10 years
    This has been changed to CTRL+Shift+K in later KDE versions: bugs.kde.org/show_bug.cgi?id=282593
  • TecBrat
    TecBrat about 10 years
    A 3 year old comment from @Dennis Williamson led me to this answer.
  • Admin
    Admin over 9 years
    which cls || echo 'printf "\033c"' | sudo tee /usr/bin/cls && sudo chmod 755 /usr/bin/cls will make you a neat clear-screen script using the above answer.
  • Admin
    Admin over 9 years
    which rept || printf '#!/bin/bash\nwhile true;do cls; $@; sleep 1; done\n' | sudo tee /usr/bin/rept && sudo chmod 755 /usr/bin/rept will create a script that keeps repeating your program with the specified parameters with one second delay and clears the screen each time. This can be used for looping some perl-experiments for instance.
  • Ponkadoodle
    Ponkadoodle almost 9 years
    Executes much faster than plain reset, but still does the job!
  • abgordon
    abgordon about 8 years
    $0.02 a few years later, but i'm a student at CU. Asked my operating systems professor and he said this was an example of ANSI escape sequence: en.wikipedia.org/wiki/ANSI_escape_code This is an example of in-band signalling.
  • Seng Cheong
    Seng Cheong almost 8 years
    The one-second delay associated with reset is unbearable for me.
  • l2aelba
    l2aelba over 7 years
    Faster than just reset
  • Pierre Voisin
    Pierre Voisin almost 7 years
    Also, given that in the "Window" settings of PuTTY you activated the "System menu appears on ALT-Space" you can rapidly do CTRL+L then ALT+Space, U which first clears the terminal window then resets the scrollback for real.
  • wim
    wim over 6 years
    Worked perfectly on Linux. Didn't clear the scrollback on macOS.
  • anishpatel
    anishpatel over 6 years
    The solution for KDE is a solution for xterm and terminals that support xterm's escape sequences. The official list of xterm escape sequences is at invisible-island.net/xterm/ctlseqs/ctlseqs.html. (If you want to learn more about terminal escape sequences, see ANSI escape sequence).
  • JoshuaDavid
    JoshuaDavid over 6 years
    There actually is some value to this. In some terminals, clearing the screen with the other methods will kill the history and not allow you to scroll back... although with 999 lines my finger would get tired. Poor guy.
  • Fabio says Reinstate Monica
    Fabio says Reinstate Monica about 6 years
    For those who are using the version of Ubuntu that can be downloaded from the Microsoft Store on Windows 10, the command clear && echo -en "\e[3J" works there, unlike all the others.
  • Dilip Raj Baral
    Dilip Raj Baral almost 6 years
    @orbfish It didn't clear all of the screen contents. I'm on High Sierra too.
  • orbfish
    orbfish almost 6 years
    @DilipRajBaral wield. You're using the default Mac term program?
  • Dilip Raj Baral
    Dilip Raj Baral almost 6 years
    @orbfish Yeah. Default terminal.
  • Toby Speight
    Toby Speight almost 6 years
    Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should vote it up instead of repeating the same information.
  • daniels_pa
    daniels_pa over 5 years
    This should be the default answer imho. Magic numbers printed are never an answer
  • RoboJ1M
    RoboJ1M over 5 years
    So, usually with Linux you have to add lines like this to a script so the alias gets set up when you log in. Where would you put this one?
  • Socowi
    Socowi over 5 years
    @JoshuaDavid The whole point of the question was that OP did not want to be able to scroll back after clearing.
  • JoshuaDavid
    JoshuaDavid over 5 years
    @Socowi My comment was addressing this -50 poorly-rated answer, not the OP. But you're right!
  • Chris McAfee
    Chris McAfee about 4 years
    clear && echo -en "\e[3J" works well for macos 10.15.x.
  • Razor Storm
    Razor Storm almost 4 years
    Trying this in python 3.7.2 and it does clear the screen. However, when I have more than one page worth of content in the terminal, it only clears all the content available to the current screen. However if you scroll back up, the old content is still there from previous screens. Is there a way to fix this
  • Sandeep Datta
    Sandeep Datta almost 4 years
    What operating system are you on? And what do you mean by trying this in python? Are you using IDLE or just running the python REPL in some shell? If it's the latter then which shell are you using?
  • mwfearnley
    mwfearnley over 3 years
    I daren't post this as an answer, but you could do something like this in bash with head -c999 /dev/zero | tr '\0' '\n', or yes '' | head -999
  • Unknown123
    Unknown123 over 3 years
    Explanation of why it is faster: Why does the reset command include a delay?
  • Jake
    Jake over 2 years
    \033c is same thing as \x1bc, and \x1bc is the same thing as \u001bc. All three of those work!
  • Piyush Soni
    Piyush Soni about 2 years
    None of the approaches mentioned here work on macOS terminal in Big Sur 11.6.2