GNU Screen | terminal multiplexer attachment
mouse 761 · person cloud · link
Last update
2019-05-10
2019
05-10
«terminal multiplexer»

Screen commands shortcuts:

Shortcut Descr
^a c create new terminal
^a # switch to # terminal (0-9)
^a " terminals list
^a A set the current terminal name/title
^a d detach session
^a S or ^a | split region horizontally or vertically
^a X close current region
^a Q close all regions but the current one
^a tab switch inputo to next region
^a ^a toggle between current and previous region
^a k terminate the current window/tab
^a \ terminate entire session
^a :number x changer current terminal number to x

Scrolling + copy&paste:

Press ^a esc then use PgUp/Dn or Up/Dn arrows to scroll, when finished hit return two times.

If you move the cursor after hitting return once, you will be selecting text to copy, and hitting return the second time will copy it. Then you can paste it with ^a ].

Command line how-to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
screen       # start a new session (default screen is 0)
screen -d -m # start a new detached session (useful in scrips)
screen -r    # reattach to a running session

screen -d|-D [pid.tty.host] # detaches the elsewhere running screen session

# reattaching options (in case of a "stale attached state"):
screen -d -r   # reattach a session and if necessary detach it first
screen -d -R   # reattach a session and if necessary detach or create it
screen -d -RR  # reattach to the first session and if necessary detach or create it

# or reattach trying even harder:
screen -D -r   # reattach a session, if necessary detach and logout remotely first
screen -D -R   # same as "-D -r" but also notify the user
screen -D -RR  # attach here and now. whatever that means, just do it

Share a session with another user

Same account

1
2
3
4
5
6
7
# user1 starts the session then attach to it
screen -d -m -S shared # start a new detached session called `shared`
screen -x shared # attach in multi display mode

# user2 only attach to it
screen -ls # show current sessions
screen -x shared # attach in multi display mode

Different accounts

1
2
3
4
5
6
7
8
# user1 starts the session then attach to it
screen -d -m -S shared # start a new detached session called `shared`
screen -r shared # attach to the named session
# press "Ctrl-a" and type ":multiuser on"
# press "Ctrl-a" and type ":acladd user2"

# user2 only attach to it
screen -x user1/shared

Note: the multiuser mode needs screen as suid root but this is a security issue... if you want to enable it then type as root:

1
2
3
chmod u+s `which screen`
chmod 755 /var/run/screen
rm -fr /var/run/screen/*

but you really should make a third shared account which both users can log into.

Customize session (see attachment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# ~/.screenrc - screen user config file

# the following two lines give a two-line status, with the current window highlighted
hardstatus alwayslastline '%{= kG}@%{G}%H%? %1`%?%{g}|%= %{= kw}%-w%{+b yk} %n*%t%?(%u)%? %{-}%+w %=%{g}|%{y}%l%{g}|%{B}%Y-%m-%d %{W}%c%{g}'

# huge scrollback buffer
defscrollback 5000

# no welcome message
startup_message off

# 256 colors
term screen-256color
attrcolor b ".I"
#termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
#defbce on

# mouse tracking allows to switch region focus by clicking
#mousetrack on

shelltitle --
#shell /usr/bin/fish

# default windows
screen 1 bash
screen 2 bash
select 1
bind c screen 1 # window numbering starts at 1 not 0
bind 0 select 10

# get rid of silly xoff stuff
bind s split

# Ctrl+F2 puts Screen into resize mode. Resize regions using hjkl keys.
bindkey "^[O1;5Q" eval "command -c rsz" # enter resize mode
# use hjkl keys to resize regions
bind -c rsz h eval "resize -h -5" "command -c rsz"
bind -c rsz j eval "resize -h +5" "command -c rsz"
bind -c rsz k eval "resize -v -5" "command -c rsz"
bind -c rsz l eval "resize -v +5" "command -c rsz"

# switch windows with F1 (prev) and F2 (next)
bindkey "^[OP" prev
bindkey "^[OQ" next

Tips & tricks

  • Use this handy script to start your terminals the way you want:
1
2
3
4
5
6
7
8
#!/bin/bash
if [ -z "$1" ]; then
  echo "USAGE: bashto pwd [cmd arg1 arg2 ...]"
  exit
fi
cd "$1"                    # go to the specified folder
shift; [ -n "$1" ] && "$@" # execute an optional command
exec /bin/bash             # run shell

.screenrc example: screen -t www bashto /mnt/ramd elinks.

1
2
[ -n "$DISPLAY" ] && echo -n "$DISPLAY" > $HOME/.last_display
[ -z "$DISPLAY" ] && export DISPLAY=$(cat $HOME/.last_display)

Alternatives:


Source: GNU.org, ArchLinux tips, A killer GNU Screen Config gist, MC mouse support, Dev.to, Shared session