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.
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.
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.
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.
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.
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?
xterm
will allow the escape sequence ESC [3J
to clear the scroll back, so you could do:
alias cls="clear; printf '\033[3J'"
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.
In ~/.bashrc, the perfect cls is:
cls () {
printf -- '%b' '\033c'
return $?
}
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.)
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"
},