By Tux Harlow — Solnyxus editorial voice.
When an SSH connection drops, the shell on the other end usually goes with it, along with whatever was running in the foreground. tmux sessions solve this by running your shell inside a server process that does not care whether you are connected. This post covers the commands you need to start using tmux today.
Starting and naming tmux sessions
tmux new -s work
This starts a new session named work. Naming sessions makes them easier to find later.
tmux ls
This lists the sessions running on the machine.
tmux attach -t work
This attaches to the session named work.
tmux new -A -s work
This attaches to work if it exists and creates it if it does not, which is a convenient single command to remember.
The prefix key and detaching
Every tmux shortcut starts with the prefix key, which is Ctrl-b by default. You press the prefix, release it, then press the command key.
To detach, press Ctrl-b then d. You return to your normal shell and the session keeps running. If your connection drops instead, tmux behaves the same way: the session continues and you can reattach.
Windows and panes
A session contains windows, and each window can be split into panes.
Ctrl-b ccreates a new window.Ctrl-b nandCtrl-b pmove to the next and previous window.Ctrl-b %splits the current pane left and right.Ctrl-b "splits the current pane top and bottom.Ctrl-bfollowed by an arrow key moves between panes.Ctrl-b zzooms the current pane to fill the window, and pressing it again restores the layout.
Scrolling back
Because tmux draws its own screen, your terminal's scrollbar does not see its history. Press Ctrl-b [ to enter copy mode, then use the arrow keys or Page Up to scroll. Press q to leave copy mode.
The default history limit is 2000 lines, which runs out quickly when following logs.
A small tmux.conf
tmux reads ~/.tmux.conf, and since tmux 3.1 it also looks for ~/.config/tmux/tmux.conf. A short configuration helps:
set -g mouse on
set -g history-limit 50000
set -g base-index 1
The first line enables the mouse for selecting panes, resizing them and scrolling. The second raises the scrollback to 50,000 lines. The third numbers windows from 1, so they match the number row on the keyboard.
To reload the configuration without restarting:
tmux source-file ~/.tmux.conf
Ending sessions
tmux kill-session -t work
This ends the session named work and everything running in it. Typing exit in the last pane of the last window has the same effect.
A note on logout
Some systems are configured to kill a user's processes when they log out, through systemd-logind's KillUserProcesses=yes setting in logind.conf. If your tmux sessions disappear when you log out, check that setting. Most distributions ship with it set to no.
Conclusion
tmux is a small tool with a large effect on daily work. Start with tmux new -A -s work, learn detach and the pane splits, and add the three configuration lines. Overall, it is one of the most useful habits a Linux user can build.
Linux
More like this
Linux
entr and watch: Make Your Terminal Rerun Things So You Don't Have To
Save, switch window, press up, press Enter, squint. Save, switch window, press up... Stop. entr reruns your command whenever a file changes, and watch reruns it on a timer. Two tiny tools, a thousand saved keystrokes.
Linux
ripgrep: grep, But It Drank Three Espressos and Read Your .gitignore
grep is a legend. It's also happy to search all 80,000 files in node_modules while you wait. ripgrep searches only what matters, very quickly, with sensible defaults. The flags worth knowing, and when old grep still wins.
Linux
Linux Terminal Toys (and the Surprisingly Useful Tools Hiding Among Them)
A talking cow, a train that punishes typos, the Matrix in your terminal, and hidden among them, six genuinely useful tools you'll keep long after the novelty wears off. A toy box for Linux nerds.