GNU Screen

From ArchWiki
Revision as of 00:15, 17 September 2011 by Jstjohn (talk | contribs) (Use 256 Colors with Rxvt-Unicode (urxvt): added Codeline and Filename formatting)
Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

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.

If you are looking for a tutorial, the gentoo wiki contains a nice one:


GNU Screen can be found in the extra repository:

# pacman -S screen


Commands are entered pressing Control A and then the key binding. The escape key can be changed with the escape option in ~/.screenrc. IE:

escape ``

sets the escape key to `

Common Commands

C-a ?

Displays commands and it's 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 d

Detach from the current screen session, and leave it running. Use screen -r to resume

Start at window 1

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 ~/.screenrc:

bind c screen 1
bind ^c screen 1
bind 0 select 10                                                            
screen 1

Nested Screen Sessions

It's 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

Detaches the inner screen session.

C-a a K

Kills the inner screen session.

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 in your ~/.screenrc:

altscreen on

Use 256 colors

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:

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)

If you are using Template:Codeline from Template:Codeline, you may need to add this line in your Template:Filename to enable 256 colors while in screen.

terminfo rxvt-unicode 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'

Informative statusbar

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

Add to ~/.screenrc:

startup_message off

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

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

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 C-a [. However, this is very inconvenient. To use the scroll bar of e.g. xterm or konsole, add the following line to ~/.screenrc

termcapinfo xterm* ti@:te@

Add a GRUB entry to boot into Screen

If you mostly use X but occasionally want to run a Screen-as-window-manager session, here's one way to do it by adding a GRUB entry for Screen on a virtual console (text terminal).

GRUB allows you to designate what runlevel you want so we'll use runlevel 4 for this purpose. Clone an appropriate GRUB entry and add a '4' to the kernel boot parameters list, like so:

# (0) Arch Linux
title  Arch Linux Screen
root   (hd0,2)
kernel /boot/vmlinuz26 root=/dev/disk/your_disk ro acpi_no_auto_ssdt irqpoll 4
initrd /boot/kernel26.img

Add some entries to /etc/inittab to indicate what should happen on runlevel 4, substituting your user name for <user>:

# gnu screen on rl4
scr2:4:respawn:/sbin/mingetty --autologin <user> tty1 linux

The line uses mingetty to automatically login some user to a virtual console on startup. You will need to install the mingetty package (AUR). 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 isn't 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. We'll need to see that nothing else happens on virtual console 1 when we use runlevel 4, so remove '4' from the the first of the agetty lines:

c1:235:respawn:/sbin/agetty -8 38400 vc/1 linux

Once logged in we want to ensure that screen is started. Add the following to the end of your .bash_profile:

  vico="$(tty | grep -oE ....$)"
  case "$vico" in
	tty1) TERM=screen; exec /usr/bin/screen -R arch;;

This checks for the current runlevel and will launch a screen session immediately after the autologin if the runlevel is 4.

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 check to see if we're on virtual console 3:

  vico="$(tty | grep -oE ....$)"
  case "$vico" in
	vc/3) TERM=screen; exec /usr/bin/screen;;

Set inittab/mingetty to automaically log in to vc/3 on runlevel 5 and you're set.

Fix Midnight Commander hard hang when starting in screen

In some cases (need deeper inspection) old gpm bug gets alive. So, then you try to run mc inside screen, you get a frozen screen's window. Try to kill gpm daemon before starting mc and/or disable it in /etc/rc.conf.

See Also