2

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?

  • I think that is specific to terminal and not clear command... I use Gnome terminal 3.18.3 and it retains the scrollback buffer.. I also enable an option that rewraps lines on resizing the terminal... – Sundeep Oct 23 '16 at 4:07
4

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.,

clear=\E[H\E[2J

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:

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 -1 and -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/xterm would be used by ncurses
  • the capabilities AX and XT are extended capabilities (like E3), shown with the -x option.

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),

For instance, uxrvt sets $TERM to something like rxvt-unicode, producing lines like this in infocmp:

#       Reconstructed via infocmp from file: /lib/terminfo/r/rxvt-unicode       
rxvt-unicode|rxvt-unicode terminal (X Window System),

The 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 hpa sequence) move right a large number,
  • issue the "clear above", and
  • return to the original position using cup (cursor addressing).

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.

  • I followed your instruction, but the problem remains. – Alvo Oct 24 '16 at 11:51
  • What does infocmp -x show? (It shouldn't show the E3 capability). – Thomas Dickey Oct 24 '16 at 20:44
  • Thank you for the detailed answer. When I mention "from the top of the screen to the current line", I meant from the top of the visible part of the screen. Since my question is no longer related to the scroll back buffer, I'm wondering how I could preserve the top of the visible part of the screen to the current line whenever I clear the screen. I had tried perl -e'print "\33[H\33[1J"'(changing the numbers according to the list you given above), but all of them deletes the visible part of the screen to the line where I issued the command. – Alvo Oct 26 '16 at 4:54
  • I tried all four values with the clear sequence e.g. clear=\E[H\E[0J, but I couln't change the behavior of clear. I tried the same in xterm but it didn't work either. But clear and ctrl-l did stop working if I remove the line with clear. – Alvo Dec 13 '18 at 1:49
  • The "\033[H" moves the cursor to the upper-left corner of the screen. If you omit that, clear-below (0) should not affect the "above" portion (except for Windows console....). – Thomas Dickey Dec 13 '18 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.