––thursday #1: screen

I’m trying something new: every Thursday I’ll go over how to do something with the command line. Let me know what you think.

If you are using a modern-ish browser, you probably use tabs to keep multiple things open at once: your email, your calendar, whatever you’re actually doing, etc. You can do the same thing with the shell using screen: in a single terminal, you can compile a program while you’re editing a file and watching another process out of the corner of your eye.

Note that screen is super handy when SSH’d into a box. SSH in once, then start screen and open up all of the windows you need.

Using screen

To start up screen, run:

$ screen

Now your shell will clear and screen will give you a welcome message.

Screen version 4.00.03jw4 (FAU) 2-May-06

Copyright (c) 1993-2002 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann


                          [Press Space or Return to end.]

As it says at the bottom, just hit Return to clear the welcome message. Now you’ll see an empty prompt and you can start working normally.

Let’s say we have three things we want to do:

  1. Run top
  2. Edit a file
  3. Tail a log

Go ahead and start up top:

$ top

Well, now we need to edit a file but top‘s using the shell. What to do now? Just create a new window. While top is still running, hit ^A c (I’m using ^A as shorthand for Control-a, so this means “hit Control-a, then hit c”) to create a new window. The new window gets put right on top of the old one, so you’ll see a fresh shell and be at the prompt again. But where did top go? Not to worry, it’s still there. We can switch back to it with ^A n or ^A p (next or previous window).

Now we can start up our editor and begin editing a file. But now we want to tail a file, so we create another new window with ^A c and run our tail -f filename. We can continue to use ^A n and ^A p to switch between the three things we’re doing (and open more windows as necessary).


screen seems pretty ubiquitous, it has been on every Linux machine I’ve ever tried running it on and even OS X (although it may be part of XCode, I haven’t checked).

Note for Emacs Users

^A is an annoying escape key, as it is also go-to-beginning-of-line shortcut in Emacs (and the shell). To fix this, create a .screenrc file and add one line to change this to something else:

# use ^T
escape ^Tt
# or ^Y
escape ^Yy

The escape sequence is 3 characters: carat, T, and t. (It is not using the single special character “^T”.) The traditional escape key is actually Ctrl-^, as the carat is the one character Emacs doesn’t use for anything. In a .screenrc file, this results in the rather bizarre string:

escape ^^^^

…which makes sense when you think about it, but looks a bit weird.

Odds and Ends

As long as you’re poking at the .screenrc file, you might want to turn off the welcome message, too:

startup_message off

Run ^A ? anytime for help, or check out the manual’s list of default bindings.

Did I miss anything? Get anything wrong? Got a suggestion for next week? Leave a comment below and let me know!

10 thoughts on “––thursday #1: screen

  1. You really should use byobu as it encapsulates screen giving a far nicer experience. Also note that many people are switching to tmux from screen as it is better architected and more usable. The next byobu version has also switched to tmux.


    1.  Unfortunately, OS X does not seem to come with either and I actually wrote this, in part, for 10gen’s marketing team, who gamely keeps trying to use the command line (they are the most technical marketing team in history, but they aren’t switching to Linux anytime soon).  But valid point.


      1. iTerm 2 for OSX now has built in tmux integration. And vertical splits, and pane layouts, and so much more.


  2. I love screen, and adding the following items make it much easier to use:

    Add this to your .bashrc file in your home directory if you use bash shell makes a nice taskbar:

    # customized taskbar for screens
    if [ $TERM = ‘screen’ ]
                PS1=”[u@h w-scr:$WINDOW] ”
                PS1=”[ d (t) ]n[u@h w] ”

    Also adding the following to a .screenrc file in your home directory gives more helpful info:

    defscrollback 9999

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

    shelltitle “$ |bash”


    1.  Thanks for the scripts! 

      For those of you who don’t want to paste random code into your .bashrc file, I pasted random code into my .bashrc file (no, just kidding… mostly) and here’s what the things above do: the customized taskbar changes your prompt to be something like “[username@host:twitter   current/working/dir-scr:2]” if you’re on the 2nd window of your screen session.

      The defscrollback controls how many lines screen keeps in memory, so how far back in history you can scroll. (See http://www.gnu.org/software/screen/manual/html_node/Scrollback.html)

      hardstatus can create fancy status lines, see https://bbs.archlinux.org/viewtopic.php?id=12571 for some sexy ones and http://www.debian-administration.org/articles/560 for some basics.


  3. tmux is the new kid on the block. It’s a ground-up rewrite of screen. http://www.techrepublic.com/blog/opensource/is-tmux-the-gnu-screen-killer/1901 explains why you may consider switching.


    1. Wow: “Theo de Raadt, the founder and project leader for OpenBSD, was impressed with the security of the tmux design: ‘The most impressive thing about tmux, in my view, is how
      frustrating the code audit was. In 2 hours, I found only one or two nits
      that had very minor security consequences. It was not accepted into the tree based on license alone. It is high quality code.'”


  4. I must be missing something.  As I’m old school when it comes to command line as anyone, and I simply can’t imagine using screen when I can use VNC and see all of them – top, editor, tailing a lot and 27 other things.   And if I really want to hide them behind each other, well, most X terminals have tabs too…   Why are people so attached to screen/screen-like implementations?


    1. Well, for one thing, it doesn’t come installed by default (afaict) which make it unusable when I’m ssh’d into someone else’s system.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: