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
Session Lifecycle
Creating Sessions
Fromsession.c:109-158, when a session is created:
- A unique session ID is assigned (prefixed with
$) - The session is added to the global sessions tree
- Creation time and activity time are recorded
- 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
Detaching and Attaching
The key feature of sessions is their persistence:session.c:196-230, when a session is destroyed:
- All windows are unlinked from the session
- The session is removed from the global sessions tree
- A
session-closednotification is sent - 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): 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
Fromsession.c, key session operations include:
Switching Windows
Prefix n- Next windowPrefix p- Previous windowPrefix l- Last (previously selected) windowPrefix 0-9- Select window by number
Activity Tracking
Fromsession.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
Fromsession.c:232-248, session names are sanitized:
Session names cannot contain
: or . characters - they are automatically replaced with _.Best Practices
- Name your sessions: Use descriptive names for easier management
- Use session groups: For pair programming or shared environments
- Leverage persistence: Detach instead of closing terminal windows
- Session scripts: Automate session creation with shell scripts
- Monitor activity: Use
list-sessionsto see recent activity times