Casey Brant

Jan 11, 2009

Intro to screen

NOTE: This post originally appeared as part of a weekly series on Unix tools I attempted to write way back in 2009 called “52in09”. Not only did it have a lame name, I didn’t make it past the first few weeks. Also, the formatting here is kind of janked up, but I’m not planning to take the time to fix it.

Really, here in 2019 where I’m writing this preface, you should just use tmux.

Intro

Screen is one of the most useful Unix programs out there, so it seemed like a good way to kick off the 52in09 challenge. If you’re just starting to learn Unix, you should try to get some of the basics down first, but screen can be one of the first commands you learn once you’re comfortable at the command line. I guarantee that once you become familiar with screen, you’ll forget how you lived without it.

Screen is described by its designers as a “terminal multiplexer.” This is a geeky way of saying it makes it easier to do lots of things at once. Since its creation, however, screen has gained some abilities that go beyond just multitasking. The three reasons I like screen so much are that it can (1) separate processes from the originating shell, (2) provide a really effective way to multitask, and (3) provide an easy way to share sessions with other users.

Points 2 and 3 are kind of self-explanatory, but if you aren’t familiar with screen, the implications of point 1 might be unclear. What I mean by separating a process from its origin is that with screen, you can start an interactive program like a text editor, completely log off the system, remote log in from a different client, and be able to pick up right where you left off. This is much more powerful than remote login by itself, because you can keep one “session” going basically forever, no matter where you are.

As an example, I use AOL Instant Messenger for all of my IM conversations. I like to leave it running all the time in case someone wants to leave me a message while I’m out. I also like to go to a nearby coffee shop to work on things. Until I found out about screen, I either had to sign off my home computer before I left or risk missing a message in transit. Now, I run a text-based AIM client in a screen session, and I can log in from anywhere and see what’s happened while I was gone! That’s kind of a silly example, but I’m sure you can come up with some cases where that kind of functionality would be a real boon to your own computing.

Well, enough exposition. What follows is intended as a tutorial of screen for the new user. It doesn’t function very well as a reference, but that’s what the man pages are for! So without further ado…

The Basics - An Overly Verbose Screen Tutorial

Start screen Type screen to start a new session. You’ll see a splash screen telling you what’s going on; hit <return> to clear it, and you’re back to a shell prompt. Treat this just like a regular shell and do your work. If you get to the point where it would be nice to have more than one program running at once, you’ll want to…

Make a new window All screen commands are prefixed by the control character, Ctrl+a by default. Type C-a c (that means to press Ctrl+a, let go, then press 'c'. That’s the notation I’ll use from now on) to create a new window. Now you have two shells running inside the single screen session, and they can be doing different things. To get back to the other one, you’ll want to be able to…

Navigate C-a n and C-a p go to the “next” and “previous” windows in your session. C-a " will give you a menu of current windows (use the arrow keys and <enter> to pick the one you want). C-a <NUM>, where <NUM> is a numeral 0-9, will take you to the window with that number. If you have more than 10 things going at once, you’re kidding yourself about your ability to multitask. But if you insist, C-a ' will give you a prompt where you can type any number or window name and switch to it. Speaking of that, make sure you are utilizing…

Window Names On my screen install, every new window I make gets called “bash” by default. Not helpful. Press C-a A (that has to be a capital 'A') to get a prompt for naming. I usually type in whatever I intend to run in that window. This also makes C-a " a whole lot more useful. By this point, you might be getting a little confused. The most important thing to remember is…

Online Help C-a ? will get you a handy list of commands. It doesn’t have definitions or anything like that, but it will usually jog your memory enough that you know what you need to search for in the man page. If you’re done with a task and don’t have any further use for the window it was in, you should probably…

Kill Window C-a k will remove a window from your session. I like to keep a very small number of windows running at once, so I use this a lot. You certainly don’t have to, of course. Screen can handle a huge number of windows in any session, so the limitation is just what your mind can keep track of. After you’ve done some work and are ready to log off, you’ll want to…

