learntmux.dev

Read the session list and juggle sessions

Goal

Back on the three-window session, watch running top detached, four sessions in total.

Two commands do the juggling, both from the plain shell, neither needing you to be attached:

tmux ls
every session on the server with its window count
tmux new -A -s name
attach to name if it exists, create it if not, so it never makes a duplicate

Sessions are independent: each has its own name, window list and current window. Names must be unique on a server, which is why plain new -s name fails when the name is taken — and why -A exists.

A word after new’s flags runs a program in the first window instead of a shell.

Do this

Run tmux ls.

Three sessions are listed. Note the one with 3 windows.

Run tmux attach -t and that name, then press C-b d.

You land on it, then leave it again.

Run tmux new -s watch -n stats top, then press C-b d.

A session opens with top in a window named stats, and you leave it running.

Run tmux new -A -s and the three-window session’s name.

You are back on it, and the server still has four sessions.

How the program argument is parsed

That program argument goes through the shell, so ~ and pipes work inside it.

Stuck on this one?

This is a live terminal. Typing goes to the machine, including Tab. Press Shift and Tab together to move focus back out to the page.

A real terminal, in this tab

about 15 MB, once

session learn

Done when

not started
  1. The learner's client is attached to the session with three windows— not done yet
  2. Session watch has a window named stats running top— not done yet
  3. Session watch is detached— not done yet
  4. The learner's client is attached to build again, via new -A— not done yet
  5. The server has exactly four sessions: no duplicate was created— not done yet