The most surprising thing about tmux is that it doesn’t actually manage your terminal sessions; it creates new, independent virtual terminals that happen to be displayed within your existing terminal emulator.
Let’s see it in action. First, start tmux:
$ tmux
You’ll notice your prompt changes slightly, and a green status bar appears at the bottom. This is tmux telling you it’s running. Now, let’s create a new "window" (which is like a tab in a browser). Press Ctrl+b (this is the default "prefix" key, meaning tmux listens for commands after you press this), then c for "create".
(Press Ctrl+b, then c)
You’re now in a new, blank shell session, but you’re still inside tmux. Notice the status bar at the bottom now shows 0:bash* and 1:bash. The * indicates your current window. To switch back to the first window, press Ctrl+b, then 0. To switch to the second, Ctrl+b, then 1.
Now, let’s split the current window into panes. With window 0 active, press Ctrl+b, then % to split vertically.
(Press Ctrl+b, then %)
You now have two panes, one on top of the other. The active pane will have a brighter border. To move between panes, press Ctrl+b, then the arrow key corresponding to the direction of the pane you want to activate (e.g., Ctrl+b then Up Arrow to move to the pane above).
Let’s split the current pane horizontally. Press Ctrl+b, then ".
(Press Ctrl+b, then ")
You now have three panes. You can resize panes too. Press Ctrl+b, then Ctrl+Arrow Key to resize the active pane. For example, Ctrl+b then Ctrl+Up Arrow will make the top pane taller.
The problem tmux solves is managing multiple, long-running processes or interactive sessions on a single terminal, especially when dealing with remote servers or limited screen real estate. You can detach from a tmux session, leaving your processes running, and reattach later from anywhere. This is invaluable for remote development, sysadmin tasks, or even just organizing your local workflow.
Inside tmux, a "session" is a collection of windows. A "window" is a single screen, like a tab, which can be split into multiple "panes." Each pane runs its own shell or command.
Here’s how you detach and reattach. First, make sure you have at least one window with a process running (e.g., ping google.com in one pane). Then, press Ctrl+b, then d for "detach."
(Press Ctrl+b, then d)
Your original shell prompt reappears, but the tmux session and its processes are still running in the background. To see your running sessions, type:
$ tmux ls
You’ll see output like 0: 2 windows (created Mon Feb 20 10:30:00 2023). To reattach to this session, use:
$ tmux attach -t 0
Or, if it’s your only session, simply:
$ tmux attach
The real power comes from scripting and automation. You can create complex layouts and start specific applications in each pane automatically when a session starts. For example, to start a new session named myproject with a specific layout:
$ tmux new-session -s myproject \; \
split-window -h \; \
select-pane -t 0 \; \
split-window -v \; \
select-pane -t 2 \; \
split-window -v \; \
send-keys 'git status' C-m \; \
select-pane -t 0 \; \
send-keys 'docker-compose up' C-m \; \
select-pane -t 1 \; \
send-keys 'tail -f logs/app.log' C-m \; \
select-pane -t 2 \; \
send-keys 'vim src/main.py' C-m
This command creates a session, splits it into three panes, and then sends specific commands to each pane (git status, docker-compose up, tail -f logs/app.log, and vim src/main.py). The C-m is tmux’s representation of the Enter key.
What most people don’t realize is that tmux’s keybindings are highly customizable. You can change the prefix key, the commands for creating windows/panes, and even create entirely new keybindings to automate complex multi-pane operations. For instance, to change the prefix key from Ctrl+b to Ctrl+a, you’d add this to your ~/.tmux.conf file:
# Change the prefix key to Ctrl+a
set-option -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix
After saving this file, you’d reload the configuration by pressing Ctrl+a (your new prefix), then :, then typing source-file ~/.tmux.conf and pressing Enter.
The next concept to explore is tmux’s scripting capabilities and how to manage multiple tmux sessions simultaneously for different projects.