Clear a terminal screen for real
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.
Sandeep Datta
Updated on January 25, 2022Comments
-
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 about 13 yearsI'd categorize this as "software tools commonly used by programmers" (mentioned in the FAQ as valid).
-
SourceSeeker about 13 yearsWhat 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 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_ about 8 yearsThat'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 almost 6 yearsThere 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 almost 6 yearsGood point, updated the question.
-
sdfsdf almost 6 yearsreset, tput reset, and printf "\033c" do not work for me media.giphy.com/media/EEyLnBuIzVc9c82xoV/giphy.gif
-
Dreams almost 4 yearsFor mac, this works like a charm : stackoverflow.com/a/2198403/4915693
-
user202729 over 2 years
-
-
Sandeep Datta about 13 yearsThanks! But it clears every thing including the prompt. See my answer for a solution which doesn't do this.
-
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 about 13 yearsThis is actually terminal specific.
"\033c"
isESC 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 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. Thoughreset
may come in handy someday when I am debugging a remote machine through the serial port etc. -
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 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 haveprintf
). -
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 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 about 13 yearsYeah, 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 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 over 11 yearsSorry, but this isn't the best solution. There are better methods than spewing 999 newlines
-
Aditya M P over 10 yearsAwesome, cleaner than any bash script. Easier too with no need for an alias. Very fast on my system. Thank you.
-
Optimized Coder over 10 yearsThis has been changed to CTRL+Shift+K in later KDE versions: bugs.kde.org/show_bug.cgi?id=282593
-
TecBrat about 10 yearsA 3 year old comment from @Dennis Williamson led me to this answer.
-
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 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 almost 9 yearsExecutes much faster than plain
reset
, but still does the job! -
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 almost 8 yearsThe one-second delay associated with
reset
is unbearable for me. -
l2aelba over 7 yearsFaster than just
reset
-
Pierre Voisin almost 7 yearsAlso, given that in the "Window" settings of PuTTY you activated the "System menu appears on ALT-Space" you can rapidly do
CTRL+L
thenALT+Space
,U
which first clears the terminal window then resets the scrollback for real. -
wim over 6 yearsWorked perfectly on Linux. Didn't clear the scrollback on macOS.
-
anishpatel over 6 yearsThe 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 over 6 yearsThere 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 about 6 yearsFor 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 almost 6 years@orbfish It didn't clear all of the screen contents. I'm on High Sierra too.
-
orbfish almost 6 years@DilipRajBaral wield. You're using the default Mac term program?
-
Dilip Raj Baral almost 6 years@orbfish Yeah. Default terminal.
-
Toby Speight almost 6 yearsThough 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 over 5 yearsThis should be the default answer imho. Magic numbers printed are never an answer
-
RoboJ1M over 5 yearsSo, 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 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 over 5 years@Socowi My comment was addressing this -50 poorly-rated answer, not the OP. But you're right!
-
Chris McAfee about 4 years
clear && echo -en "\e[3J"
works well for macos 10.15.x. -
Razor Storm almost 4 yearsTrying 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 almost 4 yearsWhat 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 over 3 yearsI daren't post this as an answer, but you could do something like this in bash with
head -c999 /dev/zero | tr '\0' '\n'
, oryes '' | head -999
-
Unknown123 over 3 yearsExplanation of why it is faster: Why does the
reset
command include a delay? -
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 about 2 yearsNone of the approaches mentioned here work on macOS terminal in Big Sur 11.6.2