Thursday, September 07, 2006

Stop gnome-terminal screen clear

Gnome-terminal (and Mac OSX clears your screen when you quit a pager or editor and I don't like it. You won't like it either if you need to refer to the thing you were just looking at after you exit back to shell. This happens with man, less, more, pico, vi and others.

Here's an example of gnome-terminal automatically clearing the screen when exiting a pager. This is what needs fixing:

Here's what it does after we fix it. No more automatic clearing:

I used to fix this problem easily in the MUD by setting an environment variable: setenv NO_CLEAR 1

But Ubuntu Dapper is no LPMUD. I searched Google for the fix. Other people had the same complaint. The best source of synthesized info I found was here.

That writer, Akkana, understood the problem and offers some solutions for everything except gnome-terminal:
"...there's no way to tell gnome-terminal to disable the alt screen behavior."
I eventually found my own solution to fix gnome-terminal that I'll share at the end of this post, but first I want to review Akkana's, since her site doesn't accept comments.

First, she correctly identifies that gnome-terminal is the source of my problem. I confirmed that by dropping out of X to a real console (CNTRL+ALT+F2), logging into my shell, and checking my TERM environmental variable:
echo $TERM
On a console, that returns "linux," and quitting any pager on a console leaves the paged info on my screen the way I want it. While in X (CNTRL+ALT+F7 to get back to X), using gnome-terminal, the $TERM variable is "xterm."

This means that I can't solve this problem under X in a way that will break my console. How limiting.

Akkana offered 3 ideas and I tried them all. The first was to create a file in my home directory called .Xdefaults (symlinked to .Xresources, just in case) that contains these lines:
XTerm*titeInhibit: true
xterm*titeInhibit: true
gnometerminal*titeInhibit: true
gnome-terminal*titeInhibit: true
and then launch new terminals both in the real xterm program and in gnome-terminal. Through trial and error, I determined that only the 2nd line above had any effect, and it only stopped screen clearing in the xterm program. That problem persisted in gnome-terminal.

If I would just use xterm, my work would be done. But I do not like xterm.

Akkana's next suggestion is to create a ~/terminfo/xtermnoalt.terminfo file and export a TERM for it into the bash environment. She provides the file, doctored xterm-color terminfo data, but with the ti/te and rmcup screen clearing bits removed. Without those, gnome-terminal is supposed to be tricked into never trying to use those features.

It sort of works, but causes an extra prompt warning that my terminal is broken:
~$ man man
Reformatting man(1), please wait...
WARNING: terminal is not fully functional
- (press RETURN)
I can hit RETURN and get the pager normally after that, and when I quit, my screen does not get cleared, but that broken warning is more irritating than the original problem.

The 'man' utility ultimately uses 'less' to do its paging, and 'less' is actually the program emitting that warning. Even though I always use 'more' instead of 'less,' and 'more' still works fine, I look at man files a lot, and I am not satisfied with these results.

Technically, I could go a bit further and just hack a fix for man. The man file on man (in the --pager option) says that man uses /usr/bin/pager for paging, and that's just a symlink to /etc/alternatives/pager, which is itself another symlink to /usr/bin/less. So I could change that last symlink to /bin/more and then 'man' and 'more' would work fine. But that is a crummy hack that still leaves pico (actually /bin/nano) totally broken:
~$ pico -w sdsd
Error opening terminal: xterm-noalt.
And I use pico a lot, so I'm still unsatisfied.

Akkana's last idea is to use a command option to 'less' to ignore terminal initializations, and yes, I could make an alias for that in ~/.bashrc so that I don't have to remember to type it (alias less='less -X') but pico would still be broken, and you know I need pico.

So I searched around the web trying to learn about termcap and terminfo. And that was hard. So instead, I downloaded all the other terminal emulators I could find, hoping to just dump gnome-terminal (apt-get install pterm aterm eterm multi-gnome-terminal konsole). I tried and hated them all.

In the end, I just got out my sledge hammer and did this:
mv /lib/terminfo/x/xterm /lib/terminfo/x/xterm.orig
ln -s /lib/terminfo/v/vt220 /lib/terminfo/x/xterm
That moves the original xterm definition out of the way, and symlinks the vt220 definition in its place. Vt220's do not do the "alternate screen" feature that makes your page disappear.

This fixes Gnome-terminal which is now being fed a vt220 definition that it thinks is xterm. Really, gnome-terminal should give users a way to turn off the annoying screen clearing feature. You and I are not the only ones that find it a nuisance.

[A reader later posted a much better solution in the Comments that uses infocmp and tic to "fix" the terminfo definition file used by the terminal program. URLs at the end of the Conclusion section.]

If you want to know how I came up with this elegant solution, I just did a 'man terminfo' (lots of websites said this was caused by "terminfo") and at the top of the page it said, "Synopsis: /etc/terminfo/*/*," so I looked in there (ls -l /etc/terminfo) and found a single README file which said that if this directory is empty, ncurses (the library in charge of cursors and terminal-like things) would look in /lib/terminfo/*/*. Looking there, I found all the term definitions and figured one of them would be without silly rmcup. Symlinking by trial and error, I found one that worked.


You can fix your broken gnome-terminal emulator by tampering with the terminfo definition files, and get gnome-terminal to swallow vt220 terminfo that is intentionally mislabelled xterm.

Nay-sayers may point out that vt220 is not the same as xterm and that this could cause other problems, but I haven't noticed any. If there are, they should be less annoying than xterm's rmcups screen clearing. I'm not worried. Vt220's don't have any "dangerous modes" such as those from a vt100 that can lock up a vt220's output to "line printer."

The worst result I've seen from this switch, after using it for 10 minutes, is that the X-mouse won't work in console programs like sysv-rc-conf anymore, because it's designed for an xterm, not a vt220. No big deal.

If you really hate my idea, there may be, after all, some X resources in gnome-terminal for which ti/te can be inhibited that would fix the original problem, but I don't know what else they might be called. I suppose one could read the gnome-terminal source, or email the developer, if one were (subjunctive, condition contrary to fact) very determined.

I do wish gnome-terminal had a few more user-configurable items in its menus. But at least it has a menu. Really, I'm sad that since I said goodbye to Windows, I don't have VanDyke's SecureCRT anymore.

My psychiatrist says that I should just run it under wine.

Benjamin's Reader Comments provided a real solution that fixes the whole problem correctly, rebuilding a custom terminfo definition file:

toward the end or reprinted on his own site.

This solution also works great on Apple OSX in In my case, on the Mac, I am using the "Homebrew" Terminal profile, which uses xterm-color, so that is the terminal definition that I customized:

infocmp > ~/xterm-color-noclear.src
pico xterm-color-noclear.src
mkdir .terminfo
tic xterm-color-noclear.src
export TERM=xterm-color-noclear
pico .bashrc


  1. Anonymous11:20 PM

    And it looks like the ASCI progress meter for scp remote file transfers come out blank.

  2. gnome-terminal is a stinking pile. It almost makes me want to use KDE.

  3. Anonymous5:55 PM

    and all that you had to do was:
    export LESS="-X $LESS" in .bashrc

    a year too late, but a comment indeed...;)

  4. reply to anoymous@5:55pm, that solution is described above, "Akkana's last idea is to use a command option to 'less' to ignore terminal initializations", and it doesn't fix pico, or in my case, vi

    I also changed my terminal type through export TERM=vt220
    however, 'home' and 'end' keys don't work properly anymore

    great rundown on all your trials tho

  5. Victory!

    Tweaking terminfo proved successful. The xterm faq pointed me in the right direction,

    It explains how to add screen clearing for those who don't have it. Ha, might seem silly to those who are reading this page, but as they say, after all it's a matter of preference.

    So here's how to "Stop gnome-terminal screen clear":
    #infocmp has replaced `untic`, a long time ago,
    infocmp -I xterm > ~/xterm-noclear.src
    vi ~/xterm-noclear.src
    # 1: change the name on the second line from 'xterm' to 'xterm-noclear', or whatever suits you
    # 2: remove the instructions 'smcup' and 'rmcup' (remove everything: "smcup=\E[?1049h, " and "rmcup=\E[?1049l, " in my case) save and exit
    tic ~/xterm-noclear.src
    # x/xterm-noclear should appear in your ~/.terminfo directory
    export TERM=xterm-noclear
    # yaa! now test with man and vi, if satisfied, add the export line to your .profile, .bash_profile or .bashrc to make permanent

    I guess I've more or less redone what Akkana did for modding xterm-color, but this way doesn't seem to generate warnings. Anyhow, now you know how it's done. Let me know if you face some problems, I've tried it in gnome-terminal and xterm, with man and vi.

    I've reposted this comment on my website, along with some pre-modded terminfo files for those who don't want to (or can't) do those steps:

  6. Anonymous11:09 PM

    Interesting. I'm a longtime Red Hat user, and they've always had a terminfo entry called "xterm1" exactly for this purpose. I've got Ubuntu on a laptop, and I quickly discovered that Ubuntu knows nothing of xterm1. Must be a RH-specific thing.

    Anyway, comparing xterm to xterm1 on a RH system shows that the changes mentioned here are exactly what they changed, too:

    ...share/terminfo/x>infocmp -d xterm xterm1
    comparing xterm to xterm1.
    comparing booleans.
    comparing numbers.
    comparing strings.
    rmcup: '\E[?1049l', NULL.
    smcup: '\E[?1049h', NULL.

  7. Anonymous9:56 AM

    Instead of moving terminfo files around, a simple :
    export TERM=vt220
    in bashrc (or similar) is enough and don't need root access.

    By the way, the issue described here is not only related with gnome-terminal (konsole and other terminals setting TERM=xterm and supporting terminfo will behave the same way).

  8. Anonymous2:34 AM

    I have not always the permission to play with tic (it writes to some directory owned by root) nor I have the time, If I'm only working for an hour on this machine.

    Heu, when I don't want clear of the screen, I don't output to the screen...

    man man | cat
    or man man | cat | more

    And nothing is clear cause you write to a pipe, and not to the terminfo screen...

    Sorry to be so late...

  9. tic normally writes into the system but it will write into your own ~/.terminfo directory if one exists. Create it with

    mkdir ~/.terminfo

    and tic will now generate local terminfo files.

  10. export LESS='-X'


  11. export PAGER=less
    export LESS=-X