LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

Tmux

Getting Started

Tmux is a terminal multiplexer: it runs multiple terminal sessions inside a single window and keeps them alive after you disconnect. It is organized in 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 keeps running in the background after you detach or your connection drops. Reattach later to pick up exactly 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 new -d -s mysession
copy
$ tmux rename-session -t old new
copy
$ tmux kill-session -t mysession
copy
$ tmux kill-server
copy
tmux attach without -t attaches to the most recently used session. kill-server stops tmux entirely, including all sessions.

Windows

Windows are like tabs within a session. Each window runs its own shell and is numbered, starting at 0 by default.
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 lSwitch to the last active window
Ctrl+b wList all windows and select one
Ctrl+b ,Rename current window
Ctrl+b .Move current window to another number
Ctrl+b &Close current window (with confirmation)

Panes

Panes split a window into multiple terminal areas. Each pane runs its own independent shell.
KeyDescription
Ctrl+b %Split into left and right panes
Ctrl+b "Split into top and bottom panes
Ctrl+b ArrowMove to the pane in that direction
Ctrl+b oCycle to the next pane
Ctrl+b ;Jump to the last active 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 (press a number to jump there)
Ctrl+b !Break current pane out into a new window

Resizing Panes

Press the prefix, then an arrow key combined with a modifier. Keep the modifier held to repeat the resize without pressing the prefix again.
KeyDescription
Ctrl+b Ctrl+ArrowResize pane by 1 cell in that direction
Ctrl+b Alt+ArrowResize pane by 5 cells in that direction
The same works from the command prompt, with -D, -U, -L, -R for down, up, left, right.
$ tmux resize-pane -D 10
copy
$ tmux resize-pane -R 10
copy

Copy Mode

Copy mode lets you scroll through output, search it, and copy text. Press Ctrl+b [ to enter it (or Ctrl+b PgUp to enter it scrolled up one page).By default tmux uses Emacs-style keys in copy mode. Most users switch to vi-style keys, which the table below assumes.
$ echo "setw -g mode-keys vi" >> ~/.tmux.conf
copy
KeyDescription
qExit copy mode
h j k lMove left, down, up, right
w / bMove forward / back one word
g / GJump to the top / bottom of the history
Ctrl+u / Ctrl+dScroll up / down half a page
/Search forward
?Search backward
n / NNext / previous search match
SpaceStart selection
EnterCopy selection and exit copy mode
Outside copy mode, paste the most recently copied text.
KeyDescription
Ctrl+b ]Paste the copied text

Command Mode

Press Ctrl+b : to open the tmux command prompt. Every keybinding is a shortcut for one of these commands, and any of them can be run here directly.
$ new-window -n mywin
copy
$ split-window -h
copy
$ swap-window -t 0
copy
$ select-layout even-horizontal
copy
$ setw synchronize-panes on
copy
synchronize-panes sends your typing to all panes in the window at once. Handy for running the same command on several servers. Turn it off with setw synchronize-panes off.

Configuration

Tmux reads ~/.tmux.conf at server start. Useful options to add:
$ set -g mouse on
copy
$ set -g history-limit 50000
copy
$ set -g base-index 1
copy
$ setw -g pane-base-index 1
copy
mouse on enables clicking panes, dragging borders to resize, and scrolling with the wheel. base-index 1 numbers windows from 1 instead of 0, matching the keyboard layout.Apply changes to a running server without restarting.
$ tmux source-file ~/.tmux.conf
copy
Copied to clipboard
Kai