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'm 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 can type:

ctrl+a c: Creates a new terminal.

You can 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 to 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!

Comments

pat
pat's gravatar

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.


Posted at 1:15 a.m. on December 13, 2006

Casey
Casey's gravatar

Great post, thank you!


Posted at 6:49 p.m. on April 29, 2007

softsea
softsea's gravatar

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


Posted at 2:34 a.m. on May 29, 2007

Michael
Michael's gravatar

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.


Posted at 11:22 a.m. on July 2, 2007

rbytes
rbytes's gravatar

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


Posted at 8:09 p.m. on July 9, 2007

Anonymous
Anonymous's gravatar

Just thought I'd mention Ctrl-a a which does something similar to the "return" button on many TV remotes. It returns to the last screen you were on. It can be useful for trying to compare two screens quickly. Just hold down ctrl-a and you'll repeatedly switch between two screens.


Posted at 5:24 a.m. on October 11, 2008

Post a comment

HTML is not allowed. You can use markdown syntax to format your comment.