screen: how to turn on alternate screen?
gnu-screen, iterm, linux, macos, terminal
Solution
UPDATE : I had forgotten about a feature of `screen` that disables alternate screen support by default.
Add `altscreen on` to your `$HOME/.screenrc` to enable it. This is probably all you have to do; all the messing about with `$TERM`, `$TERMCAP`, and `$TERMINFO` is probably unnecessary. I'll leave the rest of this answer here anyway.
I've had this in my own `.screenrc` so long it slipped my mind.
To enable this feature for the current session, type your `screen` escape character (Cntrl-A by default) followed by `:altscreen on` Enter.
Without this, `screen` won't respond to the escape sequences, even if they're defined in terminfo or termcap.
`vim` (and `less`, and most other curses-based full-screen programs) use the `smcup` and `rmcup` strings defined by the terminfo entry for your terminal. (These are referred to as `ti` and `te`, respectively, in the older termcap system). `smcup` switches to the alternate screen, and `rmcup` switches back to the primary screen.
If your terminfo entry doesn't have these strings, programs won't be able to do that.
The screen command, by default, sets your `$TERM` to `"screen"`. Apparently the "screen" terminfo entry doesn't have `smcup` and `rmcup`.
If you feel like hacking terminfo entries, you can modify your existing information for "screen" to add `smcup` and `rmcup`. You'll need the `infocmp` command to convert your terminfo to a text, and `tic` to convert it back to the binary form used by terminfo.
A simpler solution is to set your `$TERM` environment variable to whatever it was before you invoked the `screen` command. Almost all terminal emulators these days are based on the old DEC VT100, so they should be sufficiently compatible.
On my system, for example, when I login I have `TERM=xterm`; it's likely something else on MacOS. Within a `screen` session, by default, I'd have:
$ echo $TERM
screen
I could change it to `xterm` like this:
$ export TERM=xterm
(Again, replace "xterm" by whatever `$TERM` is on your system.)
You can also add
term xterm
to your `$HOME/.screenrc`. In fact `screen` has a number of options for tweaking your termcap/terminfo settings; `man screen` and search for "terminfo" for the gory details.
Problem
When I work in terminal, I see the history of the last shell commands execution output. If I run vim, I see the file I open full screen. When I exit vim, I can see the history of the last shell commands again. However, when I use vim from screen. I see the footprint of the file I was just editing on my exit from vim instead of the history of the last shell commands. I'd like to see the shell's history though. How can I achieve this behaviour? I used: - terminal.app and iterm2.app - tried switching xterm to xterm-256color (in iterm2.app) - checking and unchecking "Save lines to scrollback when an app status bar is present" and "Save lines to scrollback in alternate screen mode" (in iterm2) - adding termcapinfo xterm ti@:te@ to .screenrc (although it should be a solution to the opposite problem, but why not to try) - sshing to linux machines and trying to use gnu-screen there Up to now I didn't get the desired behaviour. P.S. I googled a bit and saw comments of people who try to solve the opposite problem: Many of you probably know the annoying behaviour of console-based programs like less or vi restoring the screen after exiting so that the text you just had there vanishes. The main idea behind this behaviour is whether programs use alternate screen to save a picture of the shell history and show it when vim exits. Thus if one wants to solve the opposite problem, he should exorce the evil alternate screen