535

I can issue the clear command or press Ctrl + L to clear the current Ubuntu terminal, but this just shifts previous output upwards and if you use mouse scroll or PgUP and PgDown keys it's hard to distinguish where the output of previous command ends and output of current command begins.

Is there a way to really clear the terminal so I won't see previous command results?

Stann
  • 15,116
  • 20
    Press Ctrl+L twice and you get blank! – Pandya Jul 30 '14 at 12:19
  • 4
    On mac os and iTERM, cmd + k does the trick for me. – John Jiang Feb 03 '21 at 00:53
  • ctrl+L twice works on mac os x vscode integrated terminal – John D Mar 06 '21 at 10:06
  • To clarify the MacOS commands: cmd.K - clear terminal, can't scroll up || ctrl.L - clear screen, scroll up for history || multiple ctrl.L - clear multiple screens. Can see the empty prompt for each clear, and will be able to see history prior to the number of clears – lodeOfCode Feb 12 '22 at 23:01

12 Answers12

592

Yes, the command you're looking for is

reset

In contrast to clear, or Ctrl+L, reset will actually completely re-initialise the terminal, instead of just clearing the screen. However, it won't re-instantiate the shell (bash). That means that bash's state is the same as before, just as if you were merely clearing the screen.

As @Ponkadoodle mentions in the comments, this command should do the same thing more quickly:

tput reset

