GNU Screen

From ArchWiki
Revision as of 07:54, 5 September 2008 by Chochem (talk | contribs) (Add a GRUB entry to boot into Screen)
Jump to: navigation, search

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.


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

Focus on next 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

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 0 select 10                                                            
screen 1
select 1

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

Use 256 Colors with Rxvt-Unicode (urxvt)

If you are using rxvt-unicode-256color from the AUR you may need to add this line in your ~/.screenrc 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

Cause it's annoying. Add to ~/.screenrc:

startup_message off

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 a (somewhat clumsy way) to do it. It lets inittab create a small file, the presence of which tells .bashrc to start a screen session.

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/by-uuid/a29113d7-2204-49e9-be69-d94699eba466 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:

# gnu screen on rl4
scr1:4:once:/usr/bin/sudo -u <user> /bin/touch /tmp/rl4
scr2:4:respawn:/sbin/mingetty --autologin <user> vc/1 linux

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). 'Once' signifies that it shouldn't be repeated in case the user logs out, and 'respawn' is the opposite: if the user logs out, init repeats the command. The first command simply creates a 'flag file' to be picked up later by .bashrc. It is made (sudo -u) in the user's name so the user can delete it later. The second command uses mingetty to automatically login some user to a virtual console on startup. You will need to install the mingetty package (AUR). The mingetty command respawns on vc/1 (virtual console 1 - the one you get when you ctrl+alt+f1) so we'll need to see that nothing else happens on virtual console 1 when we use runlevel 4. 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. This is where the flag file becomes relevant. Add the follwoing to the end of your .bashrc:

  if [ -f /tmp/rl4 ]; then
	rm -f /tmp/rl4
	exec /usr/bin/screen

This checks for the presence of the flag file and if found, will launch a screen session immediately after the autologin. Quittig the screen session simply gives you a prompt but due to mingetty respawning you cannot logout on vc/1. Note that removing the rl4 file as soon as it is found is very important as you otherwise risk starting screen sessions inside of screen session causing all kinds of weird behaviour.

See Also