17

Initially I thought Unlimited scrolling was not working with the terminal application on 16.04. I have the option "limit scrollback to" unchecked. Some times it scrolls back some times it does not. Then I realized it does not scroll back when ever I use clear command. It scrolls back only one scree.

I use clear a lot(almost once in every 3 commands).

The man page for clear points to terminfo. There are some options on terminfo but I can't find a config file where I can define the options. All the files under /lib/terminfo/ from where clear seems to be reading its config are in compiled format and I do not understand them.

I didn't see this issue with earlier to 14.04(guessing).

Any help is greatly appreciated.

Thanks.

stingray
  • 338

7 Answers7

20

This took me a while to figure out so I guess I should share how I got this to work.

If you type "man clear" you will see that the manual states:

clear clears your screen if this is possible, including its scrollback buffer (if the extended "E3" capability is defined).

We are going to remove this E3 capability:

First, find out the type of your terminal:

echo $TERM

For me this resulted in "xterm-256color". Whatever it outputs, remember it.

Now enter the command:

infocmp -x xterm-256color > tempfile

Where you obviously replace xterm-256color with the output from the first command. This will output the extended capabilities for this terminal type to 'tempfile'.

Now edit this newly created file. You are looking for:

E3=\E[3J,

Find this and just remove it. The entire thing, so if it looked like:

    ...
    Cs=\E]12;%p1%s\007, E3=\E[3J,
    Ms=\E]52;%p1%s;%p2%s\007, Se=\E[2 q, Ss=\E[%p1%d q,
    ...

It should now look like:

    ...
    Cs=\E]12;%p1%s\007,
    Ms=\E]52;%p1%s;%p2%s\007, Se=\E[2 q, Ss=\E[%p1%d q,
    ...

Save the file. And from your terminal execute:

sudo tic -x tempfile

This will load your modified terminfo and store it. restart your terminal and clear should now no longer remove the scrollbuffer

polle
  • 336
  • 1
  • 7
  • This is a good solution and worked for me. I was trying how to change the terminfo entries (all the files in /usr/share/terminfo/*/* on CentOS) are binary and it wasn't obvious how to change those. – irritable_phd_syndrome Jul 27 '18 at 11:48
  • 6
    You can do the tic command without "sudo" and it will store the result in ~/.terminfo instead of globally. – Eyal Jul 09 '19 at 20:08
  • This is exactly what I was looking for. I am used to having scrollback on macos, and switching back to Ubuntu, having my scrollback deleted is annoying, so this fixed my issue! – Prithvish Baidya May 17 '22 at 20:47
  • Appreciate your sharing of knowledge here. I have been slowly but surely gaining ground on this arcane terminal knowledge over the past 15 years. I plan to make this change to all my terminal configs, because there’s nothing to be gained by clearing scrollback. I originally wanted it to counter Create React App’s devserver behavior which also clears, like clear does. I do also like to use clear, but only when creating a screencap video, so it hasn’t bothered me much. – Steven Lu Aug 18 '23 at 00:59
15

What you want is to type CTRL+L instead of clear.

This will send a "Form Feed" to the terminal. Basically it will move everything up the height of the terminal window clearing the screen without affecting your scrollback.

heemayl
  • 91,753
bashBedlam
  • 1,022
7

Short Answer: If you have relatively newer version of clear, you can avoid the scrollbuffer's being cleared by -x option.


Long Answer:

clear commans is a part of ncurses.

According to the changelog,

20130622
        + modify the clear program to take into account the E3 extended
           capability to clear the terminal's scrollback buffer (patch by
           Miroslav Lichvar, Redhat #815790).

thus OP's guess

I didn't see this issue with earlier to 14.04(guessing).

may be right. The changelog also says

20170819
        + add -x option to clear/tput to make the E3 extension optional
           (cf: 20130622).

So you can use -x option if the release date of ncurses is equal to or newer than 2017-08-19. This can be checked by clear -V command. For example,

$ clear -V
ncurses 6.1.20180127

and this can be read as "ncurses 6.1 released on 2018-01-27".

I confirmed, as far as I use the official repositories, the procedure is valid on

  • Arch Linux

  • Arch Linux ARM

  • Linut Mint 19

  • Ubuntu 20.04

  • Raspbian Buster Lite

and invalid on

  • Raspbian Stretch Lite (This is older than Buster.)
ynn
  • 343
  • 3
  • 9
7

This answer builds off of stingray's answer (which he did some really good work on) and is meant to complete it.

1 - To clear without loosing scrollback, enter the following command in console (no need for python as suggested in stringray's answer):

printf '\33[H\33[2J'

2 - To avoid having to memorize this, you can edit your .bashrc file to make an alias for it. I would call the alias clear. In bash, enter:

nano ~/.bashrc

And add this line at the end:

alias clean="printf '\33[H\33[2J'"

I also like to add div (for divider):

alias div='echo;echo "------------------------------------------------------------------------------";echo;echo;echo;echo;echo;echo;echo;echo;echo;echo;echo;echo "------------------------------------------------------------------------------";clean'

This makes it so that when you do the div command, it enters two dividers with 10 new lines between them, followed by a clean command. This will make so that when you are scrolling back, you'll know exactly where you used div.

You can change the sudo bash behavior by doing sudo su before the procedure I listed.

I would recommend this over bashBedlam's answer of using tic, as editing .bashrc:

1) it doesn't require sudo privileges and can be easilly taken on the go.

2) only affects your user (not all users will want the modified clear function)

3) will survive updates which usually don't touch bashrc

thebunnyrules
  • 1,083
  • 1
  • 13
  • 20
5

As mentioned above, clear -x does the trick.

However, if you're like me and you habitually type clear hundreds of times a day with no hope of untraining your fingers, you may want to create a simple alias in .bashrc (or equivalent) like this: alias clear='clear -x'.

2

@heemayl and @bashBedlam Thank you.

CTRL+L does what I want. But I am used to typing clear all the time. So I kind of worked around it by copying /lib/xterm/x/{xterm, xterm-256color} from ubuntu 12.04. There is an environment variable named $TERM that stores this file. Then I read your answer and tried CTRL+L and it worked. I got curious and was trying to see if I can put CTRL+L in a script and got no where. Then I remembered strace might give me what I want. So I ran strace clear and found that it writes "\33[H\33[2J" to clear the screen. So I wrote a python oneliner print("\33[H\33[2J") and put it in a file and added a link named clear to point to it. Got 2 workarounds now. I am still curious of how terminfo files can be read and changed.

Thanks again.

stingray
  • 338
0

For me, I do not have an issue with a locally run clear, but if I ssh into another host, and a clear is run there, it trashes my scroll history, very bad; a remote machine has way too much access IMO.

I ended editing konsole source (21.12.1). File src/Vt102Emulation.cpp with this:

========================

  • case token_csi_ps('J', 3) : clearHistory(); break;
  • case token_csi_ps('J', 3) : /* IGNORE clearHistory(); */ break;

========================

This makes konsole do nothing if it sees the \E[3J string that fubars the scroll history.