Exorcising the Evil Alternate Screen

From a Linuxchix posting:
> Using the (I assume bash) terminal window on my linux desktop (gnome on
> fedora core 3) there is this really annoying "feature" configured - when
> I view a document (with vi, view, man, who knows what else) and then
> exit out of the document, the screen clears.  This bugs the crap out of
> me because a lot of time I'll page through a man page until I find what
> I was looking for, and then quit out of it, expecting the stuff I was
> just looking at to stay on the page (and in the scroll buffer) for
> reference.  I can get around it by piping the output through "more" but
> I'm thinking I shouldn't have to do that, just because I don't have to
> do it in bash on my solaris boxes. ;)
> Where do I look to configure this?

First, many programs let you turn this off in the program itself. If it's only bothering you in a few places, that might be enough. In less, use the -X option, e.g. in your .bashrc

export LESS="-X"
In vim, add this to your .vimrc:
set t_ti= t_te=

But for me that still left some other programs, such as mutt, that I didn't know how to configure. Wouldn't it be better to fix it in one central place?

How to do that depends on what terminal you're using.

Terminal Program vs. Terminal Type

Some background: your terminal's behavior is controlled at two different places, and it's important to know the difference.

First, there's the terminal program you're running (usually on your local machine, though with remote X you can run terminal programs remotely, displaying to your machine). This is something like xterm, gnome-terminal, rxvt, eterm, or konsole.

Second, there's your terminal type. The terminal type is a description of which escape sequences do what: like ctrl-L clears the screen, ESC-[-H puts the cursor "home" (at the top left), ESC-[-1-m makes text bold, and so forth. The terminal type is what you see when you look at $term or $TERM in your shell or your environment variables. If you're remotely logged in to another machine, this is set on the remote system, wherever your shell is (and programs such as vi and man are).

Configuring Your Terminal Program

If you're using xterm or any variant of it, such as rxvt, as your terminal program, all you need to do is set an X resource. Put this in your .Xdefaults or .Xresources:

XTerm*titeInhibit: true

The newer urxvt (rxvt-unicode) uses a different resource:

urxvt*secondaryScreen: false
but even with that set, it still clears the screen on exit. To fix that, continue on to the next section.

(You also need to make sure that your X resources are being read. Some distros using graphical login programs like gdm ignore .Xresources. If you're not sure, try running xrdb -l .Xresources, then run xterm and see if titeInhibit is now working.)

"tite" seems like a strange term; it comes from the fact that in the old "termcap" way of specifying terminal capabilities, the string to save and restore the "alternate screens" were called ti and te (I'm sure those stood for something, but if I ever knew what, I've forgotten).

Unfortunately, many modern terminal emulators such as gnome-terminal have decided for our own good that users should not be allowed to make choices. As far as I was ever able to determine, there's no way to tell gnome-terminal to disable the alt screen behavior. And vim and man don't seem to have a way to disable that behavior either.

Tweaking Your Terminal Type

But wait! Don't lose hope yet. Because programs such as vi and man use the "terminfo" database (which does the same thing as termcap used to) to determine what sort of terminal they're talking to and how to tell that terminal to save and restore the alternate screen.

It turns out that instead of ti/te, the capability "rmcup" is the one that represents this behavior in terminfo (you can get a little info on it by googling on rmcup, and smcup is related).

So you can trick them by making a terminfo entry that has no rmcup entry at all, and using that instead of the standard entry. You're pretending that you're using a terminal which doesn't have that feature at all.

I've written a terminfo entry that emulates "xterm-color" but removes the entries which implement rmcup and smcup. Download it here: xterm-noalt.terminfo.

To use it, make a directory in your home directory called .terminfo (don't forget the leading dot), and put the file there. Call it xterm-noalt.terminfo. Then type:

tic ~/.terminfo/xterm-noalt.terminfo 
which will compile it and stick it somewhere under ~/.terminfo where programs will see it.

Then set TERM to xterm-noalt (in csh, setenv TERM xterm-noalt; in bash, export TERM=xterm-noalt) and you should be good to go.

If you use urxvt, I have a fixed version of rxvt-unicode-noalt.terminfo too. urxvt already lets you disable the "secondary screen", so the change that terminfo needs is to remove the is2 and rs2 initialization sequences that clear the screen. I removed smcup and rmcup too, while I was at it.

My Linux links page
Shallow Sky home