Screen: Tips & Tricks
[ Friday, 13 July 2007, Treadstone ]
In this article I will describe a very useful program: GNU Screen. Usually this program is used by people who have a shell account on a Unix server. But it can be also helpful to people who haven’t yet started to use a terminal or even Linux/Unix at all. Screen — simply — is a program which enables users to create more system shells without the need of logging in multiple times. Moreover it allows to leave programs running after you’ve logged out. What can it be useful for? For example to continue compiling or downloading something when someone wants to use the computer, or to run more than one terminal for one ssh connection. There are many more possible ways of using it…
Author: Łukasz Olender
Throughout the text I will use this C-a for the shortcut of Ctrl+a and C-d for Ctrl+d. I’d like to emphasize that C-a A (Ctrl+a and next A) and C-a a are two different shortcuts :). I will also refer to the program simply as “Screen”, not GNU Screen, for simplicity reasons.
1. Quick start
To create a new terminal session just enter the command screen. It will grant you the access to, what appears at first look, to be an unchanged shell. But in fact there are many useful features a key sequence away. But let’s start from the begining
In the new shell, you can do the same things as in a normal terminal. When you want to shutdown the screen, you press the combination C-d. If you want to leave programs opened in screen running after logout, you have to press C-a d before. Then the text [detached] will appear on the display, what means that screen is running in the background, and you can log out knowing that all your programs are still functioning. When you want to reenter the screen session, type screen -r. You can have more than one screen session, detaching from active ones and creating new ones. But to reattach to any of detached session, you have to know its PID (Process ID), or name. To see running sessions type screen -list. The first column contains the PID of each session. To restore a session type screen -r [PID]. But it is very inconvenient to remember or list these numbers every time you want to reattach. Fortunately you can use the “-S” switch. With its help you can give a name to each session when creating it. To create a session named “wget” you type screen -S wget. This makes it far easier to get back to any session. Just enter screen -r wget and it’s done.
2. Running programs in the background
Sometimes it would be useful to make screen automatically open a program in its session, give it a name and detach returning to a normal shell, in one command. Use the “-md” options to do this. Combining this with the “-S” option gives some interesting effects:
screen -S ekg_session -md /usr/local/bin/ekg &
Screen will create one session, name it “ekg_session”, start the ekg program running inside it and then detach it. Using this method, checking your account on a unix server, you can have either online or “away from the computer” status all the time by one command, while doing other things on the server. Naturally, you can combine many commands into a single command, which makes returning to your work environment easier:
screen -S ekg -md /usr/local/bin/ekg & screen -S mc -md /usr/bin/mc & //
screen -S rtorrent -md /usr/bin/rtorrent &
This makes screen create three differently named sessions in the background with my favourite terminal programs - ekg, mc and rTorrent.
3. Regions and tabs.
Even though running multiple screen sessions is catered to, its creators give you another choice of how to work. You can run multiple shells within one session! What’s more, you can even display these shells in one terminal window.
But so as not to get lost in these windows, I suggest you create tabs similar to those found in web browsers. You can do this by writing to your configuration file ~/.screenrc one of the following codes:
hardstatus alwayslastline
hardstatus string '%{= mK}%-Lw%{= KW}%50>%n%f* %t%{= mK}
%+Lw%< %{= kG}%-=%D %d %M %Y %c:%s%{-}'
or
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}
(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m
%{W}%c %{g}]'
or
hardstatus alwayslastline
hardstatus string '%{gk}[ %{G}%H %{g}][%= %{wk}%?%-Lw%?%{=b kR}
(%{W}%n*%f %t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{g}][%{Y}%l%{g}]
%{=b C}[ %m/%d %c ]%{W}'
With the first code each tab will have a number, a name plus time and date. The second one will give you tabs that will be created in the center and the active tab will be highlighted in red and also have the hostname displayed. The last code will additionally display the processor usage. You can create your own codes but it is a rather specialized task - I refer you to the system manuals for more information.
You already have tabs, now it’s time to create new terminals.
Open a screen session then press C-a c. A new tab is created. But both tabs have the same title. To change the title of the active tab, press C-a A and enter a new name. Creating tabs without a shell but with a program running automatically is a useful feature. The new tab will inherit the name of the program. To use this option press C-a : and enter the following command screen [program name] (this is the way you enter all commands during the screen session). To go to the next tab press C-a n and to return to the previous one press C-a p. To switch between the tabs use C-a ". This will show you a list of all the tabs.
Regions
Regions are a very useful feature. They enable opening many programs and shells in one window of screen. To do it just press C-a S. The window will split into two separate areas. There will be your actual shell at the top part of the window and the bottom part will be empty. To switch to the bottom part press C-a [Tab]. You have to create a new shell in the new window, so press C-a c. You can create more regions by pressing C-a S more than once and creating new shells in them. Tabs display the active region shellname in its titlebar. You can also switch between tabs being in a region, so e.g: you have 4 tabs and 2 regions. Each region can display one of these tabs. A nice trick is that many regions can display the same tab and they will react simultaneously. It is difficult to describe but is very helpful when you work in multiuser mod and you want to see what another person is doing. You can change the size of each region by entering resize [size], where size is the number of lines.
![]()
Img. 1 Two windows filling whole the display with screen split into two regions
4. Configuration file
Previously I mentioned about the configuration file. I wrote that it is in a subdirectory in the home directory named ~/.screenrc. But you can have tens of such files, and when you want to use any of them run screen with the option -c [filename]. What these files can be? Simply it can contain any of the commands that you usually enter after pressing C-a :. Below I include one of configuration files which causes splitting a window into two regions and running ekg in the top one.
screen ekg— runs ekgtitle ekg— gives title to the windowsplit— splites window into two regions moving ekg to the top onefocus— switches to the bottom regionscreen— runs default shell, in my case it is bashtitle bash— sets title to the bottom regionfocus— switches back to the top one
As you can see, you can automate a lot of actions which you would do after connecting to a server or running a terminal. A full list of commands can be found in the manual.
5. Logging to file.
To log all the data displayed in a screen session or tab into a file, enter the following command:
logfile [filename]where filename is the name of the file where all the data will be logged. The default name is screenlog.0 (or 1,2,3… if there are more screen shells).flush [seconds]is used to set delay between each write to the logfile, by default it’s 10 seconds.- To start logging enter
log on, to stoplog off.
6. The password to screen session.
If there is an important program running in a session you can protect it by using a password. To do this enter password. Then you will be prompted to type your password twice.
After that the hashed password will be displayed. Next you have to copy it to the configuration file after the word “password” in the same line. To cancel the password associated with the session just enter password none in the session or delete the line from the configuration file to cancel the password permanently.
7. Screen running right after connecting to a remote host.
In fact this tip has not much in common with screen but still I think it is very useful. If you want to run screen right after connecting to a remote host just enter ssh [username]@[remote host name] -t screen -RD. Unfortunately this command will work only if there is only one screen session running on the remote host. If there is more than one session you have to know its PID or name and use this: ssh [username]@[remote host name] -t screen -R [sessionname/PID]. When you shut down the screen, SSH will automatically log out.
8. Locking the display.
To lock the display use the following command lockscreen or the shortcut C-a x. It doesn’t block the session (like it was in point no. 2), but only “freezes” the display. So detaching and attaching again will cause that you grant the access to session. It can be useful when you are connected to a remote server using the previous tip and you have to go away from the computer. You can’t change the password, because your own user password will be used.
9. Comments
I think these tips will be useful for you. I have been using screen for a long time without knowing about such things as regions, tabs and all features I described here.
Proof-read by trashcat, chaddy
Subscribe to RSS feed for this article!
27 Comments
- A hyperlink: <a href="polishlinux.org">GNU/Linux for everyone!</a>,
- Strong text: <strong>Strong text</strong>,
- Italic text: <em>italic text</em>,
- Strike: <strike>
strike</strike>, - Code: <code>
printf("hello world");</code>, - Block quote: <blockquote>Block quote</blockquote>

















