A Brief Intro To GNU Screen
Although GNU Screen is available for almost all *nix operating systems, it
is widely overlooked and underappreciated tool. This page provides a very
brief introduction to screen.
What is screen?
Screen is a program that manages multiple console-based applications from a
single terminal login. For example, if you ssh into a remote system and run
screen, you can start up multiple text-based programs or shells and switch
between them quickly with just a couple of key strokes. Screen also provides
copy/paste between managed programs, a scrollback history for each managed
program, the ability to split the physical screen between two or more managed
programs, and a 'hardcopy' (screenshot) function. Even better, if you run out
of time while working on a project and don't want to lose your workspace, you
can 'detach' the current screen session from the terminal session so that you
can logout, move to another computer, reattach to your screen session, and all
your programs will still be running exactly as they were before.
Detaching/Reattaching Terminal Sessions
The detach/reattach feature is what made me start using screen. Suppose for a
moment that you're working on a large project at school or work, but for some
reason you need to logoff your current computer and finish the rest of the work
at home. If you aren't using screen, then you'll probably have to close all of
your programs (editors, mail programs, etc.) and cancel any long-running
processes (e.g. large programs compiling) before logging out. This can be a
pain because you'll have to reload all the programs when you get home and
restart any long processes (such as compilation). Using screen, this hassle
can be avoided:
- When you start working at school/work, type "screen" as the first command
in your login shell. Hit ENTER to clear the introductory screen and then
use your shell prompt like normal.
- When you have to stop working and go home, do not close/cancel the programs
you're running under your login shell. Instead, hit Ctrl-A, d.
This will detach your screen session from the terminal session. Your
programs will all vanish, but they are all still running in the background
on the computer.
- Logout as you normally would and go home.
- When you get home, login to the computer you were working on remotely (e.g.
with ssh or telnet) and type "screen -r" -- this will reattach you to the
screen session you were running before and all of your programs
will still be running; no work will be lost.
Running multiple programs/shells inside screen
Screen's ability to detach from the terminal session is very useful, but if it
couldn't manage more than one shell/program at a time, its use would be
limited. Fortunately, screen also provides "window multiplexing," or the
ability to run multiple programs inside of screen and switch between them with
a couple of keystrokes. When you first start screen, a single managed shell
session will be started. You can spawn additional managed shells at any time
by pressing Ctrl-A, c
to create a new "window" containing another shell
prompt. Each of these new shells will receive a window number and you can
switch between them by pressing Ctrl-A
, followed by the window number
you want to switch to (note that your initial shell is '0' and each subsequent
shell or program started takes the next available number). Furthermore, if you
want to run a program in a new window without first spawining a new shell to
start it under, you can simply type "screen progname
" inside one of your
screen-managed shells to start the desired program in its own window. For
example, "screen vi" will start the vi editor in a new window instead of under
the current shell. If you start up lots of programs inside screen, it can
become difficult to remember which number corresponds to which running program.
The keystrokes Ctrl-A, "
will present a menu which can be used to choose
the desired program.
When managing multiple programs, screen is capable of splitting the physical
display so that two or more of the managed programs are visible at a time. To
split the screen, press Ctrl-A, S
(note that the S is capital). This
will divide the screen in half, leaving the lower half blank. You can switch
between the two (or more) screen regions by pressing Ctrl-A, TAB
then load different programs into them using the methods described above. You
can close screen regions with Ctrl-A, X
Copy/Paste Between Terminals
Another great feature that screen provides is the ability to copy and paste
text between or inside managed programs. Pressing Ctrl-A, [
invoke copy mode, allowing you to navigate the screen with the arrow keys
and mark regions of text with the spacebar. Since every program managed by
screen maintains a scrollback buffer, you can move past the top of the screen
to find text that has scrolled off the screen (this is a useful feature
even if you don't plan on copying anything -- just hit ESC to cancel the copy
when you're done scrolling back). To paste the text that was marked in copy
mode, simply press Ctrl-A, ]
. You can either paste into the same
program or into another program managed by screen -- very useful when working
at a terminal without mouse-paste capabilities.
The GNU screen utility is a very powerful tool that will come in useful in
a number of situations. I've only mentioned a couple of screen's features
on this page; there are a lot more advanced things that can be done with
screen as well. Read the lengthy manpage or view the help screen
) to find out about what other things screen can do. One
thing to note is that all of screen's commands start with Ctrl-A, so if you
need to pass that specific keystroke to a program, you must press Ctrl-A twice.