From the other answers:

  • You can set a Keyboard Shortcut to reset the terminal, as explained by towolf.

  • If you're running Kubuntu, and your terminal is Konsole, you need to go to Edit → Clear history, since reset doesn't work the same way there, as UncleZeiv notes.

  • 11
    Probably doesn't matter for most users, but the behavior of reset is dependent on the terminal. – cthom06 Feb 07 '11 at 13:44
  • 2
    Personally I don't mind that something is left "up" along the scroller when I hit ^L. I don't see it (that's what I want!), I don't feel it, and if suddenly I would need to restore what was there — no problem. reset in contrast is much harder tool — I'll use it when something went wrong with the terminal — like, when weird escape sequences accidentally ruined the display altogether. – ulidtko Feb 07 '11 at 13:56
  • 21
    @ulidtko: it does matter when you run consecutive sessions of programs with tons of output. If you scroll back it's easy to get confused about which execution printed something. I know perfectly well that more sophisticated solutions could be used in these cases, but nonetheless it's a scenario that comes about pretty often in quick & dirty debugging sessions. – UncleZeiv Feb 09 '11 at 12:49
  • @UncleZeiv colored prompt anchors usually help me to quickly find the needed spot. Though this is all just habits. – ulidtko Feb 09 '11 at 12:56
  • source ~./bash_profile to reset the bash. – Dummy Code Aug 13 '13 at 21:09
  • 14
    There's also tput reset which visibly does the same thing, but completes instantaneously (whereas reset can take up to about 2 seconds before the prompt reappears). – Ponkadoodle Mar 04 '16 at 22:57
  • 1
    This causes my putty.exe window to resize, would be nice to clear without that happening. – void.pointer Aug 03 '17 at 20:11
  • For anyone finding this answer via Google while they're actually on macOS & iTerm 2, it's possible to alias reset like alias reset="reset && printf '\33c\e[3J'" to get the same behavior. – Borek Bernard Oct 02 '19 at 07:26
  • How does this behave if I'm in an ssh session? – Cruncher Jan 22 '20 at 16:21
  • @Ponkadoodleyour comment would be improved by adding why it is faster using tput reset vs just reset, thanks! – Elijah Lynn May 06 '20 at 06:46
  • On Fedora clear works fine, but on Ubuntu doesn't, so I just use alias clear="tput reset" on Ubuntu. – jcubic May 20 '21 at 09:24
  • tput reset does nothing for me – Eugen Konkov Aug 08 '23 at 15:22
  • The reset and clear commands both just uselessly scroll. Neither one clears the terminal. – Andrew Koster Jan 06 '24 at 03:26
  • If tput reset doesn't clear your terminal, you may need to do printf '\033[3J\033c' instead but that is not portable to all terminals either. – Mikko Rantalainen Feb 13 '24 at 15:23
186

I was looking for this for a while and I found some genius that posted this:

clear && printf '\e[3J'

Clears the whole screen buffer, very clean. Works on OS X and believe it works fine on most *nix terminals.

For curious, this part '\e[3J' is a terminal escape command.

Goran
  • 1,961
  • 3
    Hmm, works in xterm, Konsole and Linux VT, but doesn't in gnome-terminal... – Ruslan Jun 24 '14 at 13:02
  • 1
    Best answer. Even purges tmux history (as in Ctrl+b PgUp; reset fails to do so; tested in rxvt-unicode/urxvt; tmux-specific alternative: clear && tmux clearhist). – user569825 Jul 28 '14 at 09:52
  • 11
    Thanks, that's super useful! What does printf '\e[3J' mean? How does it clean the buffer? – Filip Spiridonov Jul 24 '15 at 17:03
  • 3
    Great find! And for those who want the Bash-style reset on mac: nano ~/.bashrc and add alias reset="clear && printf '\e[3J'" – Michael Deal Aug 12 '16 at 05:43
  • 4
    be careful aliasing reset -- it does more than just clear the terminal. for example, if you accidentally print a binary file with random garbage that corrupts the terminal (e.g. disables echoing of typed characters), reset can usually fix that. of course aliasing is fine as long as you know how to \override an alias-shadowed command. – RubyTuesdayDONO Oct 03 '16 at 12:51
  • 7
    This is the only answer that works to clear Putty scrollback when accessing some SSH via Winders. – dhaupin Jan 19 '17 at 15:56
  • 2
    Can also be written as: printf '\033c\e[3J' – ideasman42 Jul 23 '17 at 10:03
  • @FilipSpiridonov: This is a terminal escape code, similar to the codes that control color of text in your terminal. Some more info at this other answer: https://superuser.com/questions/555554/putty-clear-scrollback-from-commandline – jwd Oct 10 '17 at 14:44
  • 1
    Works on the Linux Sybsystem on Windows 10! I aliased cls to this command, so as not to mess with clear or reset. – jedmao Feb 08 '18 at 16:40
  • 1
    Among all answers here, only this one work exactly list cmd-k on mac OS. FYI, I am using Mohave 10.14.3. – Siu Ching Pong -Asuka Kenji- Feb 15 '19 at 01:32
  • Great! It also works for linux subsystem in windows! – ch271828n Feb 22 '19 at 02:20
  • 3
    @Filip Spiridonov, \e[3J is the escape sequence that "Erase Saved Lines, xterm." (i.e. the scroll-back). Reference: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html (search for CSI Ps J) – Johnny Wong Feb 28 '20 at 04:32
  • Works on Ubuntu 22.04 :) – Chef Pharaoh Nov 24 '22 at 14:02
  • Unlike the "accepted answer", this one does something. – Andrew Koster Jan 06 '24 at 03:27
  • This one helped me, thanks! :^) – Cheejyg Feb 01 '24 at 09:48
  • You can also append in your ~/.bashrc or ~/.zprofile with alias clear="clear && printf '\e[3J'" – Cheejyg Feb 01 '24 at 09:52
51

You can also assign a shortcut in gnome-terminal by going to Edit → Keyboard Shortcuts. I useShift+Ctrl+Alt+C.

reset and clear shortcut

towolf
  • 667
  • +1. I always have the Menubar hidden; so even after years and years of using gnome, I never thought to look for this. Thanks :-) – Stefano Palazzo Feb 09 '11 at 12:53
  • 7
    “Reset” do nothing for me, and “Reset and Clear” clear everything but does not re‑display the prompt. None of these menu entries behave like the “reset” command from a terminal. – Hibou57 Jul 09 '14 at 08:50
  • As @Hibou57 said, my keyboard shortcut for "Reset" doesn't seem to do anything. Is this functionality broken in 14.04? – Michael Martin-Smucker Aug 23 '14 at 20:37
  • “Is this functionality broken in 14.04?”: this is the same with 12.04. – Hibou57 Aug 24 '14 at 02:53
  • 1
    @Hibou57 "Reset and Clear" isn't expected to re-display the prompt, but if you hit Enter afterwards it should re-display the prompt. Does that work for you? – Don Hatch Nov 09 '16 at 02:32
  • The other answers only work if you're in the shell; this is the only one that works if you're in some other program (e.g. repl). Alternatively in Ubuntu 20.04 you can click burger => advanced => reset and clear – Andy Dec 24 '20 at 09:10
25

Cross posting my answer from stackoverflow.

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.

19

run this command:

reset

This has the same effect as launching a new terminal.

dv3500ea
  • 37,204
11

My favorite is printf "\ec". This can also be printf "\033c" or printf "\x1bc". That is an ansi escape sequence that miraculously clears the screen and buffer for the terminal output (for most standard terminals I have worked in it seems - I know it works in such as gnome-terminal, terminator, xterm, etc. on Ubuntu-like Linuxes)

I know this works in Linux Mint and Ubuntu 14.04, so I don't know why people are appending and prepedning things like clear && echo -ne "\033c. printf "\ec" has always worked for me.

Additionally, in my .bashrc I have a binding like this:

bind -x '"\C-t": printf "\ec"';

Now I press ctrlt all the time.

dylnmc
  • 266
  • Do you know how could a mapping be created using the Windows key ? This snippet is really great, works exactly like Command-K on mac. Thank you. – Niloct Jan 12 '17 at 15:47
  • @Niloct you can see if the terminal supports it with xev (just type xev in a terminal inside an x-session). From what I could tell, urxvt doesn't support it; while I didn't try in a tty, I am confident it would not be supported. I also doubt xterm or gnome-terminal would support this. It's tricky with ansi-escapes. Cheers and sorry for late reply. – dylnmc Feb 03 '17 at 17:34
  • Alright @dylnmc, I appreciate it. Thanks for the comment. – Niloct Feb 03 '17 at 18:19
9

Here are all the ways you can clear the terminal screen in Unix:

clear               # only clear visible screen
clear && clear      # clear buffer as well
tput clear          # same as clear but by sending escape seq
reset               # clear + reset internal terminal state + 1sec delay
tput reset          # same as reset but without 1sec delay
stty sane           # don't clear screen but reset some terminal options
echo -e "\033c"     # same as tput reset but hardcoded escape seq
printf "\033c"      # same as tput reset but hardcoded escape seq
setterm -reset      # same as tput reset, setterm has friendlier commands
5

Well it really depends, if you are using Konsole for instance even reset doesn't prevent you to scroll past the first line. In that case you have to use some feature of the particular terminal you are using. For Konsole, it's Edit > Clear History.

UncleZeiv
  • 511
5

I have found the following to be quite successful with Kubuntu:

alias clc="echo -en '\033c\033[3J'"

You can use cls to keep with the Windows tradition, use the Matlab version clc, or one of your own. Good luck!

TheSchwa
  • 3,820
Charles Bell
  • 51
  • 1
  • 2
5

If you're using yakuake or Konsole, try Ctrl+Shift+K.

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

David Foerster
  • 36,264
  • 56
  • 94
  • 147
3

Alternatively to reset, you can do echo -e "\033c", or have in your .bashrc, something like alias cls="echo -ne '\033c'"

The UNIX/Linux StackExchange, has a thread on this: How to clear terminal completely? (unix.stackexchange.com), as well as SuperUser : Clear terminal using keyboard shortcut (superuser.com).

Hibou57
  • 1,195
3

Another alternative to the @Hibou57 answer:

# The Octal Way
alias clear="echo -e '\033c\c'"

OR

# The Hex Way
alias clear="echo -e '\x1b\c'"

Basically this will have exactly the same effect, it will print the ^[ character (which clear your terminal) and does not output the trailing new line.

muru
  • 197,895
  • 55
  • 485
  • 740
TwystO
  • 142
  • Slight correction: it prints the ^[ character (ESC) followed by the c character, not just the ^[ character alone. Combined, these amount to the "RIS" terminal code, which causes a reset. More info: https://www.xfree86.org/4.8.0/ctlseqs.html – jwd Oct 11 '17 at 15:29