Read the session list and juggle sessions
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
nameif 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.
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.
Here is what just happened.
list-sessions reads the server’s list, so it works from any shell. new-session with a program argument ran top where a shell would have been, and -A turned a would-be duplicate into an attach. Detaching left top running; only q inside it, or a kill, would end it.
- List sessions with their window counts and whether they are attachedtmux ls
- Run a program in the first window instead of a shell (one argument goes through the shell)tmux new -s name -n name command
- Attach to the named session if it exists, otherwise create ittmux new -A -s name
Go further
new -Dis-Aplus detaching any other client on that session.tmux ls -F '#{session_name}'prints just the names, handy in scripts.
- 1Do not press
qinsidetop. That ends the program and closes thewatchsession with it; detach withC-b dinstead. - 2
tmux lsprints one line per session with its window count. Attach to the one that says3 windowswithtmux attach -t name. - 3Create the watcher with
tmux new -s watch -n stats top, detach, then return withtmux new -A -s name.
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
Alpine Linux and tmux 3.4 on an x86 emulator. Nothing to install, and no server behind it.
about 15 MB, once
Done when
not started- The learner's client is attached to the session with three windows— not done yet
- Session watch has a window named stats running top— not done yet
- Session watch is detached— not done yet
- The learner's client is attached to build again, via new -A— not done yet
- The server has exactly four sessions: no duplicate was created— not done yet