Difference between revisions of "GNU Screen"

From ArchWiki
Jump to: navigation, search
(Common Commands)
(change category to Terminal multiplexers, rm related articles)
 
(111 intermediate revisions by 28 users not shown)
Line 1: Line 1:
[[Category:System administration]]
+
[[Category:Terminal multiplexers]]
 +
[[ja:GNU Screen]]
 
[[ru:GNU Screen]]
 
[[ru:GNU Screen]]
[[zh-CN:GNU Screen]]
+
[[zh-hans:GNU Screen]]
[http://www.gnu.org/s/screen/ GNU Screen] is a wrapper that allows separation between the text program and the shell from which it was launched. This allows the user to, for example, start a text program in a terminal in X, kill X, and continue to interact with the program. Here are a couple of tips and tricks you may be interested in.
+
[https://www.gnu.org/software/screen/ GNU Screen] is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Programs running in Screen continue to run when their window is currently not visible and even when the whole screen session is detached from the user's terminal.
  
If you are looking for a tutorial, the gentoo wiki contains a nice one: http://en.gentoo-wiki.com/wiki/Screen
+
See the official overview [https://www.gnu.org/software/screen/manual/screen.html#Overview GNU Screen manual] for the description of the features.
  
 
== Installation ==
 
== Installation ==
  
GNU Screen can be [[pacman|installed]] using the {{pkg|screen}} package found in the [[Official Repositories|official repositories]].
+
[[Install]] the {{pkg|screen}} package.
  
== Basics ==  
+
== Usage ==
Commands are entered pressing Control A and then the key binding. The escape key can be changed with the ''escape'' option in {{ic|~/.screenrc}}. For example:
 
escape ``
 
  
sets the escape key to {{Keypress|`}}.
+
Commands are entered pressing the "escape key" {{ic|Ctrl+a}} and then the key binding.
 +
 
 +
Some users find the default escape key {{ic|Ctrl+a}} inconvenient. The escape key can be changed to another key as described in [[#Change the escape key]].
  
 
=== Common Commands ===
 
=== Common Commands ===
C-a ?
 
:Displays commands and its defaults (VERY important)
 
C-a "
 
:Window list
 
C-a 0
 
:opens window 0
 
C-a A
 
:Rename the current window
 
C-a c
 
:Create a new window (with shell)
 
C-a S
 
:Split current region into two regions
 
C-a <TAB>
 
:Switch the input focus to the next region
 
C-a C-a
 
:Toggle between current and previous region
 
C-a <ESC>
 
:Enter Copy Mode (use enter to select a range of text)
 
C-a ]
 
:Paste text
 
C-a Q
 
:Close all regions but the current one
 
C-a X
 
:Close the current region
 
C-a d
 
:Detach from the current screen session, and leave it running. Use {{ic|screen -r}} to resume
 
  
== Start at window 1 ==
+
* {{ic|Ctrl+a}} {{ic|?}} Displays commands and their defaults
By default, the first screen window is 0. If you'd rather never have a window 0 and start instead with 1, put something like the following in your {{ic|~/.screenrc}}:
+
* {{ic|Ctrl+a}} {{ic|:}} Enter to the command prompt of screen
bind c screen 1
+
* {{ic|Ctrl+a}} {{ic|"}} Window list
  bind ^c screen 1
+
* {{ic|Ctrl+a}} {{ic|0}} opens window 0
bind 0 select 10                                                           
+
* {{ic|Ctrl+a}} {{ic|A}} Rename the current window
screen 1
+
* {{ic|Ctrl+a}} {{ic|a}} Sends {{ic|Ctrl+a}} to the current window
 +
* {{ic|Ctrl+a}} {{ic|c}} Create a new window (with shell)
 +
* {{ic|Ctrl+a}} {{ic|S}} Split current region horizontally into two regions
 +
* {{ic|Ctrl+a}} {{ic|<nowiki>|</nowiki>}} Split current region vertically into two regions
 +
* {{ic|Ctrl+a}} {{ic|tab}} Switch the input focus to the next region
 +
* {{ic|Ctrl+a}} {{ic|Ctrl+a}} Toggle between current and previous region
 +
* {{ic|Ctrl+a}} {{ic|Esc}} Enter Copy Mode (use enter to select a range of text)
 +
* {{ic|Ctrl+a}} {{ic|]}} Paste text
 +
* {{ic|Ctrl+a}} {{ic|Q}} Close all regions but the current one
 +
* {{ic|Ctrl+a}} {{ic|X}} Close the current region
 +
* {{ic|Ctrl+a}} {{ic|d}} Detach from the current screen session, and leave it running. Use {{ic|screen -r}} to resume
 +
 
 +
=== Command Prompt Commands ===
 +
 
 +
* {{ic|Ctrl+a}} {{ic|:quit}} Closes all windows and closes screen session
 +
* {{ic|Ctrl+a}} {{ic|:source ~/.screenrc}} Reloads screenrc configuration file (can alternatively use /etc/screenrc)
 +
 
 +
=== Named sessions ===
 +
 
 +
To create a named session, run screen with the following command:
 +
 
 +
  $ screen -S ''session_name''
 +
 
 +
To (re)name an existing a session, run the following command while screen is running:
  
== Nested Screen Sessions ==
+
{{ic|Ctrl+a}} {{ic|:sessionname ''session_name''}} 
It is possible to get stuck in a nested screen session. A common scenario: you start an ssh session from within a screen session. Within the ssh session, you start screen. By default, the outer screen session that was launched first responds to C-a commands. To send a command to the inner screen session, use C-a a, followed by your command. For example:
 
  
C-a a d
+
To print a list of ''pid.tty.host'' strings identifying your screen sessions:
:Detaches the inner screen session.
 
C-a a K
 
:Kills the inner screen session.
 
  
== Fix for residual editor text ==
+
$ screen -list
When you open a text editor like nano in screen and then close it, the text may stay visible in your terminal. To fix this, put the following in your {{ic|~/.screenrc}}:
 
altscreen on
 
  
== Use 256 colors ==
+
To attach to a named screen session, run this command:
By default, screen uses an 8-color terminal emulator. Use the following line to enable more colors, which is useful if you are using a more-capable terminal emulator:
 
term screen-256color
 
  
If this fails to render 256 colors in [[xterm]], try the following instead:
+
  $ screen -x ''session_name''
  attrcolor b ".I"    # allow bold colors - necessary for some reason
 
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'   # tell screen how to set colors. AB = background, AF=foreground
 
defbce on    # use current bg color for erased chars
 
  
== Use 256 Colors with Rxvt-Unicode (urxvt) ==
+
or
If you are using {{pkg|rxvt-unicode}} from the [[Official Repositories|official repositories]], you may need to add this line in your {{ic|~/.screenrc}} to enable 256 colors while in screen.
+
 
terminfo rxvt-unicode 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
+
$ screen -r ''session_name''
 +
 
 +
===  Customizing Screen ===
 +
 
 +
You can modify the default settings for Screen according to your preference either through a personal {{ic|.screenrc}} file which contains commands to be executed at startup (e.g. {{ic|~/.screenrc}}) or on the fly using the colon ({{ic|-}}) command.
 +
 
 +
== Tips and tricks ==
 +
 
 +
=== Autostart with systemd ===
 +
 
 +
This service autostarts screen for the specified user (e.g. {{ic|systemctl enable screen@florian}}). Running this as a system unit is important, because [[systemd/User|systemd --user]] instance is not guaranteed to be running and will be killed when the last session for given the user is closed.
 +
 
 +
{{hc|/etc/systemd/system/screen@.service|<nowiki>
 +
[Unit]
 +
Description=screen
 +
After=network.target
 +
 
 +
[Service]
 +
Type=simple
 +
User=%i
 +
ExecStart=/usr/bin/screen -DmS autoscreen
 +
ExecStop=/usr/bin/screen -S autoscreen -X quit
 +
 
 +
[Install]
 +
WantedBy=multi-user.target
 +
</nowiki>}}
 +
 
 +
=== Change the escape key ===
 +
 
 +
It can be a good idea to change the default escape key, not only because "a" is usually typed with the left pinky, but also because {{ic|Ctrl+a}} is mapped to the common command {{ic|beginning-of-line}} in [[readline|GNU Readline]] and [[Bash]]-like shells.
 +
 
 +
The escape key can be changed with the {{ic|escape}} option in {{ic|~/.screenrc}}, or the {{ic|-e}} option to {{ic|screen}}.
 +
 
 +
For example, if you find that you rarely type {{ic|Ctrl+j}} in your shell or editor, you could use {{ic|escape ^Jj}} to set the escape key to {{ic|Ctrl+j}}. The second "j" means that a literal {{ic|Ctrl+j}} can be sent to the terminal via the sequence {{ic|Ctrl+j j}}. For [[Dvorak]] keyboard users, {{ic|Ctrl+t}} ({{ic|escape ^Tt}}) might be more convenient.
 +
 
 +
More exotic options include {{ic|escape ``}} which sets the escape key to {{ic|`}}, or {{ic|escape ^^^}} which sets it to {{ic|Ctrl+^}}.
 +
 
 +
The escape key is also called the "command character" in Screen documentation.
 +
 
 +
=== Start at window 1 ===
 +
 
 +
By default, the first screen window is 0. If you would rather never have a window 0 and start instead with 1, add the following lines on your configuration:
 +
 
 +
{{hc|~/.screenrc|
 +
bind c screen 1
 +
bind ^c screen 1
 +
bind 0 select 10                                                           
 +
screen 1
 +
}}
 +
 
 +
=== Nested Screen Sessions ===
 +
 
 +
It is possible to get stuck in a nested screen session. A common scenario: you start an SSH session from within a screen session. Within the SSH session, you start screen. By default, the outer screen session that was launched first responds to {{ic|Ctrl+a}} commands. To send a command to the inner screen session, use {{ic|Ctrl+a}} {{ic|a}}, followed by your command. For example:
 +
* {{ic|Ctrl+a}} {{ic|a}} {{ic|d}} Detaches the inner screen session.
 +
* {{ic|Ctrl+a}} {{ic|a}} {{ic|K}} Kills the inner screen session.
 +
 
 +
=== Start Screen on every shell ===
 +
For Bash and Zsh, add the following snippet to your {{ic|.bashrc}} or {{ic|.zshrc}} before your aliases:
 +
 
 +
{{hc|~/.bashrc or ~/.zshrc|<nowiki>
 +
if [[ -z "$STY" ]]; then
 +
  screen -xRR session_name
 +
fi
 +
</nowiki>}}
 +
 
 +
=== Use 256 colors ===
 +
 
 +
{{Out of date|screen 4.4 sets {{ic|$TERM}} to {{ic|screen.$TERM}}, although [https://www.gnu.org/software/screen/manual/screen.html#Term the manual] says something different. There should be no need to modify it in {{ic|~/.screenrc}}.}}
 +
 
 +
By default, Screen uses an 8-color terminal emulator. To enable more colors, you need to be using a terminal that supports them and set the correct [http://aperiodic.net/screen/commands:term term] value. This will use [[wikipedia:Terminfo|terminfo]] to describe how the [[wikipedia:ANSI escape code|ANSI escape codes]] will be interpreted. An entry in the terminfo database structure must exist, {{Pkg|ncurses}} provides many common descriptions stored under {{ic|/usr/share/terminfo/}}.
 +
 
 +
First try the generic value:
 +
 
 +
{{hc|~/.screenrc|
 +
term screen-256color
 +
}}
 +
 
 +
If that does not work, try setting it based on your terminal. When using [[xterm]]-based terminal:
 +
 
 +
{{hc|~/.screenrc|
 +
term xterm-256color
 +
}}
 +
 
 +
When using [[rxvt-unicode]]:
 +
 
 +
{{hc|~/.screenrc|
 +
term rxvt-unicode-256color
 +
}}
 +
 
 +
{{Note|{{ic|/usr/share/terminfo/r/rxvt-unicode-256color}} is provided by {{Pkg|rxvt-unicode-terminfo}}, which is installed as a dependency of {{Pkg|rxvt-unicode}}. However, if you log into a server via [[SSH]] and run ''screen'' there, this terminfo file might not be available on the server. In this case it is recommended to copy {{ic|/usr/share/terminfo/r/rxvt-unicode-256color}} on the server, it can be saved in {{ic|~/.terminfo}}.}}
 +
 
 +
As a last resort, try setting [http://aperiodic.net/screen/commands:termcapinfo termcapinfo] instead:
 +
 
 +
{{hc|~/.screenrc|<nowiki>
 +
attrcolor b ".I"    # allow bold colors - necessary for some reason
 +
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'   # tell screen how to set colors. AB = background, AF=foreground
 +
defbce on    # use current bg color for erased chars
 +
</nowiki>}}
 +
 
 +
=== Informative statusbar ===
  
== Informative statusbar ==
 
 
The default statusbar may be a little lacking. You may find this one more helpful:
 
The default statusbar may be a little lacking. You may find this one more helpful:
hardstatus off
 
hardstatus alwayslastline
 
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W} %c %{g}]'
 
  
== Turn welcome message off ==  
+
{{hc|~/.screenrc|<nowiki>
Add to {{ic|~/.screenrc}}:
+
hardstatus off
startup_message off
+
hardstatus alwayslastline
 +
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W} %c %{g}]'
 +
</nowiki>}}
 +
 
 +
Another possibility, taken from [http://www.fordfrog.com/2012/09/02/71/ frodfrog's blog] is:
 +
 
 +
{{hc|~/.screenrc|<nowiki>
 +
hardstatus alwayslastline '%{= G}[ %{G}%H %{g}][%= %{= w}%?%-Lw%?%{= R}%n*%f %t%?%{= R}(%u)%?%{= w}%+Lw%?%= %{= g}][ %{y}Load: %l %{g}][%{B}%Y-%m-%d %{W}%c:%s %{g}]'
 +
</nowiki>}}
 +
 
 +
statusbar at top:
 +
 
 +
{{hc|~/.screenrc|<nowiki>
 +
hardstatus firstline
 +
</nowiki>}}
 +
 
 +
=== Turn welcome message off ===
 +
 
 +
{{hc|~/.screenrc|
 +
startup_message off
 +
}}
 +
 
 +
=== Turn your hardstatus line into a dynamic urxvt|xterm|aterm window title ===
  
== Turn your hardstatus line into a dynamic urxvt|xterm|aterm window title ==
 
 
This one is pretty simple; just switch your current {{ic|hardstatus}} line into a {{ic|caption}} line with notification, and edit accordingly:
 
This one is pretty simple; just switch your current {{ic|hardstatus}} line into a {{ic|caption}} line with notification, and edit accordingly:
backtick 1 5 5 true
+
{{hc|~/.screenrc|<nowiki>
termcapinfo rxvt* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'
+
backtick 1 5 5 true
hardstatus string "screen (%n: %t)"
+
termcapinfo rxvt* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'
caption string "%{= kw}%Y-%m-%d;%c %{= kw}%-Lw%{= kG}%{+b}[%n %t]%{-b}%{= kw}%+Lw%1`"
+
hardstatus string "screen (%n: %t)"
caption always
+
caption string "%{= kw}%Y-%m-%d;%c %{= kw}%-Lw%{= kG}%{+b}[%n %t]%{-b}%{= kw}%+Lw%1`"
 +
caption always
 +
</nowiki>}}
 +
 
 +
This will give you something like {{ic|screen (0 bash)}} in the title of your terminal emulator. The caption supplies the date, current time, and colorizes your screen window collection.
 +
 
 +
===Use X scrolling mechanism===
 +
 
 +
The scroll buffer of GNU Screen can be accessed with {{ic|Ctrl+a}} {{ic|[}}. However, this is very inconvenient. To use the scroll bar of e.g. xterm or konsole, add the following line:
 +
{{hc|~/.screenrc|
 +
termcapinfo xterm* ti@:te@
 +
}}
 +
 
 +
=== Attach an existing running program to screen ===
  
This will give you something like "screen (0 bash)" in the title of your terminal emulator. The caption supplies the date, current time, and colorizes your screen window collection.
+
If you started a program outside Screen, but now you would like it to be inside, you can use '''reptyr''' to reparent the process from it's current tty to one inside screen.
  
==Use X scrolling mechanism==
+
[[Install]] the {{Pkg|reptyr}} package.
The scroll buffer of GNU Screen can be accessed with C-a [. However, this is very inconvenient. To use the scroll bar of e.g. xterm or konsole, add the following line to {{ic|~/.screenrc}}:
 
termcapinfo xterm* ti@:te@
 
  
==Add a GRUB entry to boot into Screen==
+
Get the PID of the process (you can use {{ic|ps ax}} for that). Now just enter the PID as argument to reptyr inside a screen window.
If you mostly use [[Xorg|X]] but occasionally want to run a Screen-as-window-manager session, here is one way to do it by adding a [[GRUB]] entry for Screen on a virtual console (text terminal).  
+
{{bc|$ reptyr ''<pid>''}}
  
GRUB allows you to designate what runlevel you want so we will use runlevel 4 for this purpose. Clone an appropriate GRUB entry and add a {{ic|4}} to the kernel boot parameters list, like so:
+
===Setting a different bash prompt while in screen===
  
  # (0) Arch Linux
+
If you want a different bash prompt when in a screen session, add the following to your .bashrc:
title  Arch Linux Screen
+
{{bc|<nowiki>
root  (hd0,2)
+
if [ -z $STY ]
kernel /vmlinuz-linux root=/dev/disk/your_disk ro acpi_no_auto_ssdt irqpoll 4
+
then
initrd /initramfs-linux.img
+
        PS1="YOUR REGULAR PROMPT"
 +
else  
 +
        PS1="YOUR SCREEN PROMPT"
 +
fi
 +
</nowiki>}}
 +
[http://serverfault.com/questions/257975/how-to-check-if-im-in-screen-session]
  
Add some entries to {{ic|/etc/inittab}} to indicate what should happen on runlevel 4, substituting your user name for <user>:
+
===Turn off visual bell===
# GNU Screen on runlevel 4
 
scr2:4:respawn:/sbin/mingetty --autologin <user> tty1 linux
 
  
The above line uses mingetty to [[automatically login some user to a virtual console on startup]]. You will need to install the {{pkg|mingetty}} package from the [[Official Repositories|official repositories]]. The inittab line segments are separated by colons. The first part (scr*) is simply an id. The second part is the runlevel: This should only happen on runlevel 4 (which is not used in any default setup - 3 is by default for a tty login and 5 is for X). 'Respawn' causes init to repeat the command (i.e. autologin) if the user logs out.  
+
With this setting, Screen will not make an ugly screen flash instead of a bell sound.
We will need to see that nothing else happens on virtual console 1 when we use runlevel 4, so remove {{ic|4}} from the the first of the agetty lines:
+
{{hc|~/.screenrc|
 +
vbell off
 +
}}
  
c1:235:respawn:/sbin/agetty -8 38400 vc/1 linux
+
=== Getting rid of the vertical and horizontal bars ===
 +
To get rid of the vertical bars:  
 +
{{hc|
 +
head=$ ~/.screenrc |output=rendition so =00
 +
}}
  
Once we are logged in, we want to ensure that screen is started. Add the following to the end of your {{ic|~/.bash_profile}}:
+
Setting the back and foreground color to default (d) and displays nothing (" "), to hide horizontal bar.
vico="$(tty | grep -oE ....$)"
+
{{hc|~/.screenrc|
case "$vico" in
+
caption string "%{03} "
  tty1) TERM=screen; exec /usr/bin/screen -R arch;;
+
}}
esac
 
  
This checks for the current runlevel and will launch a screen session immediately after the automatic login if the runlevel is 4.
+
== Troubleshooting ==
  
This can also be adapted to run screen on a virtual console next to X, simply checking for the current tty instead of the current runlevel. This checks to see if we are on virtual console 3:
+
=== Fix for residual editor text ===
vico="$(tty | grep -oE ....$)"
 
case "$vico" in
 
  vc/3) TERM=screen; exec /usr/bin/screen;;
 
esac
 
  
Set inittab/mingetty to automatically log in to vc/3 on runlevel 5 and you are set.
+
When you open a text editor like nano in screen and then close it, the text may stay visible in your terminal. To fix this, put the following:
 +
{{hc|~/.screenrc|
 +
altscreen on
 +
}}
  
== Fix Midnight Commander hard hang when starting in screen ==
+
=== Fix for Name column in windowlist only show "bash" ===
In some cases (need deeper inspection) [https://bugzilla.redhat.com/show_bug.cgi?id=168076 old gpm bug] gets alive. So, then you try to run mc inside screen, you get a frozen screen window. Try to kill gpm daemon before starting mc and/or disable it in {{ic|/etc/[[rc.conf]]}}.
+
add following to ~/.screenrc
 +
{{hc|head=~/.screenrc|output=windowlist string "%4n %h%=%f"}}
  
== See Also ==
+
== See also ==
 +
* [https://www.gnu.org/software/screen/manual/screen.html GNU Screen User's Manual]
 +
* [http://wiki.gentoo.org/wiki/Screen Gentoo Wiki - Tutorial for screen]
 +
* [https://bbs.archlinux.org/viewtopic.php?id=55618 Arch Forums - .screenrc configs with screenshots]
 +
* [https://bbs.archlinux.org/viewtopic.php?id=50647 Arch Forums - Regarding 256 color issue with urxvt]
 
* [http://www.macosxhints.com/article.php?story=20021114055617124 MacOSX Hints - Automatically using screen in your shell]
 
* [http://www.macosxhints.com/article.php?story=20021114055617124 MacOSX Hints - Automatically using screen in your shell]
* [http://en.gentoo-wiki.com/wiki/Screen#Tab-bar Gentoo Wiki - Using tabs with screen]
+
* [[Ratpoison|Ratpoison - A window manager based on gnu screen]]
* [https://bbs.archlinux.org/viewtopic.php?id=50647 Arch Forums - Regarding 256 color issue with urxvt]
+
* [[Xpra|Xpra - A utility to detach/reattach X programs, in a way similar as screen does for command line based programs]]
* [https://bbs.archlinux.org/viewtopic.php?id=55618 Arch Forums - .screenrc configs with screenshots]
 
* [[tmux]], another multiplexer
 

Latest revision as of 04:17, 10 April 2018

GNU Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Programs running in Screen continue to run when their window is currently not visible and even when the whole screen session is detached from the user's terminal.

See the official overview GNU Screen manual for the description of the features.

Installation

Install the screen package.

Usage

Commands are entered pressing the "escape key" Ctrl+a and then the key binding.

Some users find the default escape key Ctrl+a inconvenient. The escape key can be changed to another key as described in #Change the escape key.

Common Commands

  • Ctrl+a ? Displays commands and their defaults
  • Ctrl+a : Enter to the command prompt of screen
  • Ctrl+a " Window list
  • Ctrl+a 0 opens window 0
  • Ctrl+a A Rename the current window
  • Ctrl+a a Sends Ctrl+a to the current window
  • Ctrl+a c Create a new window (with shell)
  • Ctrl+a S Split current region horizontally into two regions
  • Ctrl+a | Split current region vertically into two regions
  • Ctrl+a tab Switch the input focus to the next region
  • Ctrl+a Ctrl+a Toggle between current and previous region
  • Ctrl+a Esc Enter Copy Mode (use enter to select a range of text)
  • Ctrl+a ] Paste text
  • Ctrl+a Q Close all regions but the current one
  • Ctrl+a X Close the current region
  • Ctrl+a d Detach from the current screen session, and leave it running. Use screen -r to resume

Command Prompt Commands

  • Ctrl+a :quit Closes all windows and closes screen session
  • Ctrl+a :source ~/.screenrc Reloads screenrc configuration file (can alternatively use /etc/screenrc)

Named sessions

To create a named session, run screen with the following command:

$ screen -S session_name

To (re)name an existing a session, run the following command while screen is running:

Ctrl+a :sessionname session_name

To print a list of pid.tty.host strings identifying your screen sessions:

$ screen -list

To attach to a named screen session, run this command:

$ screen -x session_name

or

$ screen -r session_name

Customizing Screen

You can modify the default settings for Screen according to your preference either through a personal .screenrc file which contains commands to be executed at startup (e.g. ~/.screenrc) or on the fly using the colon (-) command.

Tips and tricks

Autostart with systemd

This service autostarts screen for the specified user (e.g. systemctl enable screen@florian). Running this as a system unit is important, because systemd --user instance is not guaranteed to be running and will be killed when the last session for given the user is closed.

/etc/systemd/system/screen@.service
[Unit]
Description=screen
After=network.target

[Service]
Type=simple
User=%i
ExecStart=/usr/bin/screen -DmS autoscreen
ExecStop=/usr/bin/screen -S autoscreen -X quit

[Install]
WantedBy=multi-user.target

Change the escape key

It can be a good idea to change the default escape key, not only because "a" is usually typed with the left pinky, but also because Ctrl+a is mapped to the common command beginning-of-line in GNU Readline and Bash-like shells.

The escape key can be changed with the escape option in ~/.screenrc, or the -e option to screen.

For example, if you find that you rarely type Ctrl+j in your shell or editor, you could use escape ^Jj to set the escape key to Ctrl+j. The second "j" means that a literal Ctrl+j can be sent to the terminal via the sequence Ctrl+j j. For Dvorak keyboard users, Ctrl+t (escape ^Tt) might be more convenient.

More exotic options include escape `` which sets the escape key to `, or escape ^^^ which sets it to Ctrl+^.

The escape key is also called the "command character" in Screen documentation.

Start at window 1

By default, the first screen window is 0. If you would rather never have a window 0 and start instead with 1, add the following lines on your configuration:

~/.screenrc
bind c screen 1
bind ^c screen 1
bind 0 select 10                                                            
screen 1

Nested Screen Sessions

It is possible to get stuck in a nested screen session. A common scenario: you start an SSH session from within a screen session. Within the SSH session, you start screen. By default, the outer screen session that was launched first responds to Ctrl+a commands. To send a command to the inner screen session, use Ctrl+a a, followed by your command. For example:

  • Ctrl+a a d Detaches the inner screen session.
  • Ctrl+a a K Kills the inner screen session.

Start Screen on every shell

For Bash and Zsh, add the following snippet to your .bashrc or .zshrc before your aliases:

~/.bashrc or ~/.zshrc
if [[ -z "$STY" ]]; then
   screen -xRR session_name
fi

Use 256 colors

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: screen 4.4 sets $TERM to screen.$TERM, although the manual says something different. There should be no need to modify it in ~/.screenrc. (Discuss in Talk:GNU Screen#)

By default, Screen uses an 8-color terminal emulator. To enable more colors, you need to be using a terminal that supports them and set the correct term value. This will use terminfo to describe how the ANSI escape codes will be interpreted. An entry in the terminfo database structure must exist, ncurses provides many common descriptions stored under /usr/share/terminfo/.

First try the generic value:

~/.screenrc
term screen-256color

If that does not work, try setting it based on your terminal. When using xterm-based terminal:

~/.screenrc
term xterm-256color

When using rxvt-unicode:

~/.screenrc
term rxvt-unicode-256color
Note: /usr/share/terminfo/r/rxvt-unicode-256color is provided by rxvt-unicode-terminfo, which is installed as a dependency of rxvt-unicode. However, if you log into a server via SSH and run screen there, this terminfo file might not be available on the server. In this case it is recommended to copy /usr/share/terminfo/r/rxvt-unicode-256color on the server, it can be saved in ~/.terminfo.

As a last resort, try setting termcapinfo instead:

~/.screenrc
attrcolor b ".I"    # allow bold colors - necessary for some reason
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'   # tell screen how to set colors. AB = background, AF=foreground
defbce on    # use current bg color for erased chars

Informative statusbar

The default statusbar may be a 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%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W} %c %{g}]'

Another possibility, taken from frodfrog's blog is:

~/.screenrc
hardstatus alwayslastline '%{= G}[ %{G}%H %{g}][%= %{= w}%?%-Lw%?%{= R}%n*%f %t%?%{= R}(%u)%?%{= w}%+Lw%?%= %{= g}][ %{y}Load: %l %{g}][%{B}%Y-%m-%d %{W}%c:%s %{g}]'

statusbar at top:

~/.screenrc
hardstatus firstline 

Turn welcome message off

~/.screenrc
startup_message off

Turn your hardstatus line into a dynamic urxvt|xterm|aterm window title

This one is pretty simple; just switch your current hardstatus line into a caption line with notification, and edit accordingly:

~/.screenrc
backtick 1 5 5 true
termcapinfo rxvt* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'
hardstatus string "screen (%n: %t)"
caption string "%{= kw}%Y-%m-%d;%c %{= kw}%-Lw%{= kG}%{+b}[%n %t]%{-b}%{= kw}%+Lw%1`"
caption always

This will give you something like screen (0 bash) in the title of your terminal emulator. The caption supplies the date, current time, and colorizes your screen window collection.

Use X scrolling mechanism

The scroll buffer of GNU Screen can be accessed with Ctrl+a [. However, this is very inconvenient. To use the scroll bar of e.g. xterm or konsole, add the following line:

~/.screenrc
termcapinfo xterm* ti@:te@

Attach an existing running program to screen

If you started a program outside Screen, but now you would like it to be inside, you can use reptyr to reparent the process from it's current tty to one inside screen.

Install the reptyr package.

Get the PID of the process (you can use ps ax for that). Now just enter the PID as argument to reptyr inside a screen window.

$ reptyr <pid>

Setting a different bash prompt while in screen

If you want a different bash prompt when in a screen session, add the following to your .bashrc:

if [ -z $STY ]
then
        PS1="YOUR REGULAR PROMPT"
else  
        PS1="YOUR SCREEN PROMPT"
fi

[1]

Turn off visual bell

With this setting, Screen will not make an ugly screen flash instead of a bell sound.

~/.screenrc
vbell off

Getting rid of the vertical and horizontal bars

To get rid of the vertical bars:

$ ~/.screenrc
rendition so =00

Setting the back and foreground color to default (d) and displays nothing (" "), to hide horizontal bar.

~/.screenrc
caption string "%{03} "

Troubleshooting

Fix for residual editor text

When you open a text editor like nano in screen and then close it, the text may stay visible in your terminal. To fix this, put the following:

~/.screenrc
altscreen on

Fix for Name column in windowlist only show "bash"

add following to ~/.screenrc

~/.screenrc
windowlist string "%4n %h%=%f"

See also