Session Lifecycle
A session is the top-level container in tmux. Mastering session management means you always know where your work is and can get back to it in seconds.
After this lesson you can navigate between multiple sessions confidently and clean up dead sessions without disrupting active ones.
Creating Sessions
Basic (unnamed)
tmux
Creates an unnamed session. Works but hard to manage once you have many.
Named (recommended)
tmux new-session -s work
# or shorter
tmux new -s work
Named with Initial Window Name
tmux new -s work -n editor
Named, Detached (start in background)
tmux new -s build -d
Useful for scripts that start sessions programmatically.
Named, Starting a Specific Command
tmux new -s logs -d "tail -f /var/log/syslog"
Listing Sessions
tmux ls
# or
tmux list-sessions
Output:
build: 1 windows (created Mon Apr 7 04:00:10 2026) [220x50]
logs: 1 windows (created Mon Apr 7 04:00:15 2026) [220x50] (attached)
work: 2 windows (created Mon Apr 7 04:00:00 2026) [220x50]
The (attached) tag shows which session your current terminal has attached.
Attaching to Sessions
# attach to a specific session
tmux attach -t work
# or
tmux a -t work
# attach to the most recent session
tmux attach
Attach and Switch Clients
If someone else is attached to a session, you can detach them and take over:
tmux attach -t work -d
Using -d disconnects any other client currently attached to that session.
Detaching
From Inside a Session
| Default Shortcut | Modification Shortcut | Action |
|---|---|---|
Ctrl+b d | Ctrl+Space d | Detach from session |
Detaching keeps the session and all its contents alive.
From the Command Line
tmux detach-client -t work
Renaming Sessions
| Default Shortcut | Modification Shortcut | Action |
|---|---|---|
Ctrl+b $ | Prefix e s | Rename current session |
From the command line:
tmux rename-session -t oldname newname
Switching Between Sessions
| Default Shortcut | Modification Shortcut | Action |
|---|---|---|
Ctrl+b s | Ctrl+Space s | Open interactive session list |
Ctrl+b ( | Alt+Home | Switch to previous session |
Ctrl+b ) | Alt+End | Switch to next session |
Killing Sessions
# kill a specific session
tmux kill-session -t work
# kill ALL sessions (shuts down tmux server)
tmux kill-server
kill-server destroys every session and every process running inside tmux. Use with caution.
Full Session Lifecycle Diagram
Session Management Cheatsheet
| Action | Command | Default Shortcut | Modification Shortcut |
|---|---|---|---|
| New session | tmux new -s name | — | Ctrl+Space N |
| New detached session | tmux new -s name -d | — | — |
| List sessions | tmux ls | Ctrl+b s | Ctrl+Space s |
| Attach to session | tmux attach -t name | — | — |
| Detach | — | Ctrl+b d | Ctrl+Space d |
| Rename session | tmux rename-session -t old new | Ctrl+b $ | Prefix e s |
| Switch sessions | — | Ctrl+b s / Ctrl+b ( / ) | Ctrl+Space s / Alt+Home / Alt+End |
| Kill session | tmux kill-session -t name | — | Ctrl+Space k |
| Kill server | tmux kill-server | — | — |
Shell Aliases
Three shell functions loaded from ~/.bash_aliases/TMUX/tmux.sh:
| Default Command | Shell Alias | Description |
|---|---|---|
tmux attach -t <name> | ta <name> | Attach to a named session (or switch-client inside tmux) |
tmux ls | tl | List all sessions |
tmux new -s <name> -d + tmux attach -t <name> | tns <name> | Create new session and attach |
.bashrc sources ~/.bash_aliases_loader, which recursively loads all *.sh files under ~/.bash_aliases/. Add new alias files to any subdirectory — they load automatically.
# ~/.bash_aliases/TMUX/tmux.sh
ta() {
if [ -z "$1" ]; then echo -e "Usage: ta <session-name>"; return 1; fi
if [ -n "$TMUX" ]; then tmux switch-client -t "$1";
else tmux attach -t "$1"; fi
}
tl() { tmux ls "$@"; }
tns() {
if [ -z "$1" ]; then echo -e "Usage: tns <session-name>"; return 1; fi
tmux new-session -d -s "$1"
if [ -n "$TMUX" ]; then tmux switch-client -t "$1";
else tmux attach -t "$1"; fi
}
Common Pitfalls
| Pitfall | What happens | Fix |
|---|---|---|
| Not naming sessions | Confusing list of numbers | Always use -s name |
Attaching without checking ls first | Overwrites wrong session | Run tmux ls before attaching |
Running kill-server instead of kill-session | All sessions destroyed | Be explicit: use kill-session -t name |
Hands-On Practice
# create multiple sessions
tmux new -s work -d
tmux new -s logs -d
tmux new -s monitoring -d
# list them
tmux ls
# attach to work
tmux attach -t work
# switch to logs: Ctrl+b s, select logs
# detach: Ctrl+b d
# clean up
tmux kill-session -t logs
tmux kill-session -t monitoring
tmux kill-session -t work