461

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?

jww
  • 97,681
  • 90
  • 411
  • 885
Sandeep Datta
  • 28,607
  • 15
  • 70
  • 90
  • 48
    I'd categorize this as "software tools commonly used by programmers" (mentioned in the FAQ as valid). – Sandeep Datta Mar 20 '11 at 06:18
  • 5
    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. – Dennis Williamson Mar 20 '11 at 11:23
  • @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. – jcollum Mar 14 '13 at 15:16
  • 1
    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" – Ra_ Apr 11 '16 at 14:54
  • 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. – Toby Speight Jul 04 '18 at 12:24
  • Good point, updated the question. – Sandeep Datta Jul 05 '18 at 13:02
  • reset, tput reset, and printf "\033c" do not work for me https://media.giphy.com/media/EEyLnBuIzVc9c82xoV/giphy.gif – sdfsdf Jul 10 '18 at 17:26
  • For mac, this works like a charm : https://stackoverflow.com/a/2198403/4915693 – Dreams Jul 01 '20 at 18:23
  • [All those solutions will not work in vim `:term`](https://vi.stackexchange.com/q/17984/26108). – user202729 Jan 25 '22 at 08:03
  • It's interesting how this question supposedly doesn't meet the guidelines, but 456 people have found it useful. – Stan Apr 03 '23 at 13:13

12 Answers12

571

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.

user202729
  • 3,358
  • 3
  • 25
  • 36
Sandeep Datta
  • 28,607
  • 15
  • 70
  • 90
  • 4
    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. – Laurence Gonsalves Mar 20 '11 at 06:35
  • 2
    `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`). – Dennis Williamson Mar 20 '11 at 11:22
  • @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 Mar 20 '11 at 12:39
  • 2
    @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. – nhed Mar 20 '11 at 18:14
  • 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. – Dennis Williamson Mar 20 '11 at 20:04
  • @Dennis Williamson 1st comment: Ok I have edited my answer to remove the reference to the separate binary. 2nd comment: Yes agreed! – Sandeep Datta Mar 21 '11 at 16:54
  • `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. –  Nov 26 '14 at 07:10
  • `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. –  Nov 26 '14 at 07:26
  • 2
    $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: https://en.wikipedia.org/wiki/ANSI_escape_code This is an example of in-band signalling. – abgordon Feb 19 '16 at 01:01
  • Worked perfectly on Linux. Didn't clear the scrollback on macOS. – wim Sep 13 '17 at 15:26
  • 1
    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](http://invisible-island.net/xterm/ctlseqs/ctlseqs.html). (If you want to learn more about terminal escape sequences, see [ANSI escape sequence](https://en.wikipedia.org/w/index.php?title=ANSI_escape_sequence)). – anishpatel Oct 10 '17 at 03:15
  • For those who are using the version of Ubuntu that can be [downloaded from the Microsoft Store on Windows 10](https://www.microsoft.com/store/productId/9NBLGGH4MSV6), the command `clear && echo -en "\e[3J"` works there, unlike all the others. – Fabio says Reinstate Monica Feb 21 '18 at 15:58
  • 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? – RoboJ1M Aug 24 '18 at 12:10
  • `clear && echo -en "\e[3J"` works well for macos 10.15.x. – Chris McAfee Mar 21 '20 at 04:56
  • 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 – Razor Storm May 08 '20 at 19:15
  • 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? – Sandeep Datta May 09 '20 at 00:11
  • None of the approaches mentioned here work on macOS terminal in Big Sur 11.6.2 – Piyush Soni Feb 09 '22 at 13:18
240

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

vpit3833
  • 7,817
  • 2
  • 25
  • 25
  • 3
    Thanks! But it clears every thing including the prompt. See my answer for a solution which doesn't do this. – Sandeep Datta Mar 20 '11 at 06:15
  • 5
    @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 Mar 20 '11 at 06:29
  • @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. – Sandeep Datta Mar 20 '11 at 07:00
  • 1
    @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. – Laurence Gonsalves Mar 20 '11 at 07:10
  • Awesome, cleaner than any bash script. Easier too with no need for an alias. Very fast on my system. Thank you. – Aditya M P Aug 07 '13 at 12:27
  • 4
    The one-second delay associated with `reset` is unbearable for me. – Jonathon Reinhart Jun 14 '16 at 12:01
  • @orbfish It didn't clear all of the screen contents. I'm on High Sierra too. – Dilip Raj Baral May 03 '18 at 04:03
  • @DilipRajBaral wield. You're using the default Mac term program? – orbfish May 05 '18 at 19:01
  • @orbfish Yeah. Default terminal. – Dilip Raj Baral May 08 '18 at 10:09
  • 2
    This should be the default answer imho. Magic numbers printed are never an answer – daniels_pa Aug 13 '18 at 15:05
82
tput reset

That will do the trick!

Community
  • 1
  • 1
10

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.)

TecBrat
  • 3,643
  • 3
  • 28
  • 45
  • 1
    A 3 year old comment from @Dennis Williamson led me to this answer. – TecBrat Apr 16 '14 at 14:37
  • 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. – Pierre Voisin Jul 12 '17 at 15:29
8
  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
    
Mir Rahed Uddin
  • 1,208
  • 2
  • 12
  • 25
7

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.

user202729
  • 3,358
  • 3
  • 25
  • 36
N1mr0d
  • 493
  • 5
  • 11
  • `\033c` is same thing as `\x1bc`, and `\x1bc` is the same thing as `\u001bc`. All three of those work! – Jake Sep 07 '21 at 23:56
7

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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
elbedoit
  • 105
  • 1
  • 1
6

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.

JWL
  • 13,591
  • 7
  • 57
  • 63
  • 1
    My terminal is for some reason unable to clear scrollback history, none of these solutions work except just printing a bunch of newlines, I would just suggest a clear at the end so to reset the cursor back at the top though. – Maximilian Ballard Jan 10 '23 at 16:46
  • @MaximilianBallard which TTY is this? – JWL Jan 25 '23 at 12:46
  • `st` terminal, but I don't know if its the specific terminal itself as it doesn't have this issue on my other computer and I use the same one. – Maximilian Ballard Jan 25 '23 at 15:29
5

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

dz902
  • 4,782
  • 38
  • 41
3

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

The111
  • 5,757
  • 4
  • 39
  • 55
-2
echo -e "\e[3J"

This works in Linux Machines

Nithin
  • 328
  • 1
  • 7
  • 16
  • 6
    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. – Toby Speight Jul 04 '18 at 12:25
-57

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;
}
Max
  • 47
  • 35
    Sorry, but this isn't the best solution. There are better methods than spewing 999 newlines – pbfy0 Nov 25 '12 at 13:40
  • 2
    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. – JoshuaDavid Jan 25 '18 at 06:51
  • @JoshuaDavid The whole point of the question was that OP did *not* want to be able to scroll back after clearing. – Socowi Oct 16 '18 at 12:30
  • @Socowi My comment was addressing this -50 poorly-rated answer, not the OP. But you're right! – JoshuaDavid Oct 17 '18 at 13:38
  • 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` – mwfearnley Dec 10 '20 at 11:17