Nice how to
Thnak you very much
Just a small fix in
Regions
to create a new shell in the new window, so press C-a C.
correct is to press C-a c
@mudrii: thanks, fixed.
is screen support vertical region split ?
I can not find any info
PS: for more detailed info about screen check http://gentoo-wiki.com/TIP_Using_screen
On Digg it was mentioned that this tutorial is good for those who don’t know the terminal. What can I use it for? With Ubuntu Linux?
On Digg it says: It can be also helpful to people who haven ’t yet started to use a terminal or even Linux/Unix at all.
Screen is used to make use of terminal easier. So — the tutorial may be good for those who haven’t used the terminal before but want an (kind of) easy way to get started. Of course you need to have a need for using a terminal in the first place. If you prefer GUI, screen won’t be very helpful to you anyway.
screen -x is also your friend. Allows the same screen to be attached multiple times
I absolutely love screen. At work I usually have some 4 or 5 named sessions going for different tasks. One thing I did not like initially was all the C-a business. I’m not a control sequence fan (can you guess which unix editor I use?
) so it was annoying.
I saw a tip to map it to the backtick character “`”. It’s almost like hitting the “ESC” button like I do in my favorite editor! Of course, if you want to actually TYPE an “`” you have to hit it twice. To set it up add the following to your .screenrc or type it into the screen command-line (which you get to by typing C-a :)
escape “
May favorite part about it is that I can use ` to quickly cycle through all my “tabs”.
You could change it to anything you like so just play around with it.
Thanks michuk for helping me moving my screen experience onto the next level. Great howto.
Thanks for the article. Screen rocks.
Here are my screen tips:
http://www.pixelbeat.org/lkdb/screen.html
I got one question about the screen. Scrolling. Is it possible? For example if I start some kind compiling process under the screen with a lot of debbuging information going into STDOUT and deatach at the beginning of the process. After some time attaching to the same screen I would like to see what has happened at the beginning but I can’t because I can’t scroll (actually i don’t know how) to the previous page like sift+PgUp in common bash shell.
It’s actually easier than a normal terminal session since you don’t have to use the mouse. Simply enter “copy mode” with ctrl-[ and you can then use vi-style key navigation, including the page up/down keys. Marking for copy is as simple as placing the cursor at your start point, hitting space, then moving the cursor to your endpoint and hitting space again. To paste the copied information, use ctrl-].
Do you know the technique for using Screen to share my SSH connection with a mentor in an office? For instance, I might be new at something, so I fire up Screen upon connection. The mentor can then supposedly login on SSH and attach to my session, either sharing keyboard control or monitoring it. We can then chat in another window where he gives me advice on something.
Thx Rick! And thx to the author for the howtows and thread. But Rick, it looks like Ctrl-a-[ key sequence should be entered to get into copy mode. Isn’t it? Anyway the advice is what i was looking for!!!!
Screen is excellent! Does anyone know how to change the prompt color (in .screenrc) to something other than the default colors?
the manual says this:
“Colors are coded either as a hexadecimal number or two letters specifying the desired background and foreground color (in that order).”
using
caption always “%{RW}%t - %n”
shows the caption as bright red text on a bright white background using the two letter combination.
so what if I wanted blue text (hex #0021A5) on an orange background (hex #FF4A00) using hexadecumal numbers as the manual mentions?
Ginger,
For changing prompt colors, you want to edit the environment variable ‘PS1′, found in ~/.bashrc for me (Ubuntu 7.04).
Here’s a good primer for tweaking colors in Bash:
http://systhread.net/texts/200703bashish.php
Very nice. … I especially like to use screen in system start-up scripts for programs that need/want a console.
2Super Mike:
You can try `screen -x`, but in this case you needa set permission up to share the same session between two users.
–
WBR
michuk, this article is very handy.
I am just wondering how you created two-column windows. Usually those windows are overlapped.
“ratpoison” and “quadconsole” can do two-column windows, but “screen” is more convenient.
GNU Screen is my favorite tool, and cannot live without it.
In latest/CVS version of GNU screen, you able to ‘VERTICAL SPLIT’ (using key ‘C-a |’ (left|right) , too.
Is it possible to start screen automatically when I log in?
I think adding it to my .bashrc will lead to infinite recursion since screen will launch bash which then will launch screen etc etc.
Making screen my shell won’t work either, because then it will try to start itself.
So how do I avoid having to type “screen” (or “control-r sc”
) every time I log in?
By the way, you don’t need to type “control-a :screen somecommand” to run somecommand in a new window, “screen somecommand” from the shell will work too.
Is it possible to start screen automatically when I log in?
On my system, adding the following two lines at the end of ~/.bash_profile worked:
screen
logout
In any case, if you are worried about infinite recursion, you can always test against $SHLVL. At login it is 1. After starting screen, it will be 2, etc.
I’ve tried having screen start automatically at login, but it ends up creating a whole bunch of screens.
What is the proper way to configure my system to have screen start automatically (and reconnect to any pre-existing sessions if there are any running) ???
Another tip I’d like to add is that you can change screen from using C-a for it’s command prefix to C-z. I tend to use emacs most of the time and the C-a key binding conflicts with the editor’s key bindings. You can do this by adding the following to your .screenrc:
escape ^zz
Since C-z is normally used to suspend a process if I forget where I am and use C-z in a non-screen terminal I can easily recover using “fg”.
Cool article. I’ve been using screen for about 5 years and I learned a couple of useful tricks here.
Now it’s about time to talk about copy & paste:
To enter Copy mode:
C-a [
or
C-a Esc
or even
C-a C-[
Then you can use the direction arrows, as well as pg up/down to navigate accross the pages of a screen. To begin selecting a text, press space, and then go to the end of your selection. Then, press space again.
You will see something like:
Copied 6 characters into buffer.
Now, try changing your window and press:
C-a ]
You will be pasting your buffer directly to the standard input of your other window. Pay attention, as it is very important: Paste pastes into your standard input, not standard output. Thus, it is like you have really typed what you have pasted.
Instead, if you want to paste it to a file, say, a log, or something, just do like this:
C-a >
Screen will create a file under /tmp with a name like screen-exchange containing exactly what you have copied into the buffer.
While yet in Copy mode, you can use it just to search for a pattern. It’s like less and vi:
/pattern
searches for a pattern from the point you are to the end of the page, while:
?pattern
searches backwards for a pattern.
How did you get the vertical white bars that divide the different screen windows?
Figured it out myself..
If you have the same question, here is the answer:
To get the divider bars, do NOT use the vertical split patch which can be found here: http://fungi.yuggoth.org/vsp4s/
But instead use the CVS snapshot of the current development version of screen. Here is the link:
http://fungi.yuggoth.org/debian/