14

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?

Steve Madsen
  • 476
  • 4
  • 13
  • What Mac OS X version are you running? Do you have the buffer on something less than "unlimited" and the lines you're missing are simply the oldest lines that fell out the back end of the buffer? – Spiff Mar 15 '10 at 17:18
  • Mac OS X 10.6.2, and the buffer is 10,000 lines, however the lines lost are those scrolling just off the screen. If I page up one screen, those lines aren't there, and in fact as lines scroll off the top, the most recent page of scrollback never changes. – Steve Madsen Mar 15 '10 at 19:38

1 Answers1

23

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.

Chris Page
  • 575
  • 4
  • 8
  • 1
    This s sounds like a very promising answer. Is a reasonable explanation that a program could quit abnormally, leaving the alternate screen active? – Steve Madsen Aug 29 '11 at 01:56
  • 1
    If you're back in the shell after running a "full screen" program, yes, that's a possibility, although most popular programs, like the ones I listed, are reliable about cleaning up after themselves. If a "full screen" program crashes it might leave the terminal on the alternate screen. However, you said you tried **Shell > Send Hard Reset**, which includes switching back to the main screen. If it happens again, you could try using this command to switch back: `tput rmcup` – Chris Page Aug 29 '11 at 02:21
  • Another possibility is that a program left the terminal with a "scroll region". There are codes to tell the terminal to only scroll a sub-range of lines when scrolling. Programs use this to display status lines, for example, that don't scroll off the screen. If the scroll region doesn't include the top line, the text won't scroll into the scrollback log. Again, however, you said you tried a Hard Reset, which should clear that, too. – Chris Page Aug 29 '11 at 02:28
  • 2
    In my case, what seems to have happened is that my `ssh` session to a remote machine failed (timed out?) while I was in `vi`, apparently leaving the terminal in the Alternate Screen (because `vi` was never given the chance to clean up). Thereafter, any new `ssh` session in the same terminal would exhibit the problem described by the OP, until I resolved the problem by selecting the **View > Hide Alternate Screen** menu item. – Hephaestus Sep 24 '13 at 16:17
  • Thank you, *View > Show/Hide Alternate Screen* helped. It has a key combination which I must have unknowingly hit and didn't know what happened or how to undo – mehov Jul 04 '19 at 08:30