Tmux Terminal Sessions: Tips & Tricks
To get the most out of tmux, prioritize prefix-key mastery, session management, and customization—these three areas solve 90% of daily friction. This guide covers essential tips for navigating, organizing, and scripting tmux, plus answers to common questions.
Why Use tmux?
tmux is a terminal multiplexer: it lets you run multiple terminal sessions inside a single window, detach from them, and reattach later. This is invaluable for remote work (SSH), long-running processes, and organizing complex projects. Unlike plain terminal tabs, tmux keeps your sessions alive even if you close the terminal or lose connection.
Getting Started: The Prefix Key
Every tmux command starts with a prefix—by default Ctrl-b. You press the prefix, release it, then press the command key. For example, Ctrl-b c creates a new window. The prefix is your “shift key” for tmux. Many users remap it to Ctrl-a (like GNU screen) to avoid reaching for the b key. To change it, add to your ~/.tmux.conf:
set -g prefix C-a
unbind C-b
bind C-a send-prefix
Session Management: Detach and Reattach
The killer feature is detaching. Start a long process, press Ctrl-b d, and you’re back at your shell—the process keeps running. To reattach later, use tmux attach. List sessions with tmux ls. Name sessions when creating them: tmux new -s work. This makes it easy to switch between projects: tmux switch -t work. Inside a session, Ctrl-b s shows an interactive session list.
Windows and Panes: Organize Your Workspace
Windows are like browser tabs; panes split a window into regions. Common commands:
Ctrl-b c– new windowCtrl-b n/p– next/previous windowCtrl-b %– split pane verticallyCtrl-b "– split pane horizontallyCtrl-b arrow– move between panesCtrl-b x– close pane
For a full list, press Ctrl-b ?. To resize panes, hold Ctrl-b and press arrow keys (or use Ctrl-b Ctrl-arrow).
Copy Mode and Scrolling
By default, the mouse wheel scrolls through the scrollback buffer—but only if you enable mouse mode: set -g mouse on. Without it, enter copy mode with Ctrl-b [, navigate with arrow keys, and copy with Space to start selection, Enter to copy. Paste with Ctrl-b ]. This is essential for working with output that exceeds the screen.
Customization: Make tmux Yours
Your ~/.tmux.conf is the heart of customization. Popular tweaks:
- Change the status bar:
set -g status-bg blue - Enable mouse support:
set -g mouse on - Set a larger history limit:
set -g history-limit 5000 - Rebind the prefix to
Ctrl-a(as above)
After editing, reload with tmux source-file ~/.tmux.conf.
Scripting and Automation
tmux is scriptable. You can create a session with multiple windows and panes in one command:
tmux new-session -d -s dev -n code
tmux send-keys -t dev 'vim' C-m
tmux split-window -h -t dev
tmux send-keys -t dev 'git status' C-m
tmux attach -t dev
This is great for setting up a project environment. You can also use tmux new-session -A -s dev to attach to an existing session or create it if it doesn’t exist—perfect for shell aliases.
Common Pitfalls and Troubleshooting
New users often forget the prefix and type commands directly—remember, every command starts with Ctrl-b. Another issue: when you reattach, the terminal may not display correctly. Fix with tmux attach -d (detach other clients). If panes are too small, use Ctrl-b z to zoom a pane (toggle). If you lose your session due to a server crash, check tmux ls—if nothing shows, your session is gone. To avoid this, use tmux new -s name and save your work often.
Frequently Asked Questions
How do I exit tmux completely?
Type exit in the shell of the last window, or press Ctrl-b & (kill window). To kill the entire server, use tmux kill-server.
Can I use the mouse with tmux?
Yes, enable it with set -g mouse on in your config. Then you can click to select panes, resize, and scroll. Some users prefer to keep it off for keyboard efficiency.
How do I copy text from tmux to the system clipboard?
In copy mode, select text and press y (if you have a clipboard integration) or use Ctrl-b ] to paste within tmux. To copy to the system clipboard, you may need a tool like xclip and a binding like bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard".
Why does my tmux session die when I close the terminal?
It shouldn’t—tmux is designed to survive. If it dies, you may be using tmux kill-server accidentally, or your system may have a service manager that kills it. Check if you have exit in your shell profile that ends the session.
How do I rename a session or window?
Use Ctrl-b $ to rename the session, and Ctrl-b , to rename the window. You can also do it from the command line: tmux rename-session -t old new.
Is tmux better than screen?
Both are multiplexers, but tmux is more modern, actively maintained, and has a scripting API. Screen is simpler but less flexible. Most new users prefer tmux due to its configuration and status bar.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.