Fixing the alternate screen problem
Many of you probably know the annoying behaviour of console-based programs like less or vi restoring the screen after exiting so that the text you just had there vanishes.
I never understood why you'd want that, and its commonly enabled by default on Linux machines.
The underlying feature is called "alternate screen" and basically its a feature of your terminal (be it [def|xterm]:http://invisible-island.net/xterm/xterm.html, [gnome-term]:http://en.wikipedia.org/wiki/GNOME_Terminal or even your [def|console]:http://en.wikipedia.org/wiki/System_console).
If you want to get rid of it, you have a few options:
!4 Tell each and every program, not to use that feature.
A prime example is the [def|less]:http://www.greenwoodsoftware.com/less/ option %"-X"%.
This is useful as a quick ad-hoc solution, but not really feasible for everyday use - You don't really want to (re-)configure every program.
!4 Tell your terminal not to enable this feature.
%XTerm% has an option located in the /Ctrl+Middle mousebutton/ -Menu and is called "Enable Alternate Screen Switching". Turn if off and all programs within will behave nicely.
If you want it for all your xterm windows, you can add something like this to your %~/.Xdefaults%:
If you're using gnome-terminal, this is not for you. Gnome-terminal simply provides no way to disable this feature. *Boo*
!4 Lie to your programs and say your terminal doesn't support it.
All (well-behaved) programs check if your terminal supports alternate screens before using them. This information is stored in the system [terminfo]:http://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html or [termcap]:http://www.gnu.org/software/termutils/manual/termcap-1.3/html_mono/termcap.html database. Whether your system is using termcap or terminfo is an historical thing -- Linux/SysV based systems tend to use terminfo, BSDish systems termcap.
This database has an entry for [every terminal type]:http://catb.org/esr/terminfo/ your system supports. Your terminal is stored in the %$TERM% environment variable. Go and check your %$TERM% value. It is most probably %xterm% or something similar.
The capabilities for alternate screen are called %ti% and %te% in termcap and %smcup% / %rmcup% in terminfo. (*Yay!* for [gratuitous differences]:http://books.google.com/books?id=zyfk1301FBoC&lpg=PA18&ots=bXAScah3i4&dq=bsd%20sysv%20differences&pg=PA18#v=onepage&q&f=true). What we will be doing, is to override these by placing a suitably modified file in your %$HOME%.
For terminfo it works like this:
[def|infocmp]:http://linux.die.net/man/1/infocmp -l $TERM > $TERM.src
[def|vi]:http://linux.die.net/man/1/vi $TERM.src # /remove the smcup= and rmcup= parts till the next ,/
This dumps the current entry to a file, and "compiles" it into a file in %~/.terminfo/ % after you modified it.
Termcap just uses plaintext files, so go look into your system termcap file (%/usr/share/misc/termcap% or similar), and find the entry for your current %$TERM%. On my system it looks something like this:
xterm|xterm-color|X11 terminal emulator:\
copy that into %~/.termcap%, and add "%:ti@:te@%" at the beginning of the second line to disable these two capabilities. As you can see from my example on FreeBSD this is done by default for %xterm% and %xterm-color%.
That should fix it for you.
!4 If you use [def|screen]:http://www.gnu.org/software/screen/
Lastly, if you are using screen, there is a simpler way to do it. Screen has a built-in feature to edit the termcap/terminfo for programs running inside. Simply add
termcapinfo xterm* ti@:te@
to your %~/.screenrc% (of course replace %xterm% with your %$TERM% in use before you start screen)
I hope this lengthy explanation did not bore you too much...
posted at: 14:51
| Category: /tidbits
| permanent link to this entry
| 7 comments