OpenSSH, FreeBSD screen overwrite when closing application

I installed a fresh FreeBSD 10 on a VM and connected via SSH and I've noticed that whenever i close a program (e.g., htop, top, nano etc) the contents of the session gets overwritten.

Terminal screenshot

This doesn't happen when I connect to a Ubuntu, Debian server for instance.

I'm not quite sure what this is called either, so Google is no help.

Has anyone experienced this before? / Is there some setting in OpenSSH Server that I need to change?


Solution 1:

The contents of the session were already overwritten, when you started up the TUI programs in the first place. You can see that that is the case.

When a TUI program, that uses ncurses and suchlike to present its textual user interface, starts up, it clears the screen. At that point, everything that is on the screen has been overwritten.

What you're missing, and what you don't know the name for, is the idea of terminals that have an alternate screen buffer. When such a TUI program as these starts up, it issues the escape sequence to switch to the terminal's alternate screen buffer, if it has one. All of its output then goes onto that buffer. When the program exits, or suspends itself, it issues the escape sequence to restore the primary screen buffer, which has remained untouched whilst the TUI program has displayed its user interface on the alternate buffer.

For terminals that don't have alternate screen buffers, there's no escape sequence to issue, and the full-screen user interface overwrites what was previously on the terminal.

Programs look these escape sequences up in the terminfo or termcap databases. In the terminfo world the terminal capabilities are named smcup and rmcup. In the termcap world they are named ti and te. As documented they make no mention of screen buffers.

Instead they talk of entering and exiting "cursor addressing mode". The notion is that a TUI program that presents a full-screen interface like this is operating in cursor addressing mode where the program really doesn't need the terminal to scroll; whereas a TUI program that just outputs scrolling lines of text is not. So one switches into and out of this mode. (In the real world, things are not so clear cut. For example: Modern shells such as the Z Shell move the cursor about for line editing, menu completion, and $RPROMPT; but don't switch to the alternate screen buffer, don't have a fully-fledged full screen user interface, and operate in terms of scrolling.)

The capabilities thus usually do more than just switch buffers. smcup also contains the escape sequence for saving the current cursor position, and rmcup the escape sequence for restoring it, as well, if the terminal has such escape sequences.

Your problem is one of two things:

  • The termcap database on your FreeBSD system doesn't have te and ti entries for your particular terminal type, because the entry is incomplete.
  • You aren't using the right terminal type for your terminal emulator in the first place.

The terminal type is taken from your TERM environment variable, on the server end. Its value denotes an entry in the termcap database. So ensure that your TERM environment variable names an entry in /etc/termcap with the capabilities that match those of your (local) terminal emulator. If there's no entry matching your terminal emulator that contains ti and te, then you'll have to simply add such an entry.

Solution 2:

As pointed out by roens, FreeBSD does not include te/ti definitions in its default termcap entry, xterm-256color. However, it does include these definitions in a termcap entry for those who want screen clearing: xterm-clear.

Thus, an easy way to accomplish this is to set your TERM variable:

export TERM=xterm-clear

Even though it's a very late answer, I'm putting this here because many of the other solutions I've seen (including roens's) are much more complicated than necessary (involving modifying the termcap file with the escape codes and recompiling the db, or using a .termcap file).

Solution 3:

I found example of just what to change in this posting on the FreeBSD forum. After making this change, what is on screen while viewing a manpage or editing a file gets cleared away, showing only the commands & such in my terminal (bash). What also now works is the variety of methods of scrolling (like "2-finger scrolling") while in an editor or other such process.

What has worked splendidly for me, running FreeBSD 11.0-RELEASE-p9, is to add the 4th line (defining te & ti) and preceding backslash to the term alias in /usr/share/misc/termcap:

xterm-256color|xterm alias 3:\
    :Co#256:pa#32767:\
    :AB=\E[48;5;%dm:AF=\E[38;5;%dm:tc=xterm-new:\
    :te=\E[2J\E[?47l\E8:ti=\E7\E[?47h::tc=xterm-xfree86:

Since I'm coming from macOS, and my TERM=xterm-256color, I've added the above to xterm-256color|xterm alias, at line 2884. Your TERM and placement in your termcap file may be different.

Then, after making changes to termcap, you'll have to rebuild the termcap database with:

cap_mkdb /usr/share/misc/termcap

(I'd have offered this as a comment to the existing answer, but lack >50 reputation.)