77

How do you clear the entire terminal in BASH, like the command prompt's cls command?

clear doesn't work because it doesn't actually clear anything, it just scrolls down.

braX
  • 11,506
  • 5
  • 20
  • 33
user541686
  • 205,094
  • 128
  • 528
  • 886
  • What do you mean "it just scrolls down"? You actually want to not be able to scroll up to see what was there before? What terminal are you using? – Random832 Jun 16 '11 at 04:20
  • 8
    What about "reset"? – Alex R Jun 16 '11 at 04:23
  • 1
    @Random832: I mean it just literally scrolls down, instead of clearing anything. (I'm using Ubuntu's default terminal, which I believe is gnome-terminal, sorry I forgot to mention that). @Alex: I'd never heard of `reset` -- it seems to do exactly what I need, but is that *all* it does? Or does it reset other things too? – user541686 Jun 16 '11 at 04:46
  • I guess my question is, why is "just scrolling down" a bad thing? The _screen_ is cleared. the stuff you scroll up for isn't part of the screen. – Random832 Jun 16 '11 at 13:25
  • 9
    @Random832: Because it's annoying when I subsequently scroll up to go back to where it started, since it's harder to find the beginning. – user541686 Jun 16 '11 at 14:31
  • 1
    `tput reset` does – it3xl Nov 25 '17 at 10:37

9 Answers9

48

As far as I know, there isn't a way to do this any better than what clear does with bash.

I think it's a feature that could be built into the terminal you're using though. I know the Mac Terminal app has a 'Clear Scrollback' menu option (command + k) that does what you're asking for.

DouglasHeriot
  • 1,674
  • 2
  • 13
  • 19
  • +1 thanks for the info. I still need to get the difference between "shell" and "terminal" straight. :) – user541686 Jun 16 '11 at 04:47
  • 2
    `shell` -> The software that is running. `terminal` -> A part of the computer, usually made out of glass, that you can throw. `virtual terminal` -> A piece of software that is acting like a terminal. (xterm). – Jeremy J Starcher Sep 27 '12 at 18:20
46

Why don't you try Ctrl+l (control, lowercase "L"). This works in most shells (err terminals)...

In OSX terminal -

Command ⌘+l (command, l) leads to removing last typed command from display.

Command ⌘+k (command, k) leads to removing/clearing all display buffer.

reset (type this in terminal) leads to reset of terminal in case display becomes garbled.

not sure of equivalent in other unix flavors.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
23

You're probably looking for the reset command.

However, the scroll-back buffer is not a feature of bash but of the terminal program. You didn't say what terminal program you were using.

user541686
  • 205,094
  • 128
  • 528
  • 886
zad
  • 3,355
  • 2
  • 24
  • 25
19

Short Answer

clear && clear

or

tput reset

Other Ways

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

Long Answer

The clear command only clears the visible screen but not the buffer so you can do Shift+PageUp to scroll up in the terminal and still view previous outputs. If you want to get same result as cls then do clear twice like clear && clear.

Another related command is reset which (I believe) resets the internal state of the terminal program. Unfortunately, this command includes 1 second of delay to support really old terminals. So if you are not ok with that kind of delay then use tput reset which seems to do same thing as reset minus the delay.

But what does tput do? In Unix, you can send terminal all kinds of ASCII character sequences which are interpreted as commands by the terminal. This allows you to do funky things like blink or color the text or turn off echo (during password typing) or set terminal options or do clear or reset. This you can send by tput clear or tput reset. The clear and reset command are equivalent but they run from the binaries that comes with your distro and may do additional stuff. The setterm -reset is similar to tput reset. Setting terminal using setterm is usually better because unlike tput it has more readable options in general case however we here use tput because it's smaller in length :).

You might have also seen people using things like echo -e "\033c" or printf "\033c" which is equivalent to tput reset but the escape sequence is now hard coded. The tput looks up terminal properties and uses the right escape sequence.

Another related command is stty sane which actually doesn't do any screen clearing but it sets many of the terminal options to defaults so if your terminal looks garbled or if terminal stays blank when you type (for example, because you printed binary file to terminal with escape sequence to turn off echo) then this command might help. For extreme garbled terminal cases, you can use all of the available resetting techniques in the sequence. I've alias like this for such occasions:

alias cls='tput reset'
alias clshard='reset; stty sane; tput rs1; setterm -reset; tput reset'

Related

What's the equivalent of the “cls” command from Windows/DOS?

What commands can I use to reset and clear my terminal?

Shital Shah
  • 63,284
  • 17
  • 238
  • 185
  • is there a command that achieves a similar effect of scrolling down into empty space until the current input is at the top of the window, but without deleting the history above it? – rojobuffalo Dec 23 '20 at 18:03
  • Unfortunately none of these are remotely as fast as Cmd-K (on Mac) which is instant. They all take at least around a third of a second. It doesn't seem to be possible to do it quickly. – Timmmm Jun 09 '21 at 17:25
17

xterm will allow the escape sequence ESC [3J to clear the scroll back, so you could do:

alias cls="clear; printf '\033[3J'"
Random832
  • 37,415
  • 3
  • 44
  • 63
9

Use +K. It removes the entries so I can't scroll up anymore.

So +K to clear everything including scrolling. Ctrl+L to clear terminal window but still be able to see everything when scrolling up.

Pedro Lacerda
  • 1,098
  • 9
  • 23
Ami
  • 495
  • 1
  • 4
  • 13
0

In ~/.bashrc, the perfect cls is:

cls () {
    printf -- '%b' '\033c'
    return $?
}
Mario Palumbo
  • 693
  • 8
  • 32
0

The clear command works for me.

But I personally find it impractical, because, for me, it clears the scrollback permanently and irreversibly. However, often I want just to insert some "marker"/"separator" into the scrollback, in order to be able to visually distinguish the "recent scrollback" from the "too old scrollback" (but sometimes ability to see the "too old scrollback" would be still useful). So I use something like:

yes '' | head -n100

That inserts 100 empty lines into the scrollback. (Inspired by this answer. You can vary the number of lines, of course.)

Sasha
  • 3,599
  • 1
  • 31
  • 52
0

If you're using Windows Terminal you can map the "clearBuffer" action to the keyboard shortcut of your choice. See https://learn.microsoft.com/en-us/windows/terminal/customize-settings/actions, search for clearBuffer.

If you're on Windows and not using Windows Terminal, well, IMO you should be. To host bash inside Windows Terminal, add this to the "profiles" list in the Windows Terminal settings json:

    {
        "commandline": "C:\\Program Files\\Git\\bin\\bash.exe",
        "guid": "{cfef8f0b-5c87-412c-b9ec-0fe1c1befdcf}",
        "icon": "C:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico",
        "name": "git bash",
        "startingDirectory": "%USERPROFILE%\\documents"
    },
Darryl
  • 5,907
  • 1
  • 25
  • 36