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.
# 新会话 + 命名,方便后续按名复用
tmux new -s blog-dev
# 分离(不退出,后台保留),快捷键:Ctrl-b 然后 d
# 会话列表
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.
# 新建并命名
tmux new -s web
# 在分离状态下重连
tmux attach -t web
# 不切换也往某会话发送命令
tmux send-keys -t web "tail -f /var/log/app.log" Enter
# 脚本探测:没有则创建,有则复用
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 # 快速均分当前窗格
bind-key s swap-pane -U # 上下交换当前窗格
# ~/.tmux.conf 加载后: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.
# 后台启动一个常驻服务会话并分离
tmux new -s svc -d "cd /app && ./server 2>&1 | tee /tmp/server.log"
# 稍后从任意终端复用查看
tmux attach -t svc
# 查看会话及其窗口状态
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" # 在会话内执行后 Ctrl-b d 离开
# 关掉 SSH,重连后仍在执行
tmux attach -t test
# ~/.tmux.conf
set -g mouse on