Dev scratchpad. Digital garden

GNU Screen helpers

Nov 9, 2019 | Reading Time: 3 min

References, basic commands and sample rc file for GNU screen.


Basic commands

Description Command
Start a new session with session name screen -S <session_name>
List running sessions / screens screen -ls
Attach to a running session with name screen -R <session_name>
Detach a running session screen -d <session_name>
Command mode Ctrl+a
Enable vertical scrolling mode in a running session Ctrl-a ESC
Create new window Ctrl-a c
Change to window by number Ctrl-a <number>
Enter screen command Ctrl-a :
Send command to the screen session screen -X -S <session_name> <command>
Send kill command to the screen session screen -X -S <session_name> kill

RC File

 1# location: ~/.screenrc
 2# A sample screenrc file with a hardstatus line at bottom
 3# 3 windows created, with custom commands stuffed into each at session creation 
 4# and some key bindings.
 6# the following lines give a status line, with the current window highlighted
 7hardstatus alwayslastline
 8hardstatus string '%{= kg}[%{G}%H%? %1`%?%{g}][%= %{= kB}%?%-Lw%?%{+b r}(%{G}%n*%f %t%?(%u)%?%{r})%{-b B}%?%+Lw%?%?%= %{g}%][%{B}%d/%m %{W}%C%A%{g}]'
 9#hardstatus string '%{= kg}[ %{G}%H %{g}][%= %{= kB}%?%-Lw%?%{+b r}(%{G}%n*%f %t%?(%u)%?%{r})%{-b B}%?%+Lw%?%?%= %{g}%]'
10#hardstatus string '%{= kG}[%{G}%H%? %1`%?%{g}][%= %{= kw}%-w%{+b yk} %n*%t%?(%u)%? %{-}%+w %=%{g}][%{B}%d/%m %{W}%C%A%{g}]'
12# huge scrollback buffer
13defscrollback 10000
15# no welcome message
16startup_message off
18# 256 colors
19# attrcolor b ".I"
20# termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
21defbce on
23# mouse tracking allows to switch region focus by clicking
24# mousetrack on
25# default windows
26screen -t HOME 0 bash
27stuff "cd /root"
28screen -t SRV 1 bash
29stuff "cd /root/srv"
30screen -t MYSQL 2 bash
31stuff "cd /root/mysql"
32select 0
33#bind c screen 1 # window numbering starts at 1 not 0
34#bind 0 select 10
36# get rid of silly xoff stuff
37#bind s split
39# navigating regions with Ctrl-arrows
40bindkey "^[[1;5D" focus left
41bindkey "^[[1;5C" focus right
42bindkey "^[[1;5A" focus up
43bindkey "^[[1;5B" focus down
45# switch windows with F3 (prev) and F4 (next)
46bindkey "^[OR" prev
47bindkey "^[OS" next
49# switch layouts with Ctrl+F3 (prev layout) and Ctrl+F4 (next)
50bindkey "^[O1;5R" layout prev
51bindkey "^[O1;5S" layout next
53# F2 puts Screen into resize mode. Resize regions using hjkl keys.
54bindkey "^[OQ" eval "command -c rsz" # enter resize mode
55                                                                                                                                                                             1,1           Top
56# switch layouts with Ctrl+F3 (prev layout) and Ctrl+F4 (next)
57bindkey "^[O1;5R" layout prev
58bindkey "^[O1;5S" layout next
60# F2 puts Screen into resize mode. Resize regions using hjkl keys.
61bindkey "^[OQ" eval "command -c rsz" # enter resize mode
63# use hjkl keys to resize regions
64bind -c rsz h eval "resize -h -5" "command -c rsz"
65bind -c rsz j eval "resize -v -5" "command -c rsz"
66bind -c rsz k eval "resize -v +5" "command -c rsz"
67bind -c rsz l eval "resize -h +5" "command -c rsz"
69# quickly switch between regions using tab and arrows
70bind -c rsz \t    eval "focus"       "command -c rsz" # Tab
71bind -c rsz -k kl eval "focus left"  "command -c rsz" # Left
72bind -c rsz -k kr eval "focus right" "command -c rsz" # Right
73bind -c rsz -k ku eval "focus up"    "command -c rsz" # Up
74bind -c rsz -k kd eval "focus down"  "command -c rsz" # Down
75#source .screen_layout
76#layout save def