Linux: Screen

A small tool that helps you to:

1. keep a program running while you disconnect the ssh session (irssi, big copy job, compile stuff, …)
2. deal with multiple programs in one terminal

tmux is more modern and has for example a easier to read config file. But on CentOS it is for example not available by default, so I still prefer screen. For the same reason I’m using vi, but that’s another topic.

Here is my cheat sheet of the commands:

create a screen session
open a new session screen
open a new session and give it a name screen -S session_name
detach
detached a session Ctrl+a d
list screen sessions screen -ls
rename a session (inside screen) Ctrl+a :sessionname session_name
rename a session (outside screen) screen -S old_session_name -X sessionname new_session_name
attach
attach a session screen -r
list current sessions screen -ls
attach a specific session screen -r your_screen_name
adjust sessions
rescale application inside the session screen -r -A
attach a session that is attached somewhere else
(detach and attach)
screen -r -D
attach to a not detached screen
(“screen sharing”)
screen -x
view connections to the current session Ctrl+a *
windows
create a new one window Ctrl+a c
cycle through the windows Ctrl+a n
Ctrl+a space
cycle backwards Ctrl+a p
show window list Ctrl+a w
switch to a specific windows
first window is 0, second one = 1, …
Ctrl+a number
cycle between the current window and the last used window Ctrl+a a
rename current window Ctrl+a A
selection a window from a list Ctrl+a “
force application to fit into the current screen size Ctrl+a F
movement/scrolling
activate copy mode Ctrl+a Esc
Ctrl+a [
move cursor – PgUp/PgDn or Ctrl+b/Ctrl+f to scroll one page
– Ctrl+u/Ctrl+d: scroll half of a page
– arrow keys, j/k or +/-L scroll one line
– G: moves to the bottom
– ESC: to exit copy mode
search
search forward in copy mode /string_to_find_xyz
search backwards ?string_to_find_xyz
copy/paste
copy – navigate to beginning
– press Enter
– navigate to the end
– press Enter
insert the text Ctrl+a ]
split screen
horizontal split – create a new pane
– switch to the new pane
– create a window inside the new pane
that means:
Ctrl+a S
Ctrl+a Tab
Ctrl+a c
switch between the panes Ctrl+a Tab
vertical split Ctrl+a |
requires version 4.01 or newer
patches for OSX
close a pane Ctrl+A X
ending windows/sessions
kill a window Ctrl+a K
kill all windows and terminate the screen session Ctrl+a \
lock your session Ctrl+x
daemon mode
daemon for poor people… screen -dmS a_session_name /opt/script.sh
start daemon as a different user su -s /bin/bash – username -c “screen -dmS a_session_name /home/username/script.sh”
logs
create a log >screen -S ping_log -d -m -L ping google.de
>sleep 60
>screen -r ping_log -X quit
The logfile is written to the current directory and is called screenlog.?

config file

Here is my ~/.screenrc

# in case you don't like Ctrl+a for the shortcuts you can change it
# for example you can use the ` key. It makes Ctrl+a available to jump to the first character of the command line
#escape ``

bufferfile  $HOME/.screen_buffer
hardcopydir $HOME/.screen

startup_message  off
vbell            off
defutf8          on
defscrollback    10000
msgwait          2
term             screen-256color

#enable scrolling mechanism
termcapinfo xterm* ti@:te@

#F1-F9= select window 1-9
bindkey -k k1 select 1
bindkey -k k2 select 2
bindkey -k k3 select 3
bindkey -k k4 select 4
bindkey -k k5 select 5
bindkey -k k6 select 6
bindkey -k k7 select 7
bindkey -k k8 select 8
bindkey -k k9 select 9

hardstatus on
hardstatus alwayslastline
hardstatus string '%{= K7} %{= K7}%-w%{= 70}%n %t%{= K7}%+w %-= | %{= K7}%Y-%m-%d %D %c'
#http://michael-prokop.at/computer/config/.screenrc
#  %{= K7}                 default color = bright blac[K] background; white foreground
#  %{= K7}%-w              -w means before active window -> bright blac[K] background; white foreground
#  %{= 70}%n %t            number and text of the windows will be displayed; backgroud color 7 is white, foreground color 0 means black
#  %{= Kd}%+w              +w means after active window -> bright blac[K] background; white font
#  %-=                     move the rest of the text to the right
#  |                       print a | character
#  %{= Kd}%Y-%m-%d %D %c   change the color again to bright blac[K] background; white foreground; display Year-Month-Day weekday time(24h format)

#default window name
shelltitle shell

# start with window 1 instead of 0
bind c screen 1
bind ^c screen 1
bind 0 select 10
#create three windows by default and select the first one
screen -t shell 1
screen -t shell 2
screen -t shell 3
select 1
Tagged ,