A recent freshmeat article introduced me to the wonderful world of GNU screen, a console-management program. It's built in to Mac OS X, and is a great benefit to users of the Terminal.
Type 'screen' in your Terminal to try it out now; read on for hints on using and configuring it.
[Editor's note: Although we ran a great hint on screen a while back, now that it's included in OS X (as of 10.2, I think), I felt it worth another mention, and this hint has some additional tricks up its sleeve for screen.]
Screen has many features; I mostly use two:
1. Detach from a session, and reattach later
Suppose you're in a console, with Terminal.app or remotely via SSH. You're doing something long and involved, like compiling a Fink project, or editing a config file. Suddenly you need to close your SSH connection, or log out to let someone else use the computer. Will you have to stop what you're doing and lose your place? Not with screen. Just close the Terminal window (or type Ctrl-A, Ctrl-D) to Detach from the session, leaving your work running in the background. Later, type screen -r to reattach to your session, and pick up right where you left off!
2. Multiple 'virtual' consoles in one terminal window
Typing Ctrl-A, Ctrl-C while in screen will Create a new console session, while leaving your existing one intact. Then press Ctrl-A, Ctrl-A to flip back and forth. Although you can do this by just making a new window in Terminal.app, I find it useful when I'm connected via SSH to my machine, since it can be annoying to open a whole new SSH connection (e.g. when connecting from a web browser with the Java SSH terminal).
Some general screen hints and helpers
Using screen as your shell
Being able to Detach from work in progress is wonderful, but doesn't do much good if you have to remember to explicitly start screen before you start working. So I've configured my shell to start screen automatically when I log in, and to remind me if I have any detached sessions waiting.
1. Customize screen
By default, screen detaches and saves your session anytime you quit Terminal without typing 'exit'. I do this all the time, so I found it annoying to have all the old sessions piling up. To fix this, create a file named .screenrc in your home directory, with the following contents:
autodetach offThe second line turns off the annoying welcome message that appears when you start screen.
startup_message off
exec screenTo receive a helpful message reminding you of any detached sessions, add the following to your .cshrc file:
# ---------------- screen setup ----------This also adds an alias, so you can type reattach to reattach to one of your detached sessions.
alias reattach "screen -r"
if ( "$TERM" == "screen" ) then
if (!~ $?SHOWED_SCREEN_MESSAGE ) then
set detached_screens=`screen -list | grep Detached`
if ( "$detached_screens" != "" ) then
echo "+-------------------------------------+"
echo "| Detached screens are available: |"
echo "$detached_screens"
echo "+-------------------------------------+"
else
echo "[ screen is activated ]"
endif
setenv SHOWED_SCREEN_MESSAGE true
endif
endif
# ----------------------------------------
exec screenJust as with .login for tcsh users.
# Login greeting ------------------As always, man screen will reveal many, many more details about using screen.
if [ "$TERM" = "screen" -a ! "$SHOWED_SCREEN_MESSAGE" = "true" ]; then
detached_screens=`screen -list | grep Detached`
if [ ! -z "$detached_screens" ]; then
echo "+---------------------------------------+"
echo "| Detached screens are available: |"
echo "$detached_screens"
echo "+---------------------------------------+"
else
echo "[ screen is activated ]"
fi
export SHOWED_SCREEN_MESSAGE="true"
fi
Mac OS X Hints
http://hints.macworld.com/article.php?story=20021114055617124