OS X Terminal lines don’t go into scrollback

shellterminalxterm

I have a problem that crops up when using Mac OS X's Terminal (TERM=xterm): sometimes it gets itself into a state where lines that scroll off the top are not added to the scrollback buffer. I'm not using screen or similar; this is a plain bash shell inside a Terminal tab.

It doesn't do this immediately after opening a new tab. I believe it is a side-effect of something I've run in the problem tab. My guess is that it has something to do with the xterm emulation, possibly the scroll region.

What hasn't worked:

  • Soft and hard reset via the Shell menu
  • Running reset in the tab

Is there a reliable way to reset Terminal and/or the xterm state?

Can anyone provide a explanation for what's happening, even if the only fix is to close the tab and open a new one?

Best Answer

This means that the terminal is using the alternate screen buffer. There is a main screen, which scrolls into the scrollback log, and an alternate screen that does not. "Full screen" or "screen-oriented" programs like top, less, emacs, vim and screen switch the terminal to the alternate screen by default.

Each screen has its own contents and state. Having an alternate screen enables programs to take over the entire display, then restore the previous contents when they exit, by switching back to the main screen.

As of Mac OS X Lion 10.7, Terminal has a menu item you can use to manually switch screens:

View > Show/Hide Alternate Screen

This is provided primarily so that you can view or copy text from the alternate screen after a "full screen" program exits. Xterm has a similar command.

You can also use it to manually force a program to use a particular screen, to control whether the output goes to the scrollback log, for example. However, if you're currently using a program that explicitly switches to the alternate screen, you may confuse it or get unexpected results if you force it to the main screen while the program is running, so use this with caution.

Depending on the program, it may have a command-line argument or some other means to configure it to use the main screen instead. e.g., less -X.

Some terminfo entries are specifically designed to suppress using the alternate screen, e.g., xterm1. TERM=xterm1 emacs will run Emacs on the main screen. I don't recommend using xterm1 permanently, because it disables a number of other useful features compared to the default xterm-256color on Lion.

You can also switch screens using the tput command to issue the escape sequences from inside a shell or script. tput smcup switches to the alternate screen, and tput rmcup switches back to the main screen.

To see which program is currently running, look at the program name displayed in the Terminal window or tab title, or look at the Inspector window (Shell > Show Inspector). The last process in the list in the Inspector is (usually) the current program using the screen. That's the one you'll need to configure to use the main screen instead.