Ramblings of an aging IT geek
← Ramblings of an aging IT geek
tooling

tmux, and the end of losing a session to a dropped ssh

How a small tmux config and a habit of attaching rather than reconnecting stopped me losing long-running work to flaky links.

A mechanical keyboard in front of a terminal

The trigger was a train. Two hours of patchy tethering, a long migration script running on a remote box, and an ssh session that dropped every time we went through a tunnel. The script kept running, because I'd had the sense to start it under tmux, but the version of me from three years ago would have lost the lot.

If you've not committed to tmux yet, this is the pitch: your shell sessions live on the server, not in your terminal. The link can die, your laptop can sleep, you can close the lid and go home, and the work carries on. When you come back you attach to the session and it's exactly where you left it, scrollback and all.

The whole workflow is two commands. Start or attach a named session:

tmux new -A -s work

The -A is the bit people miss. It means "attach if it exists, otherwise create it", so you can run that same line every time and never think about whether the session is already there.

A split terminal under tmux

After that it's the prefix key and a handful of bindings. I leave the prefix on the default Ctrl-b because I move between too many machines to retrain my fingers per host. The ones I use constantly:

  • prefix " and prefix % to split a pane horizontally and vertically
  • prefix arrow to move between panes
  • prefix d to detach and walk away
  • prefix [ to enter copy mode and actually scroll the buffer

My .tmux.conf is deliberately short. The only opinionated lines:

set -g history-limit 50000
set -g mouse on
set -g base-index 1
setw -g pane-base-index 1

The big scrollback has saved me more than once when a command spewed something useful and then a thousand lines of noise on top of it. Mouse mode I went back and forth on, since purists hate it, but being able to drag a pane border when I'm tired is worth the small loss of dignity.

The habit that actually matters isn't the config, though. It's running everything long under tmux by reflex, before you know whether you'll need it. The migration on the train didn't survive because I made a good decision on the train. It survived because I'd made the boring decision twenty minutes earlier, out of habit, when the connection was still fine.