LinuxCommandLibrary

Tmux

Getting Started

Tmux is a terminal multiplexer — it lets you run multiple terminal sessions inside a single window and keep them alive after disconnecting. It is organized into three layers: sessions contain windows, and windows contain panes.
$ tmux new -s mysession
copy
All keybindings start with the prefix key, which is Ctrl+b by default. Press the prefix, release it, then press the command key.

Sessions

A session is a collection of windows that persists even after you detach or close the terminal. You can reattach later to pick up where you left off.
KeyDescription
Ctrl+b dDetach from current session
Ctrl+b sList all sessions and switch
Ctrl+b $Rename current session
Ctrl+b (Switch to previous session
Ctrl+b )Switch to next session
$ tmux ls
copy
$ tmux attach -t mysession
copy
$ tmux kill-session -t mysession
copy
$ tmux rename-session -t old new
copy

Windows

Windows are like tabs within a session. Each window runs its own shell.
KeyDescription
Ctrl+b cCreate a new window
Ctrl+b nSwitch to the next window
Ctrl+b pSwitch to the previous window
Ctrl+b 0-9Switch to window by number
Ctrl+b ,Rename current window
Ctrl+b &Close current window (with confirmation)
Ctrl+b wList all windows and select one
Ctrl+b lSwitch to the last active window

Panes

Panes split a window into multiple terminal areas. Each pane runs independently.
KeyDescription
Ctrl+b %Split pane vertically (left/right)
Ctrl+b "Split pane horizontally (top/bottom)
Ctrl+b ArrowMove to the pane in that direction
Ctrl+b oCycle to the next pane
Ctrl+b zToggle zoom on current pane (full window)
Ctrl+b xClose current pane (with confirmation)
Ctrl+b {Swap current pane with the previous one
Ctrl+b }Swap current pane with the next one
Ctrl+b SpaceCycle through pane layouts
Ctrl+b qShow pane numbers briefly
Ctrl+b !Convert current pane into a new window

Resizing Panes

Hold the prefix and use modifier keys with arrows to resize the active pane.
KeyDescription
Ctrl+b Ctrl+ArrowResize pane by 1 cell in that direction
Ctrl+b Alt+ArrowResize pane by 5 cells in that direction
You can also resize from command mode.
$ tmux resize-pane -D 10
copy
$ tmux resize-pane -U 10
copy
$ tmux resize-pane -L 10
copy
$ tmux resize-pane -R 10
copy

Copy Mode

Copy mode lets you scroll through output and copy text. Tmux uses vi-style keys by default when `mode-keys` is set to vi.
Press Ctrl+b [ to enter copy mode and q to exit.
KeyDescription
Ctrl+b [Enter copy mode
qExit copy mode
h j k lMove left, down, up, right
wMove forward one word
bMove back one word
/Search forward
?Search backward
nNext search match
NPrevious search match
SpaceStart selection
EnterCopy selection and exit copy mode
Ctrl+b ]Paste the copied text
To enable vi-style keys, add this to your config file.
$ echo "setw -g mode-keys vi" >> ~/.tmux.conf
copy

Command Mode

Press Ctrl+b : to open the tmux command prompt. From here you can run any tmux command directly.
$ new-window -n mywin
copy
$ split-window -h
copy
$ swap-window -t 0
copy
$ select-layout even-horizontal
copy
$ source-file ~/.tmux.conf
copy
Useful configuration options to add to ~/.tmux.conf.
$ set -g mouse on
copy
$ set -g history-limit 10000
copy
$ set -g base-index 1
copy
$ setw -g pane-base-index 1
copy

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard