SCREEN Tutorial

Author:
SCREEN Tutorial

While tabbed terminals might be
a regular feature in GUI based terminals like Gnome Terminal and KDE
Konsole, it is not a possibility in text mode console. Screen helps us
overcome this backdraw and also offers many advanced features. Screen
has a sharp learning curve, but once learnt it can optimise your time
and effort like never before. Screen doesnt come default installed with
many distros, but installing it is as easy as typing in “apt-get
install screen” in debian or “yum install screen” in Fedora/Red Hat.
You can also build from source.
The
applications of Screen include:


1.
Create new full screen windows

2.
Kill existing windows

3.
View a list of windows

4.
Turn output logging on/off

5.
copy and paste text between virtual terminals

6.
View scroll back history

7.
Switch between windows
and
much much more

Notations
Used:

 C-x
 

–  The ‘C’ character in the notation is not the C alphabet but
the Ctrl key, which is abbreviated as C (in the true spirit of an emacs
fan). C-a
A

 –  The Alphabet following the Ctrl+a is ‘A’ and not
‘a’ i.e. The commands are all case sensitive. For instance C-a A is
a different command from C-a a

Italilisation
– All the commands for screen and associated commands are italised for
easy viewing. e.g Ctrl+a is italised as C-a

Command
Line arguments:
The
manual page define the screen options as:

       
           
        screen
[ -options ] [ cmd
[ args ] ]


           
           
    screen
-r
[[pid.]tty[.host]]

To
run screen all you have to do is have screen installed (of course!!),
and then run “screen cmd args”, Where
cmd and args the command name and its respective arguments. for e.g,
 
           
           
        screen
 
emacs
 prog.c


will
open a screen terminal from inside the same window with emacs editor
opening the prog.c file.
The
command line options to screen are many, we will look at some of the
most used options here. The ‘-r’ option for screen stands for “resume
operation” and will be covered shortly.

Command
Line option
Meaning
-c
FILE
override
default config file with specified file
-d
(or) -D [pid.tty.host]
detaches
an elsewhere running screen session
-e
xy
changes
control character to ‘x’ and literal character to ‘y’. Default is ‘C-a’
and ‘C-a a’ for literal. This will be changed to ‘C-x’ and ‘C-x y’
respectively.
-h
num
Specifies
the history scrollback buffer length
-ls
(or) -list [MATCH]
specifies
all the detached screen sessions in the pid.tty.host format
-r
[pid.tty.host]

-r
sessionowner/[pid.tty.host]
This
resumes a detached screen process. The pid.tty.host is optional and can
be specified to choose if more than one screen process is running. All
the screen sessions can be obtained by the ‘-ls’  option
-R Resumes
first detached screen

This
is a small compilation of the command line arguments that you will
probably use. Many more are available, but the usage of which will be
very limited. The two most important commands are:

 -r
 — Resume session

 C-a
d

— Detach session

Default
Key Bindings:

[This
list is
not to
be memorised but to
know that
the options
exist
]

Key
Binding
Action
C-a
Prompt
for window number
C-a
Presents
Window list
C-a
0….9
Switches
between windows
C-a
Switches
input focus
C-a
C-a
Move
to last window
C-a a sends
literal Ctrl+a
C-a A Allows
change of title
C-a c
C-a
C-c
Creates
new window session within the present screen session
C-a C clears
screen
C-a d
C-a
C-d
Detaches
screen session
C-a
C-g
Toggles
visual bell
C-a H Start
logging to screenlog.N, where N is the window number
C-a h sends
hardcopy of screen to hardcopy.N where N is the window number.
(something like a screenshot)
C-a
i

C-a
C-i
Shows
information about the present screen
C-a

C-a n
C-a
C-n
Toggles
to next window
C-a

C-a p
C-a
C-p
Toggle
previous window
C-a S Split
region into 2 windows
C-a w
C-a
C-w
Shows
a lit of windows
C-a x
C-a
C-x
Locks
window
C-a
z
Suspend
screen
C-a
C-
Terminate
all screens
C-a : Enter
a command line
C-a {
C-a }
view
history of commands
C-a * shows
list of attached displays

This is a rather huge set of
commands (although not exhaustive). So the simplest way to view a
command even if you forget is to use the command C-a
?”
or view “man
screen”
or “info
screen”.

Screen
Commands:

All these commands for screen have to
be entered after using the
“C-a
:”
command in screen. They can also be
inputted into the .screenrc file, which will be present in the home
folder of the user. If it is inputted into the .screenrc configuration
file, those commands will be executed by default each time screen is
started.

[This
list is not to be memorised but to know that the options exist]

Command
Description
autodetach
STATE
Automatically detach the session on exit
bind
KEY [COMMAND [ARGS]]
Bind a command to a key
bindkey
[OPTS] [STRING
[CMD ARGS]]
Bind a string to a series of keystrokes
chdir
[DIRECTORY]
Change the current directory for future
windows
clear Clear the window screen
colon Enter a `screen’ command
detach
[-h]
Disconnect `screen’ from the terminal
displays List currently active user interfaces
echo
[-n] MESSAGE
Display a message on startup
escape
XY
Set the command and `meta’ characters
hardcopy
[-h] [FILE]
Write out the contents of the current window
help Display current key bindings
kill Destroy the current window
log
[STATE]
Log all output in the current window
only Kill all other regions
other Switch to the window you were in last
password
[CRYPTED_PW]
Set reattach password
quit Kill all windows and exit
sessionname
[NAME]
Name this session
setenv
[VAR [STRING]]
Set an environment variable for new windows
split Split region into two parts
su
[USERNAME [PASSWORD
[PASSWORD2]]]
log
in as username
time
[STRING]
display
time
vbell
[STATE]
Use visual bell
windows List active windows

OK Until now was the sharp
learning curve that I mentioned. It was an orgy of commands, but you
would not need to know all of this. Just read through this to know that
all these commands exist. The complete information is provided in the
manual pages. For the actual usage of screen read on…

Using
Screen

This section will describe some
of the most used commands and it’s applications in detail.

First and foremost, you will be
working with different virtual terminals, so how do you know which
window you are working on? well Screen defines the environmental
variable ‘WINDOW’. so a simple `echo
$WINDOW
`
will tell you which window you are working on.

Another useful feature of screen is the
ability to title windows. You can set the title of a window by using
the command ‘
C-a
A
‘ and can be verified using the command
C-a w

To create a new window just
type in ‘C-a
c
‘ and to move between
windows you can use ‘C-a num
where num is the window number (obtained from $WINDOW). To move between
named windows, you can use the command ‘C-a ‘
to name the session you want to move to.

Screen has the useful ability to define
user defined Key bindings. To create a key binding, you have to edit
the .screenrc file. In the file, add the following commands:

bind  

e.g.
bind  o title

What this does is that, the command key
C-a o‘ will cause the title for the window
to be asked. Where as, had we given the command as


bind  o title  “New Title”

this would modify the title of the
window as “New Title”.

The most useful feature of screen is
its ability to be detached and reattached as and when required. What
this means is that you can work on a terminal within screen, suspend it
and then come back to it at a later stage exactly as you left it. To do
this automatically you can set the command ‘autodetach on’ in .screenrc
or at the prompt (
C-a
:
). If you want to do this manually,
just use the commands ‘
C-a d‘ or ‘C-a
C-d
‘. Now to resume the detached session,
i.e. to reattach the session, you have to run the command ‘
screen
-r
‘ at the prompt. You can have more than
one detached session. To view a list of all the detached sessions, type
in ‘
screen
-ls
‘ at the prompt.

Screen also provides the same lock
feature as Window managers provide, i.e. all activity will be locked
unless a password is specified. To set the password use the command ‘
password‘ at the prompt (C-a :) or set the line ‘password
‘ in your .screenrc
file. Now that you
have the password, you can lock the screen by using the command
‘lockscreen’. It is that simple. You can also use the key bindings ‘
C-a x‘ or ‘C-a
C-x
‘ for doing the same. The screenlock
program is based on PAM and can be changes using the environmental
variable $LOCKPRG

You can name the sessions too as would
be named when you do ‘
screen
-ls
‘. You can suspend the activity of
screen by using the key binding ‘
C-a z‘ or ‘C-a
C-z
‘.

Some of the useful environmental
variables used by screen are PATH – locating program to run (similar to
the terminal’s PATH), LOCKPRG – to locate the locking program of
screen, SCREENDIR – used to specify alternate socket path (where the
detached sessions are saved), SCREENRC – to specify an alternate
configuration file apart from the usual .screenrc file in the user’s
home folder, WINDOW – used to specify the current window’s number.

A really useful application of screen
is when you ssh or telnet into a remote machine. All you have with you
is the friendly CLI (command line interface). In such a case, screen
comes in very handy, making wonderful use of bandwidth, resources and
time. Using screen, you can avoid making multiple connections to the
server. All you need is that screen be installed on the server machine.

Another friendly application of screen
is in Instant Messaging. In the case of IRC for instance, you can use a
CLI based IRC client such as irssi, and log in from a remote server.
Suppose you want to go out, and log out of your machine, you can set
the status to away in the client, and then detach the screen session
and log out from the remote machine. The screen session continues to
run on the remote server. So the next time you log in, you can resume
the screen session and see all the activity that took place as history,
thereby losing no data in spite of your lack of presence.

If you are used to emacs
and love the splitting of
windows.
You can
use that in screen too. All you have to do is ‘C-a
S
and there you have it 2 windows in the same window. You can switch focus between these windows using ‘C-a
and of
course
you can split the splitted windows
into
even more windows
.

Screen is a powerful CLI tool. Starting
to use CLI based tools might be initially hard. But the power of CLI
can be realised only upon usage. It has many advanced options, though
only the most useful ones have been explained here. All information can
be obtained from the manual pages.

Prashanth
Mohan

prashmohan
[at] gmail [dot
]
com

Leave a Reply

Your email address will not be published. Required fields are marked *