This is a bug in Terminal.app which has been noticed by others, e.g., in Any way to fix screen's mishandling of line wrap? (Maybe only Terminal.app)
I can reproduce the effect using bash+tmux, and capturing the text sent to the screen see that tmux is setting the scrolling margins to exclude the last line on the screen before updates that might cause a wrapped line. Meanwhile, bash is writing a long line by writing the part which would fit on the first line, plus two more characters, and then a carriage return and the second line, etc.
The two extra characters should force the line to wrap. But Terminal.app appears to be confused by the switch of scrolling margins along with moving the cursor to the last line of the scrolling region and then writing a long line there. Instead of wrapping, it stays where it is, and the second part of the long line is written on top of the first.
While there is a feature to turn off line-wrapping, most applications assume that it is enabled. In the trace (using script
), neither tmux nor bash used it. Unless one of those manipulates the line-wrapping, there's nothing to gain there. tmux, for instance, just assumes the feature and does not even check the terminal description for the am
(automargin) capability. It does check the related xenl
flag (newline glitch), but removing that from the terminal description would only make things worse.
So... if this bug stops you from using Terminal.app, iTerm2 is a suitable alternative.
Note that screen
is a terminal emulator. So your question is a bit like asking how can I start xterm
from gnome-terminal
and have what was last displayed in xterm
visible in my gnome-terminal
when xterm
exits.
Now the difference between xterm
and screen
is that while xterm
uses the X protocol to draw its screen, screen
uses a host terminal.
By default, screen
will clear the screen of its host terminal to display its own emulated terminal and where available will use the alternate screen (before clearing) of that host terminal, so that upon termination, it can restore the host terminal state as it was before starting.
So what you see is not screen
clearing the screen upon leaving, but restoring the host terminal's main screen. The content of the screen
window that was last displayed is still there on the alternate screen. With xterm
, you can have a look at it by selecting Show alternate screen in the Ctrl+MiddleClick menu.
If the host terminal doesn't support an alternate screen (like vt102 ones), it obviously can't do that. Instead, it does nothing, which is basically what you want.
So, what you can do is tell screen
that the host terminal doesn't support an alternate screen. For that, you can add to ~/.screenrc
:
termcapinfo * ti=:te=
Which says: for all possible host terminals (*
, matched against $TERM
), override the termcap/terminfo
database to say that for those terminals, the escape sequences to enter or leave the alternate screen are the empty string.
ti
and te
do not exactly mean alternate screen. From https://www.gnu.org/software/termutils/manual/termcap-1.3/html_node/termcap_39.html:
ti
(smcup
in terminfo
)
String of commands to put the terminal into whatever special modes are needed or appropriate for programs that move the cursor nonsequentially around the screen. Programs that use termcap to do full-screen display should output this string when they start up.
te
(rmcup
in terminfo
)
String of commands to undo what is done by the ti
string. Programs that output the `ti' string on entry should output this string when they exit.
But that translates to the alternate screen for most terminal emulators (specically, in xterm
it's alternate screen and saving/restoring of the cursor position)
Best Answer
Some terminals, such as xterm, support what is known as an “alternate screen”: there are separate screens for full-screen programs and for scrolling programs. In xterm, you can switch between the two screens with the “show alternate screen” command at the bottom of the Ctrl+mouse 2 menu.
This behavior is disabled by default in screen but can be enabled with the
altscreen
option: addaltscreen on
to your~/.screenrc
.