100 Useful Command-Line Utilities

by Oliver; 2014

65. screen

Imagine the following scenario: you're working in the terminal and you accidentally close the window or quit or it crashes. You've lost your session. If you were logged in somewhere, you'll have to log in again. If you had commands you wanted to refer back to, they're lost forever. This is where screen or tmux comes in. Regardless of whether or not you quit the terminal, your sessions are persistent processes that you can always access until the computer is shut off. You can get your exact terminal window back again: you're still logged in wherever you were and, moreover, you can see everything you typed in this window by simply scrolling up. For this reason, it's a good idea to make using screen or tmux a habit every time you're on the terminal, if only as insurance. screen also allows you to access multiple terminal instances within the same terminal window and more.

A word of advice: skip the rest of this section. Learn tmux, the superior program (discussed next), instead of wasting brain space on screen.

Start screen:
$ screen
Let's see an example of what this looks like:

image

This looks like an ordinary terminal window except that you see a bar at the bottom with some names:
  • 0 bash
  • 1 bash
  • 2 bash
These are all the terminal instances we have access to in this one screen session (and in this one window).

So, let's get this straight: we can have many different screen sessions and, within each session, we can have multiple instances which are—muddying the terminology—also called windows (but not the kind of window you can create on the Mac by typing ⌘N which I was referring to above).

List current screen sessions:
$ screen -ls
Another figure:

image

To reattach—i.e., enter into—screen session of id 1167:
$ screen -r 1167
To kill a screen session of id 1167:
$ screen -X -S 1167 quit
Once you're in a screen session, what are the commands? By default, each screen command starts with Cntrl-a, but I find this impractical because this combination's default key binding in bash is to jump your cursor to the beginning of the line, which I use all the time. So I like to use Cntrl-f as my Leader Key. You can use whatever you like—we'll see how to set it below with .screenrc. Let's learn some of the basic screen commands. I will copy some of these directly from here:

<Leader> cCreate new window (shell)
<Leader> kKill the current window
<Leader> nGo to the next window
<Leader> pGo to the previous window
<Leader> <Leader>Toggle between the current and previous window
<Leader> dDetach the screen session
<Leader> ASet window's title
<Leader> wList all windows
<Leader> 0-9Go to a window numbered 0-9
<Leader> [Start copy mode
<Leader> EscStart copy mode
<Leader> ]Paste copied text
<Leader> ?Help (display a list of commands)

A note: to scroll inside screen (you'll find you can't use the window scrollbar anymore), enter into copy mode and then navigate up and down with Cntrl-u and Cntrl-d or j and k, as in Vim.

You can configure screen with the file .screenrc, placed in your home directory. Here's mine, which my co-worker, Albert, "lent" me:
# change Leader key to Cntrl-f rather than Cntrl-a
escape ^Ff

defscrollback 5000
shelltitle '$ |bash'
autodetach on

# disable the startup splash message that screen displays on startup
startup_message off

# create a status line at the bottom of the screen.  this will show the titles and locations of
# all screen windows you have open at any given time
hardstatus alwayslastline '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'

#use F8 to turn the status bar off at the bottom of the screen
bindkey -k k5 hardstatus alwayslastline

# use F9 to turn the status bar on the bottom back on
bindkey -k k6 hardstatus alwaysignore

# the next 2 lines are set up to use F1 and F2 to move 
# one screen forward or backward (respectively) through your screen session.
bindkey -k k1 prev
bindkey -k k2 next
This demonstrates how to use Cntrl-f as the Leader key, rather than Cntrl-a.

<PREV   NEXT>