Increase Your Linux/Unix Productivity With GNU/Screen

screen is one of my favorite GNU utilities; it provides a quick and easy way to have several open terminals and do multiple things at once. Another cool feature about screen is that it stays running even if the user logs out or disconnects. This enables the user to detach from their session and resume it later. This feature is a life saver when working on a remote machine through SSH. If I was disconnected for some reason, I can easily reconnect and resume my session.

screen is very easy to use. Below is a list of its main commands. Read on, experiment with them, and you will become proficient in screen in minutes. Believe me you will appreciate it.

Creating And Managing Sessions

To start screen, simply run the following command:

$ screen

This will launch a new shell. It's a normal shell that you can use to run any type of commands. Now that we have screen running, let's explore its main commands. As you will notice, all of these commands start with pressing and releasing ctrl+a, then pressing another key that represents what you want to do. For example, to create a new terminal window, you may type:
ctrl+a c: Creates a new terminal.

You may create as many terminals as you like. To cycle through terminals, you can either reference a terminal by its number, go to the next terminal, or the previous terminal:
ctrl+a 1 ctrl+a 2 ... ctrl+a 9: Goes to the nth terminal.
ctrl+a n: Cycles to the next terminal.
ctrl+a p: Cycles to the previous terminal.

There is also ctrl+a <space>, which has the same effect as ctrl+a n. It cycles through terminals one by one.

Copy and Paste

To switch to copy mode, use:
ctrl+a [: Enables copy mode.
Now you can move the cursor around. To start highlighting text, press space or enter. Once you're done with highlighting, press space or enter again. This will copy highlighted text to a buffer. To paste it, use:
ctrl+a ]: Pastes buffer.

You may also paste from or to a file, to do so:
ctrl+a <: Reads paste buffer from a file.
ctrl+a >: Writes paste buffer into a file.

Detaching

screen allows you to get out of the current session, logout, and later log-in and re-attach to the same session. This comes in handy in many situations; suppose that you have an active session to a remote server at your office, and you want to go back home but you can't close the current session for some reason, or that you're using SSH and got disconnected in the middle of something. screen makes this quite simple, if you want to detach from the current session, run the following command:
ctrl+a d: Detaches the current session.

To resume a previous session, run screen with the -r option:

$ screen -r
(Re-attaches a previous session)

If you have several instances of screen you may specify which one to re-attach by providing its PID on the command line:

$ screen -r PID
(Restores screen instance with a process ID of PID)

Exiting

To close screen, exit all running programs and shells.

Miscellaneous

To protect the current session with the current user's password, use:
ctrl+a x: Locks the current session.

For help, use:
ctrl+a ?: Shows a list of commands.

Conclusion

Whether you regularly use SSH, don't have a reliable Internet connection, or work from different machines, screen can be a bliss to your Linux/Unix experience. Learning it only takes minutes so there is no excuse not to use it!

Tags:
Submitted by Ayman on Sun, 2006/09/17 - 9:03pm.

pat (not verified) | visual screen writeup | Wed, 2006/12/13 - 2:15am

Hi Ayman,

Nice writeup! Screen is one of those things that it takes a while to wrap one's head around... but it's indispensable once grokked.

I wonder what you think of this little "visual" introduction to screen I put together:

A Visual Introduction to 'screen'

It's not quite as complete as yours in terms of commands but I find that some people (myself definitely included) can remember things a bit more permanently with pictures. ☺

Off to dig about in your blog a bit more, cheers.

Casey (not verified) | Great post, thank you! | Sun, 2007/04/29 - 7:49pm

Great post, thank you!

softsea (not verified) | Great post, it is so | Tue, 2007/05/29 - 3:34am

Great post, it is so easy-to-use, I every knoe this before,
very useful for me, Thanks Ayman.

Michael (not verified) | You are right, screen is | Mon, 2007/07/02 - 12:22pm

You are right, screen is really a useful utility and helps a lot when you work with linux. It is a pity that so many people have never heard of this tool and so give themselves a hard time.

rbytes (not verified) | I use screen to share a | Mon, 2007/07/09 - 9:09pm

I use screen to share a screen session with another user.

Here is introduction to screen in images:
http://blog.preshweb.co.uk/index.php/2007/03/05/gnu-screen-a-primer/

and here is splited vim & gnu screen sessions (very nice!):
http://daveg.outer-rim.org/entries/2006/01/11/split-vim-gnu-screen-sessions/">http://daveg.outer-rim.org/entries/2006/01/11/split-vim-gnu-screen-sessions/

good luck

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <strike> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <sup> <sub> <h1> <h2> <h3> <b> <i> <u>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

About

Ayman Hourieh

I'm a Computer Science graduate, an Open Source enthusiast, and a Googler.

I'm 24 years old, and live in Dublin, Ireland.

This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.

More

Books

Learning Website Development with Django

Learning Website Development with Django
A beginner's tutorial to building web applications, quickly and cleanly, with the Django application framework.

My first book. Published by Packt Publishing in April 2008.

Icons

Get Firefox!
Drupal.org
Linux
Gentoo
Creative Commons License