GNU Screen | terminal multiplexer attachment
mouse 2721 · person cloud · link
Last update
2024-02-23
2024
02-23
«terminal multiplexer»

Screen commands shortcuts:

Shortcut Descr
^a c create new terminal (use in new regions too)
^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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# ~/.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 1000

# 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'
#termcapinfo xterm* ti@:te@
#defbce on

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

shelltitle --
#shell /usr/bin/fish

# select window 1..10 with sequence "^a 1..0"
bind c screen 1 # window numbering starts at 1 not 0
bind 0 select 10

# turn of flow control (^S-^Q / XON-XOFF characters)
defflow off

# default windows
screen 1 bash
screen 2 bash
select 1

# https://www.gnu.org/software/screen/manual/html_node/Key-Binding.html#Key-Binding
maptimeout 1000
# NOTE: run `showkey -a` to see keycodes to use here
# select window 11..20 with sequence "^A .N", where N=1..0
bind .  # unbind "^A ." (write termcap)
bindkey "^A.1" select 11
bindkey "^A.2" select 12
bindkey "^A.3" select 13
bindkey "^A.4" select 14
bindkey "^A.5" select 15
bindkey "^A.6" select 16
bindkey "^A.7" select 17
bindkey "^A.8" select 18
bindkey "^A.9" select 19
bindkey "^A.0" select 20

# switch windows with F1 (prev) and F2 (next)
# x11
bindkey "^[OP" prev
bindkey "^[OQ" next
# old VT console
bindkey "^[[[A" prev
bindkey "^[[[B" next

## 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"

# navigating regions with Ctrl-arrows
#bindkey "^[[1;5D" focus left
#bindkey "^[[1;5C" focus right
#bindkey "^[[1;5A" focus up
#bindkey "^[[1;5B" focus down

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 screen manual, ArchLinux tips, A killer GNU Screen Config gist, MC mouse support, Dev.to, Shared session