Andrew Mercer
on this page

screen keeps shell sessions running on a server after you disconnect, and lets you run several shells in one terminal. (tmux is the modern alternative, with the same ideas.)

Basics

screen                         # start a session (press Space/Enter to dismiss the intro)
screen -S deploy               # start a named session

Inside screen, commands start with Ctrl+a:

Keys Action
Ctrl+a d detach (the session keeps running)
Ctrl+a c create a new window
Ctrl+a n / p next / previous window
Ctrl+a " list windows
Ctrl+a A rename the current window
Ctrl+a k kill the current window
exit end the current shell (the session ends when the last window closes)
screen -ls                     # list sessions
screen -r                      # reattach (if only one)
screen -r 23108.pts-8.host     # reattach a specific one
screen -r deploy               # ... or by name
screen -d -r deploy            # detach it from elsewhere first, then attach here

Typical use: start a long job over SSH, detach, log out, and come back later. See scrub and reptyr for examples.

Status bar

vi ~/.screenrc
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "

Nested sessions

Start screen locally, ssh to a server, and start screen there. To send a command to the inner screen, type the escape key twice or, if it does not pass through, press Ctrl+a then a then the command. For example, detach the inner session with Ctrl+a a d (older notes describe Ctrl+a, wait, a+d). You can change the inner escape with screen -e^Bb to use Ctrl+b instead.

screenie

screenie is a small menu wrapper around screen for people who want a launcher. Run screenie, press a to add a job, give it a session name and a command (for example bash), then choose sessions by number. Detach with Ctrl+a d to return to the menu. It works nested over SSH the same way (ssh host; screenie).

Related: tmate (share a session), terminator.