One screen session can contain several screen windows. A window runs another program instance.
To start a new screen session
$ screen [command]
If an explicit command is not given then a shell is started.
A session has a unique identifier by default following the following pattern:
pid.tty.host
. You can give a more useful name (to reattach later) using
the following command
$ screen -R name [command]
A window has a numeric identifier.
From a running session you can enter command mode holding the control key and pressing the 'a' character. Command mode allows you to enter speial characters to: create new windows, detach from screen session, etc.
`<C-a>` : enters command mode from a running `screen` session.
When in command mode, one command is one character. After the command is executed then the program goes back to normal mode.
Use "space bar" to toggle selection to copy.
-list
: List running screen
sessions.-r <session-id>
: reattach to a screen session.-R <session-id>
: reattach to a screen session or create it first.-RR <session-id>
: like -R but resume the yungest if there are multiple
possibilities (*).-d <session-id>
: detach a screen session-D <session-id>
: detach a screen session (logout if necessary)(*): a session id is unique, however a partial session-id (without the pid prefix) can be specified. In this case there can be a conflict.
For example to detach a running screen session from the holding terminal and reattach it in the current one (given session-id: 123.pts-1.localhost)
$ screen -d -r pts-1
Screen can be extremely useful when is run above an SSH session because allows to:
We use "weechat-curses" program as an example.
ssh <host> -t screen -D -RR mysessionid top
The command, after login, will
-t
) and connects it to the screen sessionscreen
session named "weechat". It a session with that name
exists, it will reattach to it (-D -RR
).Detach
To know if the current shell is running under screen or tmux check the STY and TMUX environment variables, respectively. If they are empty then you are not under a multiplexer.
Add the following to the .bashrc
file to have the information always in
the prompt.
if [ -n "$STY" ]; then
export PS1="(s) $PS1";
fi
if [ -n "$TMUX" ]; then
export PS1="(t) $PS1";
fi
The default statusbar may be little lacking. You may find this one more helpful:
~/.screenrc
hardstatus off
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%uu)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W} %c %{g}]'
Proudly self-hosted on a cheap Raspberry Pi