Sessions: detach, reattach, never lose work

Sessions are the reason tmux exists. Learn these four commands and you already get most of tmux's value — everything else is convenience.

The core loop

Create a named session, work, detach, come back later. That's the loop you'll run for the rest of your career:

shell
tmux new -s work     # create + attach
# ... do things ...
# Ctrl-b d           # detach: everything keeps running
tmux ls              # see what's running
tmux attach -t work  # pick up exactly where you left off

The SSH story (why people fall in love)

SSH into a server, start tmux, kick off a 6-hour job, close your laptop, drive home, SSH back in, tmux attach — the job never noticed. Any long-running remote work that isn't inside tmux (or similar) is one flaky connection away from dying.

One habit worth stealing

tmux new -As main attaches to "main" if it exists, creates it otherwise. Make it a shell alias and every terminal you ever open lands in the same persistent workspace:

~/.zshrc
alias t='tmux new -As main'

Multiple sessions

One session per project keeps contexts clean. prefix s opens an interactive session tree — switch without detaching. prefix ( and ) cycle, prefix L toggles the last two, and prefix $ renames the current one.

Before moving on

  1. tmux new -s practice
  2. Detach with Ctrl-b d
  3. Run tmux ls and find it
  4. Reattach with tmux attach -t practice
  5. Bonus: create a second session and switch with Ctrl-b s