When I use clear
command to clear the screen. It is not cleared (see screenshot when I scroll up a bit after command done)
So I double the command to get right behavior:
$ clear && clear && DD=0 ...
Why do I need to double the command to get cleared screen?
UPD
Actually if I just clear
I got cleared screen. But I can scroll up and see last 25lines (if screen is 80×25). When I run clear;clear
I got those lines cleared.
Best Answer
The important thing to note here is the tag on the question. This behaviour is specific to GNOME Terminal and any other terminal emulators that are built upon libvte. You won't see this in Xterm, or in Unicode RXVT, or in the terminal emulator built into the Linux kernel, or on the FreeBSD console.
What happens in general is this.
clear
command looks at terminfo/termcap and issues appropriate control sequences.E3
capability it, it first writes out that. This issues control sequences to clear the scrollback buffer. This and the history behind it are documented in detail in the Dickey ncurses manual page for theclear
command.clear
capability to clear the visible screen.putty
entry definesE3=\E[3J
which is the Xterm extension control sequence.pcvtxx
entry is one of many that defineclear=\E[H\E[J
or something similar. This is two ordinary ECMA-48 control sequences.H
(CUP) homes the cursor.0
J
(ED 0) or just CSIJ
erases from the current cursor position to the end of the screen.2
J
(ED 2) erases the whole screen.3
J
(ED 3) erases the scrollback buffer.When it comes to GNOME Terminal in particular:
gnome
, but some people leave it erroneously set toxterm
.gnome
terminfo entry does not define anE3
capability, and on many systems — still! — neither does thexterm
entry as this has not percolated down from Dickey terminfo. Soclear
just writes out the contents of theclear
capability.clear
capability for those terminfo entries are the control sequences to home the cursor followed by erase the whole screen.VteTerminalPrivate::seq_clear_screen()
function. Rather, libvte scrolls the screen down an entire screen's worth of blank lines, and moves the cursor position to the first of those blank lines.This is why you see what you see. libvte is not erasing the whole screen when told to. Rather it is doing something that has a superficial resemblance to that, until one does exactly what the questioner has done here: scroll the terminal window back to look at the scroll back buffer. Then the difference is blatant.
On other terminal emulators such as Xterm and Unicode RXVT, the ED 2 control sequence really does erase the screen, erasing every position on the screen in place, from the top down, and not altering the scrollback buffer. But on libvte terminal emulators, it just pushes the current screen up into the scrollback buffer and adds a screen's worth of blank lines. The prior screen contents are not erased but shifted into the scrollback buffer.
And if you run the
clear
command twice, it adds two screen's worth of blank lines. If your scroll back buffer is large enough, you can still find the original screen contents, simply further up in the scrollback buffer.Further reading