An Introduction to GNU Screen
From DIT NetSoc
http://www.textolochicnaget.com
Contents |
What is GNU Screen?
GNU Screen is a console based window manager and terminal multiplexer. This means you can use multiple applications from a single TTY or SSH (PuTTY) session. You can also "detach" your screen session and programs running within the session will continue running until you reattach.
There are several advantages to this. You can:
- View two or more applications in the same terminal
- Log out quickly and resume your session at a later date as you left it
- Keep an app or set of apps running all the time (such as an IRC client)
So how do I use it?
In this tutorial I'll cover the basic operation of screen:
- Attaching and detaching sessions
- Creating new windows and switching between them
- Splitting the screen
Before we start I'll just explain a simple naming convention I'll use to denote keypresses:
-
^A-space- This means you should press ctrl and A at the same time, release them and press space. -
^A-^I- This means you should press ctrl and A at the same time, release them, then press ctrl and I at the same time. -
^Q- This means press ctrl and Q at the same time.
For each of the exercises I'll assume you are logged onto the calculon server although operation should be the same on any GNU/Linux or Unix machine with screen installed.
Getting started
Screen is usually invoked in one of two ways. The first is simply to run screen as follows:
you@calculon:~$ screen
You might see a startup message. Press space to start using screen. It might not look like it has run but it has - the screen now contains a running instance of the default login shell (usually bash).
Another way is to specify an application to run when screen starts:
you@calculon:~$ screen irssi
This will run a screen session containing a running instance of the irssi IRC client.
Attaching and detaching
You can detach a screen by pressing ^A-d. You'll be placed back at your login shell. All the windows and applications in the screen session are still running in the background. You can reattach your screen session by running:
you@calculon:~$ screen -r
Something you might find useful is the remote detach feature. If you try to attach to a screen that's already attached elsewhere you'll see an error much like the following:
you@calculon:~$ screen -r
There is a screen on:
4444.pts-4.calculon (Attached)
There is no screen to be resumed.
Let's say you've left a PuTTY session at home logged into calculon and attached to your screen session. Now you're at college and you need to reattach but you're seeing this error - You could ring home in the hope that there's someone there who can close your session...
...or you could run:
you@calculon:~$ screen -rd
...to detach the remote session remotely and attach it in the current terminal.
There's also a sharing feature which allows two terminals to attach to the same screen session:
you@calculon:~$ screen -x
This means the session will be active on two terminals. This can cause problems if the first screen to attach is larger than the second one - some of the screen may be invisible to the second user.
Creating new windows
A new window in screen will automatically contain an instance of your default shell. You can open a new window by pressing ^A-c. You can close it by exiting the shell:
you@calculon:~$ exit
When the last terminal/application in the screen session is closed the screen session ends.
You can create as many new windows as you like but keep in mind there are other users on the machine so don't keep too many active at once.
Switching between windows
Each window in a screen session is assigned a number, the first being 0, the second being 1 and so on. to switch directly to a window press ^A-i where i is the number of the window, so to go to window 2 press ^A-2.
You can cycle through the windows with ^A-space. To go to the next Window by number press ^A-n and the previous one with ^A-p.
Splitting your screen session
You can create horizontal splits in your screen using ^A-S (NB! Captial S - Shift+S!). Each of these splits contains a window. You can switch focus between these windows with ^A-^I. Switching windows inside splits operates as normal.
To make the currently focussed window full screen (break the splits) press ^A-Q (NB! Captial Q - Shift+Q!).
You can create as many splits as will fit in your terminal. I've found that the most you can use practically is about three or four.
Kill Current Window
^A-k
Things to look out for
Locking the screen
It's quite easy to accidentally lock your screen session but never fear, it's also easy to unlock it. The two usual ways your screen can become locked:
-
^S- this locks the whole session. You can't switch between windows when this lock is enabled.^Qremoves this lock. -
^A-s- this locks the current window. You can switch to and use application in the other windows.^A-qremoves this lock.
Backspace doesn't work!
This is caused by the default terminal type not being compatible with screen's terminal emulation. Exit your screen session (Don't detach - close all shells and applictions in the screen session) and enter the following:
you@calculon:~$ echo "export TERM=xterm" >> ~/.bashrc && export TERM=xterm
When you restart your screen session the backspace key should now function as normal.