Detach from session C-a d detaches from the current screen session. This is the real power of screen: If you’re running a window manager, you can go ahead and close the terminal window; your session is still alive. You can even log totally off the system, and it still survives. As long as you don’t power off the box that’s running your screen session, you’ll be able to pick up right where you left off any time in the future from any location with…

Reattach to session When you’re not inside screen, you can type screen -ls to see what sessions are running. Type screen -r <SES_NAME> (where <SES_NAME> is the name of the session you want) to jump back into that screen session. If you’re still attached to that session somewhere else (say you forgot to log off back at work and now you’re home), screen -x will do the trick. That will get you two simultaneous connections. That’s only with the same user account, however. There are a couple of extra steps to take care of before we can have…

Multiuser sessions Screen is a great way to engage in remote collaboration on a project or give a demo of something. For security reasons, you can’t do this by default, so you’ll need to enable it. Try to convince your admin to run chmod +s /usr/bin/screen at some point (this only needs to happen once) before you start your session. Once the session is up, type C-a :multiuser on to get started. Figure out who will be joining your session and type C-a :acladd <USER1,USER2,ETC.> where <USER1,USER2,ETC.> is a comma-separated list of the usernames who will be connecting. Note that these need to be users on the system; you don’t get to assign nicknames or anything like that. There are lots of permissions to be set here as well. Check out the aclchg portion of the man page for more info. It’s a little tricky at first.

Join the multiuser session If you’re trying to join a session, first log on to the same system as the screen host. screen -ls is probably not going to give you a lot of information, since it only displays your own sessions, so you’ll need to have your host tell you the name of the session you’ll be joining. If they ran screen -S <SESSION_NAME> instead of vanilla screen, it might be easier. Type screen -x USER_NAME/<SESSION_NAME> and you’re in! If you want to catch up on what’s already happened, you may be interested in the…

Copy/Scrollback Mode C-a [ will start up the scrollback buffer. Using the “vi keys” (h=left, j=down, k=up, l=right) you can scroll around in any screen window. This mode also lets you copy regions of text to a clipboard. Spacebar will open up a copy region; move to the end of the region you want and hit <spc> again, and screen will put that text into a buffer. &lt;esc&gt; will get you out of the copy buffer, and C-a ] will paste the text into the regular screen window. Check out the copy mode part of the man page, as it can do a lot more than what I just mentioned here.

Screen split If you’re the kind of person who likes to have a lot of stuff going on, you may start to feel limited by having to look at only one screen window at once. C-a S splits the window in two (or three, four, etc.) and C-a <TAB> moves around between open splits. C-a X closes the split your cursor’s in, and C-a Q closes every split except the one your cursor’s in.

Screen config file At this point, we’ve covered all of the basics you need to get started with screen, as well as a couple of slightly more advanced features. By now you’re ready to tackle the man page and learn the rest of the powerful features screen is capable of.

There’s one last thing I’ll leave you with. Like many Unix programs, screen makes use of a .rc file in your home folder (.screenrc, surprisingly enough) to customize settings. If you run cp /etc/screenrc ~/.screenrc you’ll get a nice starter file. Here are the extra customizations I’ve added in my own .screenrc (just enter each of these into the file on its own line):

nethack on -> Makes all of the screen info messages silly.

defscrollback 5000 -> Increases the amount of information screen will store for the scrollback buffer.

hardstatus alwayslastline -> Gives you a highly customizable permanent status bar on the last line of your terminal.

hardstatus string "%{= kG}[ %n %t ] %u" -> The status line formatting language is pretty arcane, but this one just displays the name and number of whatever window I’m currently looking at, followed by a list of other users connected to my session.

Conclusion

If you want to learn more about screen, your best bet is the man page. Screen’s manual is very well written and thorough, but if you can’t find what you want there, just Google for it; since screen is so awesome, there are loads of articles out there.

That’s it for the first week of 52in09! Thanks for reading, and make sure to come back next week for more Unix command goodness. Please leave comments on this post to tell me what you’d like to see me do differently or what’s working well. And of course any screen questions are welcome too!

Hey, thanks for reading. I don’t have comments on my blog, but I’d still love to hear from you. These are the best ways to reach me:

Or, check out my other posts and projects.