Prevent the 'clear' command from destroying the scroll-back buffer
Terminals come in many different shapes and sizes. Some posses only the most bare essentials, some feel like a full fledged operating system. This versatility is great, there is aways something for everyone. But when the capabilities of your terminal have fundamental impact on some of your most used commands, things can get frustrating.
This was an issue I ran into while updating my linux distro to the latest version. Suddenly, my clear command was no longer simply pushing my terminal output to the top of the screen, it was completely clearing the scroll-back buffer.
For someone who knew nothing about terminal capabilities at the time, fixing this turned out to be quite painful. So here I present the fix for anyone unfortunate enough to end up in the same situation.
The nugget
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).
A quick search for the E3 termcap capability gives us:
E3 string, tells how to clear the terminal's scrollback buffer.
When present, the clear(1) program sends this before clearing
the terminal.
Perfect! This is what we are looking for. Lets remove the E3 capability from our favorite terminal. Start by figuring out the terminal type:
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 scroll-back buffer.
