#14: Using screen

Posted by | Comments (5) | Trackbacks (2)

If you work on remote servers via SSH and don't want to open multiple connections, or if you don't have a graphical terminal emulator and don't want to open several concurrent ttys, screen is the right tool for you. screen acts as window manager on the command line. It opens a session, which can contain multiple terminal instances. It receives your input and delegates it to the current shell instance/window. With the keystroke Ctrl+a (abbreviated C-a) you can send commands to screen.

To open screen, just run


This opens a new screen session with an empty shell (if you see some weird text about copyright, GNU GPL, beer and pizza, press space or return). You now have a terminal that you can use. If you want to open another shell in a different window, press C-a c to create one and jump directly to it. To switch between both windows, press C-a n to go to the next or C-a p to go to the previous window. To close a window either type exit to close the shell or hit C-a K. To see a list of all windows, press C-a ". You can switch between them by using your up and down arrow keys and pressing enter on that window you want to go to.

The windows are named automatically after the commands they run (e.g., bash), but you can easily rename them. Hit C-a A to change the name. Now the new name appears in the window list you get with C-a ".

Another very useful thing is the copy mode. Really, screen lets you do copy&paste on the console. To enter copy mode, press C-a [. Now you can move the cursor using the arrow keys. To start a selection, hit spacebar – to end a selection hit spacebar again. To insert what you've selected in the current command prompt, hit C-a ]. But the copy mode is not only a copy mode. You can also use the copy mode to scroll beyond the upper screen edge. screen has a fairly big text buffer, so you can scroll through many lines of text which is not on the screen anymore, just move the curser upwards using the arrow or page up/down keys. If you want to exit copy/scrollback mode, hit either ESC or q (some other keys like s also work). If you can't use the arrow or page up/down keys for some reason, you can also use different keys for movement. Use h, j, k, l to move left, down, up and right, 0 or ^ and $ to move to the first or last non-whitespace character of the current line or + and - to jump down or up one line. You can also jump to the beginning of the buffer with g or to the end with G. If you enter a number before G, you can also jump to that specific line. By the way, these keys are the same as the default vim keys. If you know vim, you can move around in copy/scrollback mode very efficiently. Of course, there are many more movement keys, but I can't list all of them here.

Once you have started a screen session, you don't have to stick to it. You can detach a session at any time, just hit C-a d. This closes screen but doesn't terminate the session. The session still runs in background and can always be reattached. To reattach/resume a session, run

screen -r

If there is more than one detached session, you get a list of all of them. To reattach a specific session, pass its name/PID to screen -r as printed in the list. To resume or create a session (if it doesn't exist yet), use the parameter -R instead. To detach sessions running in other terminals, use screen -d, to take over another session, run screen -d -r.

You can also use the same screen session from multiple terminals at the same time. This is very interesting for working collaboratively. The first person starts a new screen session with


and the second person joins this session by running

screen -x

Now all participants can see what the others are doing.

screen is a very exciting tool and I can't teach you all commands here. For more information read the manual and learn what you haven't known yet about screen, it's definitely worth the effort.

Read more about screen:


robo47 sent a Trackback on : (permalink)

RT @reflinux: #Advent series "24 Short #Linux #Hints", day 14: Using #screen http://bit.ly/gCqA39

Manko10 sent a Trackback on : (permalink)

RT @reflinux: #Advent series "24 Short #Linux #Hints", day 14: Using #screen http://bit.ly/gCqA39


There have been 5 comments submitted yet. Add one as well!
vector wrote on : (permalink)
useful article. thanks
George wrote on : (permalink)
I always thought of split-screen as one of screen's most important features. (C-a S or C-a | to split, C-a tab to switch subwindows, C-a X to kill a subwindow) It's kind of superfluous when you can just open another xterm or something (though xterm makes screens copy/paste mode superfluous as well) - but it's handy in cases where I'm ssh'ing in without X forwarding. Though certainly session detaching could be useful if I used a TTY-based bittorrent client or something... It should also be noted that screen works by acting as a terminal emulator - it listens to each session's output, watching for control codes, and uses that to maintain a buffer of the session's screen state... This is important because the terminal emulation imposes some limitations that may not normally apply to the terminal in which it runs. For starters, it doesn't support the mouse input modes supported by xterm, etc. Screen basically chokes on aptitude's output (because of its use of extended character set, I think?) But other console programs like alpine or frotz do OK - I guess it'd be fair to say that it's aptitude that's not written to be compatible with different terminals, but on the other hand, color xterm is pretty much the standard for terminal capabilities these days and screen doesn't match it...
Janek Bevendorff
Janek Bevendorff wrote on : (permalink)
Thank you for your addition, George! Split screen is indeed a very handy thing in screen. Other programs like vim also provide split screen, but screen does it as the terminal emulator. Very useful. Could you explain a little bit more, which problems you have when using aptitude? I'm not a Debian or Ubuntu user, but here on Gentoo I've never experienced any issues with meta characters. Portage (or emerge, respectively) has pretty fancy output with lots of control characters, never had any problems. So it actually seems to be aptitude. Personally, I've never liked that tool, as it's just a slight improvement compared to the nasty apt-get command.
George wrote on : (permalink)
Actually I think the problem I was having with aptitude may have just been a bad interaction between my terminal window (rxvt) and screen. I'm not sure what the issue is, exactly - aptitude in rxvt has a few errant characters but nothing that messes up the overall display too badly. aptitude in screen in rxvt has additional errant characters that screw up the display. screen in xterm handles aptitude fine. So maybe it's just an rxvt issue...? I'm not sure. So, I don't know, maybe screen's terminal emulation is better than I thought. It's been a while since I dug into the terminfo table to see what it can do. I do remember it doesn't handle mouse mode - though honestly that's not a feature I generally want to use anyway...
Janek Bevendorff
Janek Bevendorff wrote on : (permalink)
screen emulates a VT100/ANSI terminal in each window, but also depends on the capabilities of your native terminal or terminal emulator, in which it runs. Have a look at "this":http://www.gnu.org/software/screen/manual/html_node/Window-Termcap.html

Write a comment:

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

By submitting a comment, you agree to our privacy policy.

Design and Code Copyright © 2010-2024 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms.