tmux doesn’t just let you split windows; it lets you build a fully-fledged, keyboard-driven operating environment that can outpace your mouse-based workflow.
Let’s see it in action. Imagine you’re working on a project with a frontend dev server and a backend API running. You’d normally open two terminal tabs, cd into each directory, and run the commands. With tmux, it’s one command to rule them all:
tmux new-session -d -s 'dev_env' 'cd ~/projects/my-app && npm run dev'
tmux split-window -v -t 'dev_env' 'cd ~/projects/my-api && npm start'
tmux select-pane -t 'dev_env.0'
tmux attach-session -t 'dev_env'
This creates a new tmux session named dev_env, detached so it keeps running in the background. It immediately starts your frontend npm run dev in the first pane. Then, it splits the window vertically (-v) and runs your backend npm start in the second pane. We select the first pane (-t 'dev_env.0') to focus on it, and then attach to the session.
Now, you’re in a single terminal window, but you have two processes running, each in its own pane. To switch between them, you press Ctrl+b (the default tmux prefix) followed by an arrow key: Ctrl+b then Right Arrow to go to the backend, Ctrl+b then Left Arrow to go back to the frontend. To create a new pane, you’d press Ctrl+b then % for a vertical split or Ctrl+b then " for a horizontal split.
The core problem tmux solves is context switching and managing multiple terminal processes efficiently. Instead of juggling separate terminal windows or tabs, you have a unified, persistent workspace. Sessions can be detached and reattached, meaning you can close your laptop, go home, and come back to find all your running processes exactly as you left them. This is invaluable for long-running tasks, remote development, or just keeping your workspace organized.
Internally, tmux operates on sessions, windows, and panes. A session is a collection of windows. A window is a single screen within a session, which can be split into multiple panes. Each pane runs a shell or a command. You navigate and control these elements using keybindings, prefixed by a special key combination (the "prefix key," usually Ctrl+b).
Here are some power user tricks:
1. Sync Panes for Simultaneous Input:
Ever need to type the same command into multiple panes? Ctrl+b then :source-file ~/.tmux.conf
This is a game-changer for configuration management. If you’re editing a script or a config file, you can have it open in multiple panes and make changes simultaneously across all of them. This is incredibly useful when you’re debugging or setting up identical environments.
2. Target Specific Panes and Windows:
You can address specific panes and windows by their index. For example, Ctrl+b then 1 switches to window 1. Ctrl+b then Ctrl+b then Up Arrow will move focus to the pane above the current one. This allows for precise navigation without relying on sequential movement.
3. Shell Integration for Better Copy-Paste:
Modern tmux versions (2.6+) offer shell integration. This means when you select text, it’s automatically copied to tmux’s paste buffer. You can then paste it into another pane using Ctrl+b then ]. To enable this, add these lines to your ~/.tmux.conf:
set -g default-command "reattach-to-user-namespace -l bash" # For macOS
set-option -g set-clipboard on
This automatically copies selected text to the system clipboard, making it seamless to paste into other applications outside of tmux.
4. Tmuxinator/Teamocil for Session Management:
For even more advanced session management, consider tools like tmuxinator or teamocil. These allow you to define your tmux layouts, windows, and panes in YAML files. Instead of typing out multiple commands to set up your environment, you just run tmuxinator start myproject.
# Example ~/.tmuxinator/myproject.yml
name: myproject
root: ~/projects/my-app
windows:
- frontend:
layout: main-vertical
panes:
- npm run dev
- echo "Frontend logs here"
- backend:
panes:
- cd ~/projects/my-api && npm start
Then, running tmuxinator start myproject will instantly create this exact layout and start the commands.
5. Tmux Plugins:
The tmux plugin manager (tpm) allows you to easily install and manage plugins that extend tmux’s functionality. Popular plugins include tmux-sensible (sensible defaults), tmux-resurrect (saves and restores tmux sessions), and tmux-yank (improved copy-paste). Installing tpm is a simple git clone, and then you add plugins to your ~/.tmux.conf like this:
# ~/.tmux.conf
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
run '~/.tmux/plugins/tpm/tpm'
After installing tpm and adding plugins, you’ll typically press Ctrl+b then I to install them. tmux-resurrect is particularly powerful; it saves not just your window layout but also the running commands and their current state. Press Ctrl+b then Ctrl+r to restore a saved session.
When you copy text within tmux using Ctrl+b and [ to enter copy mode, then Space to start selection and Enter to finish, that text is placed into tmux’s "kill ring" (its paste buffer). If you then try to paste this into a different tmux pane using Ctrl+b and ], you’re pasting from this internal kill ring. However, if you want to paste into an application outside of tmux, you need to ensure the text also gets to your system’s clipboard. The set-clipboard on option, combined with the reattach-to-user-namespace for macOS, bridges this gap by synchronizing tmux’s kill ring with the system clipboard.
The next rabbit hole to fall into is scripting tmux commands to automate complex workflow setups.