tmux sessions are more than just windows and panes; they’re independent workspaces that can persist even after you close your terminal.
Let’s see a tmux session in action. Imagine you’re working on a project. You start tmux and immediately see a prompt in your terminal. This is your first session, session "0".
$ tmux new-session -s myproject
This command creates a new session named "myproject". You’re now inside it. Your terminal prompt is gone, replaced by a status bar at the bottom of the screen. This is tmux.
You can split your current window into panes. Ctrl+b is the default prefix key. Press Ctrl+b then % to split vertically.
+---+---+
| | |
+---+---+
Press Ctrl+b then " to split horizontally.
+---+
| |
+---+
| |
+---+
You can switch between panes using Ctrl+b and the arrow keys. To create a new window within the same session, press Ctrl+b then c. You’ll see a new status line indicating window "1". You can switch between windows with Ctrl+b and n (next) or p (previous), or by number.
The power of tmux comes from its ability to detach from a session and reattach later. If you press Ctrl+b then d, you detach from the "myproject" session. Your terminal returns to its normal state, but your tmux session, with all its windows and panes, keeps running in the background.
To see all your running sessions, use tmux ls.
$ tmux ls
myproject: 2 windows (created 2023-10-27 10:00:00) [80x24, 1 panes]
another_session: 1 window (created 2023-10-27 09:30:00) [80x24, 1 panes]
To reattach to a session, use tmux attach-session -t <session_name>.
$ tmux attach-session -t myproject
You’re back exactly where you left off. All your windows, panes, and running processes are preserved.
Killing a session is straightforward. You can do it from within the session by closing all its windows. Alternatively, from outside the session, use tmux kill-session -t <session_name>.
$ tmux kill-session -t myproject
If you forget the name of a session, tmux ls will remind you.
The real magic is how tmux manages these detached processes. When you detach, tmux doesn’t pause your applications; it keeps them running in its own virtual terminal environment. This means your code compilation, long-running scripts, or server processes continue uninterrupted, even if you close your laptop or lose your SSH connection. You simply reattach to the session later and pick up exactly where you were, with everything still running.
When you create a new tmux session with tmux new-session -s name, you’re not just starting a new terminal emulator. You’re creating a server process that manages a set of virtual terminals. Each window and pane is a client connected to this server, and when you detach, the server continues to serve those clients, allowing them to run independently of your physical terminal connection.
The most common pitfall is forgetting the prefix key (Ctrl+b by default). If you accidentally type Ctrl+b followed by a character that tmux doesn’t recognize as a command, it will often pass that key combination through to the shell, which can lead to unexpected behavior. Many users rebind the prefix key to something more convenient, like Ctrl+a, which is a common setting in older terminal multiplexers like screen. To do this, you’d edit your ~/.tmux.conf file and add the line set-option -g prefix C-a. After saving the file, you’d need to reload the configuration within tmux by pressing Ctrl+b then : and typing source-file ~/.tmux.conf and pressing Enter.
The next step is understanding tmux’s scripting capabilities and how to automate session creation and management.