100 Useful Command-Line Utilities
by Oliver; 201465. 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:
$ screenLet's see an example of what this looks like:
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
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 -lsAnother figure:
To reattach—i.e., enter into—screen session of id 1167:
$ screen -r 1167To kill a screen session of id 1167:
$ screen -X -S 1167 quitOnce 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> c | Create new window (shell) |
<Leader> k | Kill the current window |
<Leader> n | Go to the next window |
<Leader> p | Go to the previous window |
<Leader> <Leader> | Toggle between the current and previous window |
<Leader> d | Detach the screen session |
<Leader> A | Set window's title |
<Leader> w | List all windows |
<Leader> 0-9 | Go to a window numbered 0-9 |
<Leader> [ | Start copy mode |
<Leader> Esc | Start 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 nextThis demonstrates how to use Cntrl-f as the Leader key, rather than Cntrl-a.