4

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.

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.

5

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 at 20:08
2

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.

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.