A custom terminfo to restore 10.2 Terminal behavior

Dec 29, '04 08:37:00AM

Contributed by: jpf

I can't quite remember exactly when this changed, I think most likely during the transition from 10.2 to 10.3, but I'm no longer sure. In any case, Terminal used to behave in a (to me) reasonable manner when using such applications as vi, less and top. These programs and probably others will use the (emulated, e.g., xterm) terminal's "alternate screen buffer" if available while they do their work, and restore the previous screen contents when they exit. The problem is that most of the time, I don't want them to do this. In fact, I want just the opposite, especially if i want to scroll back through that man page I just read, etc.

The Problem:
Back in the 10.2 (?) days, this wasn't an issue -- for whatever reason (my guess being differences in the terminfo database), this alternate screen business was ignored, and one could see the work left behind by exiting programs. With Panther, and its incorporation of significant Linux influences, this behavior changed, much to my disappointment. Suddenly, one-too-many presses of the space bar and man pages were disappearing, pids that top revealed as needing to be killed were no longer visible after hitting "q" -- a whole litany of annoyances cropped up.

A solution:
I have seen various suggestions in various places regarding remedies, but none of them have been optimal. Some fixed the problem, but caused others equally irritating. Finally over Christmas, I found the time to play around a bit, and created an alternate terminfo entry for xterm-color.

What it does:
My slightly altered xterm-color has the following beneficial (at least to me) effects:

  1. Disables the "alternate screen buffer" so the work done by a program is left on the screen after it exits, as described above.
  2. Fixes an annoying problem with some other terminfo entries in which the last line of top's output is overwritten by the shell prompt upon exit.
  3. Allows the use of colorschemes and syntax coloring in vim without drawing insane underlines all over the place.
How it works:
By replacing the xterm-color entry in the terminfo database, we can alter the capabilities which tell programs the character sequences to send to the Terminal to begin and end the use of the alternate screen buffer. If we then tell Terminal to use our new terminfo entry, its xterm-color emulation will take on the behavior specified by the altered entry. In the replaced entry, only the smcup and rmcup capabilities have been changed; it is otherwise exactly the same as Apple's xterm-color which shipped with Panther.

How to install it:
  1. Download the compiled terminfo description file. The source is also included, so if you're paranoid, you can compile it yourself. Doing so isn't that hard, but requires a little work which is beyond the scope of this hint.

  2. In a Terminal window, cd to /usr/share/terminfo, and do a find . -name xterm-color. If your system is the same as mine, you should see this:
    ./78/xterm-color
    If not, replace 78 in the following with whatever directory your xterm-color entry was in.

  3. Make a backup copy prior to replacing:
    $ cd 78
    $ sudo cp xterm-color xterm-color.apple
    Obviously, use whatever name you want for the backup.

  4. Move or copy your downloaded xterm-color (in the archive's compiled directory) into place. You'll probably want to use sudo again, and set the ownership and permissions to correspond with those of the other files in the directory.
Now, if your Terminal preference for "Declare terminal type ($TERM) as" is set to "xterm-color," new windows opened subsequent to the replacement should exhibit the beneficial effects described above. You don't need to restart Terminal.

If you decide you don't like it, just replace the downloaded xterm-color with your backup copy.

Disclaimer:
As you might guess, there are several other ways in which similar effects can be achieved. The one I have decided to implement may be less than optimal depending on your needs or philosophy, but it fits mine. This hint is offered in the hopes that others may find it useful, and does not pretend to be the "best" or even the only solution.

[robg adds: This hint describes (in the hint and comments) a couple of other ways of working around the problem; between these two hints, there should be a solution for everyone. I've personally just set the terminal type to VT100, though that leaves me with the "last line overwrite" problem in top described above...]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2004122710403854