The alternate screen terminal emulator plague

November 29, 2011

There is one bit of behavior in modern X environments that drives me up the wall: the use of 'alternate screens' in terminal emulators (and in programs). That's probably obscure to most people, so let me put it the other way; what drives me up the wall is when I edit a file in vi or view it in a pager like less, quit, and all of the text that I was just looking at instantly disappears in favour of what was on the screen before I started whatever it was.

(Having this happen in less is especially infuriating, since less's entire purpose is showing me information. But the moment I quit, I can't have it.)

What is happening is not the fault of vi, less, and so on, or at least not exactly. Unix systems have terminfo, which is essentially a big database of terminals and escape sequences that programs can send to make them do things; two of the defined terminfo capabilities are escape sequences that you are supposed to send when your full screen program starts and stops. Many terminal emulators (and a certain number of real terminals) support an alternate screen, and the people who wrote terminfo database entries for them decided that full screen programs should always use this alternate screen so they put the escape sequences for 'switch to alternate screen' and 'switch to main screen' into the initialization and de-initialization sequences. When programs like vi, emacs, and less dutifully send the escape sequences that the terminfo database tells them to, they shoot your foot off.

My personal opinion is that this is an unfortunate example of the slow fossilization of X terminal emulators. xterm probably does this because some real terminal had its terminfo set up this way in the depths of time, and everyone else has probably slavishly copied how xterm works. No one has stopped to ask if the end result makes sense and is usable, because if they had a great many people would have told them that it doesn't and isn't.

(Xterm gets partial credit because it has a way to turn this off, so at least the xterm people recognize that it's a terrible idea even if they feel constrained by backwards compatibility to not fix the default.)

Unfortunately there is no general fix for this. Some programs can be told to not send the terminfo initialization and de-initialization strings; some terminal emulators can be told to ignore them. Sadly, sending the strings and paying attention to them is the default behavior; this leads to fixing a lot of programs on a lot of systems, one by one.

(For extra fun, some Unixes do things right to start with. For example, Solaris has never put the alternate screen escape sequences into their terminfo entries for xterm.)

Sidebar: the quick fixes

If you use xterm, set the XTerm*titeInhibit resource to true to make it ignore the alternate screen escape sequences. As usual, gnome-terminal has no way of controlling this.

For less, do 'export LESS=X' or otherwise give it the -X switch.

For vi, add 'set t_ti= t_te=' to your .vimrc.

(Some information taken from here. See also.)

Written on 29 November 2011.
« The login name problem
My mouse button bindings in fvwm, my window manager »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Tue Nov 29 01:10:01 2011
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.