Whenever I run clear in my terminal, it deletes the scrollback buffer from the top of the screen to the current line. I have tried it in xterm, st, and urxvt, and the problem remains. Is there any way I can change the behavior of clear so it does not touch the scrollback buffer?
The comment "from the top of the screen to the current line" is ambiguous. If you meant from the top of the visible part of the screen, that is not the scrollback. The scrollback of a terminal is the part that you can see only by using the scrollbar (or suitable keys such as shift pageup).
XTerm Control Sequences documents the relevant escape sequence:
CSI Ps J Erase in Display (ED). Ps = 0 -> Erase Below (default). Ps = 1 -> Erase Above. Ps = 2 -> Erase All. Ps = 3 -> Erase Saved Lines (xterm).
The terminal description capability
clear uses the next-to-last one, e.g.,
to position the cursor to the upper left and then clear the whole (visible) screen. You could use the Erase Below, but that is not used in the terminal description.
Referring to clearing the scrollback: That's a terminal-specific feature, originally an escape sequence in xterm (1999, documented in ctlseqs.ms but not mentioned in changes) and later (2011) implemented as an extension for Linux console and the corresponding terminal description. The terminal database lists it as a "miscellaneous extension".
Currently, these terminal descriptions have the feature:
- linux3.0 (the current default for "linux")
- xterm+basic (a building block used in most "xterm" variants)
Whether it is supported in xterm look-alikes such as VTE would have to be answered by testing (there is no useful documentation for VTE or Konsole).
If you prefer to not use the extension, you could remove the
E3 capability from the terminal description which you use, e.g.,
infocmp -1x >foo edit foo, removing the line with "E3=" tic -x foo
I suggested using the options
-x to simplify the formatting and to show the feature to change. The example given in https://ghostbin.com/paste/kfsbj is consistent with that advice:
- the pathname
/home/flowerpick/.terminfo/x/xtermwould be used by ncurses
- the capabilities
XTare extended capabilities (like
E3), shown with the
If you are using more than one terminal type, you would have to do this for each (value of
$TERM), and the change only applies to the machine where you run
clear. The first couple of lines of the
infocmp output show which one you are working on:
# Reconstructed via infocmp from file: /home/flowerpick/.terminfo/x/xterm xterm|xterm terminal emulator (X Window System),
$TERM to something like
rxvt-unicode, producing lines like this in
# Reconstructed via infocmp from file: /lib/terminfo/r/rxvt-unicode rxvt-unicode|rxvt-unicode terminal (X Window System),
st program uses
xterm (or possibly
xterm-256color), though it's been a while since I saw a copy of that which worked well enough to comment upon.
By the way, you could have an alias for
clear which is sending the given escape sequence (ignoring the terminal description), but I haven't seen this reported by anyone.
If you wanted to "clear above", that is not as straightforward as typing "clear". The escape
\033[1J erases from the upper-left to the current cursor position. You could make a script which does this, to clear only the lines above your current cursor:
- use the cursor position report to find the row/column on which the cursor currently is, and
- if the cursor is not on the first line, (saving that position), move the cursor up one line and then (with the
hpasequence) move right a large number,
- issue the "clear above", and
- return to the original position using
That part with the cursor position report doesn't seem as if it would work in (for example) a readline binding, so I suggested a script. You could make a binding that used the save/restore cursor capabilities if there were not the problem of being on the first line.