Skip navigation

I’ve been a big user of ‘screen’ for a couple of decades. Yes, I’m old and have been using some form or another of unix for over 20 years. Aside from the fact that means I’m becoming older than dirt, it also means I’m also set in my ways. I find methods to do things and, over time, customize those methods until they become like an old pair of shoes or leather jacket. “Why should I change? The way I’m doing things works really well.” And that’s what I thought of ‘screen’. For those who don’t know, screen’s a way to take a single terminal window and be able to have multiple sessions going on inside that single window. You could detach it on one computer, then re-attach on another computer without losing any of the session you were working on. Back in the days of dial-up, it was crucial. It was crucial for the occasional maintenance task that “Absolutely Positively Cannot Be Interrupted At All” to help mitigate me being disconnected from whatever I was working on (typically a long-running compile or software install). Eventually I started using it in my every-day workflow to interrupt tasks in medias res to be picked up later when someone wasn’t telling me how they’d broken something or other. It stood me in good stead for 20+ years through many, many jobs. Recently, though, I’ve migrated away from ‘screen’ to a newer alternative: ‘tmux’. It fills the same role as ‘screen’ by allowing mutiplexing sessions in a single window, but improves on ‘screen’ in a few significant ways:

  • Much better handling of the changing of screen size. ‘tmux’ dynamically adjusts when any of the attached terminals looking at the particular session resizes. It will choose the size of the smallest attached terminal. Also, the size of any individual multiplexed session is independent of the size of any other session, so if you’ve got multiple terminals of different sizes attached to the same overall tmux server, they can each be different sizes and it won’t affect any other window. Very handy.
  • Easier splitting of windows. ‘screen’ would let you split a window horizontally, so you had one up top and one down below. I believe there was a way to adjust the sizing, but it was a pain and I didn’t use it much. With tmux, you can split a session vertically as well as horizontally. I typically like the left half of my window as a tall editing pane, then the right half split between two panes for entering commands, reading man pages, or editing secondary files. With ‘screen’, I’m not sure if that’s possible. With ‘tmux’, it’s trivial.
  • Scriptability. ‘tmux’ allows you to execute arbitrary commands read in from scripts. I have a script that allows me to set up a new window with my default pane layout. Any time I create a new pane, it’s already set up for me without any additional work on my part.
  • Mouse interaction. When I first started using ‘tmux’, I found out you could select windows using a mouse. It was really cool, but interfered with the select and copy inside iTerm 2, so while it was a selling point, it’s something I’ve stopped using.

Modifications I’ve done…

  • I’ll say that old habits die hard. The default bind key in ‘tmux’ is Control-b, but I’ve switched it to Control-a which is the default in ‘screen’. Two decades worth of keystrokes is hard to overcome. In fact, the first part of my .tmux.conf file is adding in some ‘screen’-like key binds.
  • ‘tmux’ by default doesn’t let you easily attach multiple windows to the same server like screen does with ‘screen -a’. I’ve got an alias of ‘ta’ set to ‘tmux new -t 0’ which will re-attach itself to a currently existing server. It’s not as smart as screen in that it’ll create a new server if one’s not currently existing, but I know to simply use ‘tmux’ after restarting my box.
  • Using the scripting infrastructure, I’m able to use a script to open an arbitrary number of panes ssh’ing to different hosts and have the keystrokes synchronized so what I type goes to all of the hosts. I found a script named ‘ssh-multi’ by a D.Kovalov that I’ve renamed ‘mwin‘ (for ‘multiple windows’) and been using during maintenances to log in and execute the same commands on all affected hosts instead of having to retype them on each individual box.
  • Useful pattern to use with ‘mwin’: SLEEPTIME=$(expr $(hostname | sed -e 's/non.*numeric.*hostname.*bits//') * 30); sleep $SLEEPTIME; sudo /etc/init.d/java_thingy restart . . . You can replace the hostname bit with ifconfig or some such that’ll get you a number that’s unique to that host.

If you’re a long-time ‘screen’ user, spend a few days playing with ‘tmux’. I think you’ll find it’s worth the time investment.