#14: Using screen
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
screen
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
screen
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
:
RT @reflinux: #Advent series "24 Short #Linux #Hints", day 14: Using #screen http://bit.ly/gCqA39