Why tmux Is a Lifeline for Remote Development
A terminal process belongs to the SSH session that created it; when the connection drops, foreground tasks (a download, a compile, npm run dev) receive a hang-up signal and die. tmux is a terminal multiplexer: you start a persistent session on the server, and SSH merely attaches to it, so the session stays alive in the background when the connection breaks.
You can lose your network, close your laptop or switch Wi-Fi, then reattach later to see everything exactly as you left it. Once you are used to it, it becomes the standard way to work on remote servers, debug across machines, and run long tasks.
# New session + named, easy to reuse later by name
tmux new -s blog-dev
# Detach (keep it running in the background), keys: Ctrl-b then d
# List sessions
tmux lsNamed Sessions: One Command to Reconnect to the Right Task
Giving each complex task its own named session is the key to organizing remote work. Keep build tasks, database debugging and log tailing in separate sessions instead of piling everything into an unnamed default — otherwise after a while you cannot tell which is which.
Re-enter with tmux attach -t name. To send a command into a window without switching to that session, use send-keys -t. Sessions can be probed for existence with has-session, letting you write a script that "creates if missing, attaches if present" as your standard entry point.
# Create and name
tmux new -s web
# Reconnect to a detached session
tmux attach -t web
# Send commands to a session without switching
tmux send-keys -t web "tail -f /var/log/app.log" Enter
# Script check: create if missing, reuse if present
tmux has-session -t dev 2>/dev/null || tmux new -s dev -d; tmux attach -t devWindows and Panes: Scheduler, Logs and Editor in One Session
One session can contain multiple windows, and each window can be split into several panes. A common layout: one pane runs the app, one tails logs live, and one opens vim for code. That collapses the edit-run-watch loop onto a single screen without constant tab flipping.
Common commands: Ctrl-b c opens a window, Ctrl-b n/p cycles, Ctrl-b % splits vertically, Ctrl-b " splits horizontally, and Ctrl-b arrow keys move between panes. Reset layouts with select-layout (e.g. even-horizontal), or cycle defaults with Ctrl-b space.
For convenience, turn common layouts into bindings in ~/.tmux.conf — remap a key to a select-layout so one keystroke settles the pane geometry.
bind-key ' select-layout even-horizontal # Quickly split panes evenly
bind-key s swap-pane -U # Swap the current pane up/down
# Reload ~/.tmux.conf after editing: source-file ~/.tmux.confPersistent Processes and Recovery: Run Services Inside tmux
Many ops just drop a service onto the SSH foreground, so it dies on every disconnect. The right way is to run the command inside tmux: create a session detached, start the command inside it, then detach — the service lives as a member of a tmux session permanently, independent of whether anyone is on SSH.
Combined with boot auto-start or systemd, this solidifies into "auto-lift services after reboot". To keep processes from being reaped when nobody is attached, give the session a name and avoid kill-session, and confirm tmux’s detach behavior (default detach does not end the session). To recover, run tmux ls to check if the session is alive; if so attach directly, otherwise re-run the start command inside the session.
# Start a persistent service session in the background and detach
tmux new -s svc -d "cd /app && ./server 2>&1 | tee /tmp/server.log"
# Later, reattach from any terminal to check
tmux attach -t svc
# View sessions and their window state
tmux lsVerify the Reconnect and Copy-Paste Pipeline
Actually verify after configuring: open a named session, run a long command (say sleep or tail), then Ctrl-b d to detach and close SSH entirely. Reconnect and run tmux attach -t session — the long command should still be running, not interrupted.
Copy-and-paste trips many people: by default scroll-wheel enters browse mode, and copying requires holding Shift or entering copy-mode. To enable mouse support, set -g mouse on in ~/.tmux.conf, which lets you select-to-copy and scroll history naturally, making text transport easy even outside a terminal emulator.
tmux new -s test
echo "start"; sleep 60; echo "done" # Run it inside the session, then press Ctrl-b d to leave
# Close SSH and reconnect; it is still running
tmux attach -t test
# ~/.tmux.conf
set -g mouse on