GNU Screen is a "window" manager for the terminal. Similar in concept to tabbed browsing in Firefox, but without the GUI, all command-line. It allows you to have multiple terminal sessions open while only taking up one on your desktop.
The screen utility also allows you to detach and leave the session running in the background. So you can exit the terminal; log back in; and reattach exactly how you left it, running processes and all. Great for remote server work, or if you accidentally disconnect or close the wrong window.
Basics
The GNU screen command is simply: $ screen
Once you're in a screen, you can issue commands using ctrl-a (default), or you can map another key. Emacs and bash use ctrl-a, so you may want to map the escape key to a different key. To map to Ctrl-w use escape "^Ww" (see .screenrc below)
Screen Commands
| ctrl-a ctrl-c | Create New Window |
| ctrl-a ctrl-a | Cycle Window |
| ctrl-a ctrl-n | Next Window |
| ctrl-a ctrl-p | Previous Window |
| ctrl-a 0-9 | Switch to Window # |
| ctrl-a d | Detach from current screen (it's still there) |
Detach and Reattach
You can detach from a screen session using ctrl-a d ; All your screens will still exist, with all running processes. You can even close your terminal sesssion, ie. disconnect if you were logged in remotely.
You can reattach using: $ screen -r
After reattaching, everything will be as you left it. All screens available and running processes. See the screen man page for multiple options on reattaching, I find using -RR works best.
Navigating and Scrolling
GNU Screen is its own window manager and operates outside of the system windowing, which makes scrolling and using the mouse a bit more difficult. First make sure you define a good chunk of lines to scrollback in your screenrc. Set using defscrollback 5000
Next to enter scrollback mode use ctrl-a [ and then to control scrolling it is standard vim commands.
h,j,k,l - cursor movement
ctrl-b - back a page
ctrl-f - forward a page
/ - search forward
? - search backward
Starting Screen Automatically
I have the following set up to start gnu sreen automatically when I log in to my server. You should place it in your shell startup (.bashrc or .bash_profile), this checks to make sure your not running screen and then starts it. When you detach from screen using ctrl-a d you will also disconnect if logged in to a remote server.
if [ "$TERM" != "screen" ]; then
exec /usr/bin/screen -d -RR -A
fi
Using Multiple Windows
My typical usage is to have multiple full screen remote windows open using screen, for example one used as a file browser and run commands, another for vim open and editing, another tailing logs, etc... I then switch between the screens using ctrl-a ctrl-a which is really just holding down ctrl and hitting "a" twice this walks through the screens.
You can map keys to navigate between the different screens, here's how to set the key bindings:
# Bind F5 and F6 to previous and next screen window
bindkey -k F5 prev
bindkey -k F6 next
Using Titles for Your Windows
I setup my .screenrc file to display all open windows in the bottom status bar, see my config below. So using this I can set names for each of my windows to navigate through. To set a title use ctrl-a : which enters command mode and then type title YourWindowTitle
Split Windows
Also, using gnu screen you can split the screen up into different panels, which is not really part of my workflow, since I do in vim, but an incredibly useful feature. Here's how to split windows in screen and navigate between:
| ctrl-a S | Split screen horizontally |
| ctrl-a | | Split screen vertically |
| ctrl-a tab | Navigate between splits |
| ctrl-a X | Remove current split |
| ctrl-a Q | Remove all other splits |
| ctrl-a :resize | Resize Screen (prompts for lines) |
Collaborative Coding - Window Sharing
You can use GNU screen to share interactive sessions, allowing people to see and type in the same shared terminal. This could be a nice way to do pair-programming remotely or walk through a command-line demo.
Try the following:
- Start up to two terminal sessions, A and B, via ssh, telnet or even local.
- In Terminal-A, start up screen:
$ screen - In Terminal-B, connect to screen already established:
$ screen -x - Now whatever you type in either terminal will show up on both. They are actual the same screen shared.
This sharing can be done a few different ways with multiple people. At work, since most of us have admin rights, we simply "su" to the other person and connect to the screen as them.
Another way is to set the permissions on your pts/tty session, location is usually in /dev somewhere depending on your unix environment. Or use $ mesg y to set your tty session as writable.
My screenrc config
# screenrc
startup_message off
escape "^Aa"
defscrollback 5000
# add status lines
caption always "%{= bb}%{+b w}Screen: %n | %h %=%t %c"
hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"
TMUX - Terminal Multiplexer
There is a new improved window manager in town, I've been trying out TMUX as a replacement for screen. It is a bit more refined and easier to configure and use, but I've run into a few issues so far. Plus not a default in most distributions so may not be everywhere you login to. See my notes on TMUX.
Additional Resources
- The Antidesktop - another persons' work flow, including his screen configuration
- The Power of Screen [Mac OS X Hints article]
- If not installed and you need to download screen GNU Screen Download [mirror]
- Xterm Color Chart - for customizing xterm and terminal
Pingback: Tmux – Terminal Multiplexer – Solutions Log - Marcus Kazmierczak
Pingback: Ubuntu Guide for Mac Converts – Linux - solutions log