GNU Screen Basics

Those who use the command line a lot eventually find a reason to want a way to have one terminal behave as if it were several terminals. This is initially a weird concept, but turns out to be very useful. For example, I can log in to a remote server, run a compile in one terminal and continue to work in another terminal in the same session, but still glance at the compile occasionally. I could of course open several local terminals and ssh to that remote server in all of them, but at some point this becomes inconvenient and it's much easier to ssh once and let that one session carry multiple shell sessions.

There are two main programs that do this: screen (the original and older one) and tmux. If you're not familiar with them, here's another reason to become familiar: if you're using them on a remote machine and your connection is dropped, all programs running inside screen or tmux will continue to run - they are NOT terminated by your disconnection and aren't even aware that anything has changed. You ssh back in and reconnect (reconnecting requires a special command, we'll get to that later), and everything is still running.

If you're just starting with this concept now, use the younger and less crufty tmux. Unfortunately, after years of using only tmux, I find that I'm having to use screen on some servers where tmux is unavailable and I can't install any software. So ... it's time to get reacquainted with screen. Don't get me wrong, screen is a fantastic program - tmux is just ... a bit better. And I just wish I didn't have to store the knowledge of both in my already crowded skull.

Start screen just by typing its name in the terminal or in an ssh session on a remote server. You may or may not see a startup screen. It will look almost exactly like all you've done is created a subshell, although it may have a status bar along the bottom of the terminal. I personally really don't like it when I'm in screen and there's no indicator that this is the case, so I always make sure I have the status bar.

To exit screen, just type exit as you would for any subshell. (If you have several shells open, you'll have to exit all of them.)

Here's the most basic config file to switch the two settings I find most annoying:

# add this as ~/.screenrc
startup_message off
hardstatus alwayslastline

After you create that file in your home directory, try running screen again: if you had a startup message it should be gone, and if you didn't have a status bar at the bottom of the screen, you will now.

By default, the main command for screen is 'Ctrl-a'. This can be changed in the configuration file, but it seems most people keep it and I'll assume that's what you're using. By itself, 'Ctrl-a' does nothing: it's a preface to a screen command. The first command you need is 'Ctrl-a c' which will create another "window" (this is what screen calls it - I'd call it another shell - but we'll use their terminology). If you're following along, you now have two terminals inside screen. From here, there are several basic screen navigation commands: 'Ctrl-a n' goes to the next screen: if you have several screens, use this over and over to step through all of them. (There are better ways to navigate, but this is a good start.) 'Ctrl-a Ctrl-a' toggles between this and the last-used window: this can be very useful for a quick switch between your two most-used windows. 'Ctrl-a ?' shows you basic help ... but honestly it's a lot like a man page, it's only useful after you've been using screen for a long time. More human readable (but more verbose - that's why it's more readable ...) is the "DEFAULT KEY BINDINGS" section of the man page, which I highly recommend at least glancing at.

Those are the most basic commands, the ones you're likely to use the most. The first command I wanted after those was a way to rename the windows so I could tell them apart - otherwise they all say "<N> bash" on my system as "bash" is my default shell. That command would be 'Ctrl-a A' (capital "A" - which is different from small "a" command!). This will prompt you to rename the current window. I recommend sticking with short names that describe what you're doing in that particular session. If you have multiple windows, the quickest way to navigate them is usually 'Ctrl-a "' (double-quote), which opens a list of all windows, allowing you to select by using the up-down arrow keys. Another quick navigation method is 'Ctrl-a <N>' where '<N>' is a number from 0 to 9, the window number. If you have more than ten screen windows, this system starts to break down (I rarely have more than four or five open at once). If you need to send an actual 'Ctrl-a' to a window, use the 'Ctrl-a a' command. If you find you need this a lot, then you might consider changing screen's default command to something other than 'Ctrl-a'. Consider 'Ctrl-b' because that's what tmux uses ... or if you don't want them to be the same, choose something else.

Continue on to GNU Screen, Part 2.