Screen

From Hack Evergreen Wiki
Jump to: navigation, search

GNU Screen (usually just called screen) is a program that allows the user (ie, you) to command persistent console sessions on a Linux server. This article covers its basic functionality for use on ada, but screen comes packaged with most Linux distros and should work the same.

It's not only useful for keeping you always logged into IRC, but many use it to keep a "virtual workspace" of sorts. Leave your command line programs running at all times, even when you're not logged in. Have one window with irc, another with a file open in vim, another where you're using wget to download Hackers in 720p. Next time you log in, it'll all be there just like you left it.

Another popular alternative is tmux, but meh.

Getting Started

Easy! Connect to ada with ssh and invoke the screen command:

ferris@home-computer:~$ ssh buefer21@ada.evergreen.edu
Password:

. . .

Welcome to ADA!
buefer21@ada:~$ screen

What happened there? After typing screen, it looks like nothing's happened. But that's the beauty of screen, baby! It opens a virtual console on ada that'll stay there even after you out log out of ada. There's a lot of key commands to control screen, each starting with ctrl + a held at the same time (henceforth referred to as C-a) followed by a command.

Use C-a ? to get all the commands, or just jump to the cheatsheet.

Attaching/Detaching/Reattaching (*taching)

Right now you're "attached" to a screen session you created. If you're on ada then you'll probably have a gray window bar at the bottom, but that's only because ada has a totally sweet screen config by default. Try C-a d to detach:

C-a d
[detached from 24697.pts-12.ada]
buefer21@ada:~$ 

Check it! You've detached from your screen session. Run screen -dr and...

buefer21@ada:~$ screen -dr

Wham! You're right back in your screen session! And it'll be just how you left it.

  • r means reattach to the screen.
  • d tells screen to detach the screen if it's attached in another terminal.
    • Handy if you might've left yourself attached elsewhere.

Window Management

Each screen session can have a number of windows to flip between. Try using C-a c to create a new window, then C-a A to name it.

Use C-a n and C-a p to switch to the next/previous window. C-a [number] will move you to that particular window number. Or use C-a a to flip-flop between two windows.

Type exit in a window to close it, or use C-a k.


Using with IRC

See also: IRC

Woooo! This is probably why you're here. Using using an IRC client within is handy so you don't have to always be logging in and out, and you'll be able to view the chat history (which you would lose if you weren't in the channel).

The IRC page has details on how to use various clients. Once you learn how to use screen, you can keep a window running an irc client at all times.

buefer21@ada:~$ screen -dr
(in a screen session now)
buefer21@ada:~$ irssi

Advanced functionality

Managing multiple sessions

If you have more than one screen session active at once, you'll have to specify which one you want to attach to. screen -ls will show you your active sessions:

buefer21@ada:~$ screen -ls
There are screens on:
       25255.pts-12.ada        (04/17/2013 05:04:59 PM)        (Detached)
       25245.pts-12.ada        (04/17/2013 05:04:56 PM)        (Detached)
       24697.pts-12.ada        (04/17/2013 04:13:32 PM)        (Detached)

Type your reattach command, followed by something that uniquely identifies that session:

buefer21@ada:~$ screen -dr 25255
--------- OR ----------
buefer21@ada:~$ screen -dr 24

Here the third session is the only one with '24' in it, so I can use that to identify it. You can also name your sessions; when you start a new screen, use the -S flag:

buefer21@ada:~$ screen -S schoolwork

That'll start a screen session name 'schoolwork', so you can just reattach with screen -dr schoolwork.


Command cheatsheet

Make sure to try C-a ? to see the manual of available commands.

Session management / Attaching

To be done on the command line outside of a screen

command function
screen -S <name> start a new screen session with name <name>
screen -ls list running sessions/screens
screen -r attach to a running session
<code.screen -r <name></code> reattach to session <name> (can also be a uniquely identifying string from screen -ls
screen -dRR The 'ultimate reattach'. Attaches to a screen session. If the session is attached elsewhere, detaches that other display. If no session exists, creates one. If multiple sessions exist, uses the first one.


Every command hereafter are screen key commands which use the meta-escape key (Ctrl + a by default, written as C-a)
They are used within screen to modify the current session.

Detaching

command function
C-a d detach
C-a DD detach and logout (quick exit)

Window Management

command function
C-a c create new window
C-a A rename current window
C-a C-a (commonly used to flip-flop between two windows) change to last-visited active window
C-a <number> change to window by number (only for windows 0 to 9)
C-a ' <number or title> change to window by number or name
C-a n or C-a <space> change to next window in list
C-a p or C-a <backspace> change to previous window in list
C-a " see window list (allows you to select a window to change to)
C-a w show window bar (if you don't have window bar)
Close all applications in the current window (including shell, using exit) close current window
C-a k kill current window (not recommended)
C-a \ kill all windows (not recommended)

Split screen

command function
C-a S split display horizontally
or C-a V split display vertically
C-a tab jump to next display region
C-a X remove current region
C-a Q remove all regions but the current one

More

See here for more, or just rtfm.