5

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.

  • With relatively newer clear command, you can use clear -x to avoid the issue. See my answer for the detail. – ynn Oct 18 '19 at 11:37
8

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.

6

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

  • 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_syndrom Jul 27 '18 at 11:48
  • You can do the tic command without "sudo" and it will store the result in ~/.terminfo instead of globally. – Eyal Jul 9 '19 at 20:08
4

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

1

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

0

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 the procedure is valid on Arch Linux and Linut Mint 19. However, on Raspbian Stretch Lite, that is invalid by default because the ncurses-base package supplied via apt is old. I don't know about newer Raspbian Buster Lite.

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.