Skip to main content

Overview

A session in tmux is a single collection of pseudo terminals under the management of the tmux server. Sessions are the highest-level organizational unit in tmux and provide the foundation for tmux’s persistence model.
Each session is persistent and will survive accidental disconnection (such as SSH connection timeout) or intentional detaching.

Client-Server Architecture

Tmux operates using a client-server model where:
  • Server: A single server process manages all sessions
  • Clients: Multiple clients can connect to the server and attach to sessions
  • Socket: The server and clients communicate through a socket in /tmp
Any number of tmux instances may connect to the same session, and any number of windows may be present in the same session. Once all sessions are killed, tmux exits.

Session Lifecycle

Creating Sessions

From session.c:109-158, when a session is created:
  1. A unique session ID is assigned (prefixed with $)
  2. The session is added to the global sessions tree
  3. Creation time and activity time are recorded
  4. A session working directory is set

Session Identification

Sessions can be identified by:
  • Session ID: Prefixed with $ (e.g., $0, $1)
  • Session name: Exact name or glob pattern
  • Prefix match: Partial name matching the start of a session name
From the man page (tmux.1:753-770):

Detaching and Attaching

The key feature of sessions is their persistence:
From session.c:196-230, when a session is destroyed:
  1. All windows are unlinked from the session
  2. The session is removed from the global sessions tree
  3. A session-closed notification is sent
  4. Resources are freed when reference count reaches zero

Session Groups

Sessions can be grouped together to share windows. From the man page (tmux.1:1344-1370):
Sessions in the same group share the same set of windows - new windows are linked to all sessions in the group and any windows closed are removed from all sessions.
The current and previous window and any session options remain independent, and any session in a group may be killed without affecting the others.

Session Management

From session.c, key session operations include:

Switching Windows

Key bindings for window navigation:
  • Prefix n - Next window
  • Prefix p - Previous window
  • Prefix l - Last (previously selected) window
  • Prefix 0-9 - Select window by number

Activity Tracking

From session.c:266-292, sessions track activity time for lock-after-time functionality:

Session Options

Sessions have their own option hierarchy that inherits from global options:

Renaming Sessions

From session.c:232-248, session names are sanitized:
Session names cannot contain : or . characters - they are automatically replaced with _.

Best Practices

  1. Name your sessions: Use descriptive names for easier management
  2. Use session groups: For pair programming or shared environments
  3. Leverage persistence: Detach instead of closing terminal windows
  4. Session scripts: Automate session creation with shell scripts
  5. Monitor activity: Use list-sessions to see recent activity times