tmux (Terminal MultipleXer) is a tool that lets you run multiple terminal sessions inside one terminal
window, and keep them running even if you disconnect. It's especially popular with developers,
sysadmins, and anyone who works over SSH. It is a modern replacement for screen.
Benefits:
Connection: main benefit is that tmux session survives even if it disconnects (close terminal or drop SSH).
Terminals: tmux allows multiple terminals in one window, using horizontal and vertical splits.
Keyboard control: everything in tmux is keyboard-driven (faster than mouse-based terminals).
Good for remote servers: works the same locally and over SSH, without GUI.
Session management: separate workspaces for different projects, which allows instant switch.
If your terminal closes or your SSH drops, tmux keeps everything running. You can also have multiple
terminals (splits horizontally or vertically) in one window.
tmux is structured in three levels: Session -> Window -> Pane
Session is a whole tmux environment. You can have multiple sessions, and each session
can have multiple windows. Each session has a name. If no name indicated, it uses numerics by default.
The name of the session is on the bottom left corner of the the tmux ribbon.
Window is the terminal that you can run a program. It is similar to tab in a browser, or terminal.
Windows are full screen in the session. They are numbered at the bottom ribbon of the session.
Pane is a split terminal inside the window. It allows you to see multiple terminals at once.
To create a plane you need to split a window.
The commands for each levels are described below.
There are three types of tmux commands:
terminal commands: All tmux commands in terminal start with tmux.
All terminal commands in this tutorial start with tmux.
session commands: To execute a command/shortcut while you are in a session, press
prefix <command>. The default prefix is ctrl+b, but can be changes in the
tmux.config configuration file.
All session commands in this tutorial start with ctrl+b (the default prefix).
typed session commands: When in a tmux session, you can enter the tmux command mode by ctrl+b :.
The tmux typed commands in this tutorial start with :.
tmux list-keys
:list-keys
ctrl+b ?
lists all shortcuts/key bindings
Installation/Versions/Updates
sudo apt install tmux || for Ubuntu | 'ti0'
sudo dnf install tmux || for Ferdora | 'ti1'
sudo yum install tmux || for RHEL | 'ti1'
sudo brew install tmux || for macOS | 'ti1'
tmux -V || prints the version of tmux | 'ti1'
Configuration
tmux can be configured using command lines. Some of the commands are listed below.
To permanently set tmux configuration, you put your settings in a tmux config file at ~/.tmux.conf.
tmux reads this file every time it starts. If this file does not exist, create it, and use the following
commands to set the tmux.
tmux show-options -g
tmux show-window-options -g
shows current settings
set -g renumber-windows on || automatically renumbers windows after closing a window | 'tc3'
set -g mouse || activates mouse in tmux | 'tc4'
:set <tmuxConfig>|| sets the tmuxConfig for active tmux window | tc1
:setw -g <tmuxConfig>|| sets the tmuxConfig for all windows in the current
tmux sessions | tc2
:set -g <tmuxConfig>|| sets the tmuxConfig for all tmux sessions | tc3
:set -g mouse
enables mouse in the current session (first enter the command mode ctrl+b :). By default, mouse is
disabled. You can switch between panes with mouse, resize panes, or even right-click on a pane to swap or split.
Session
tmux
tmux new
tmux new-session
:new
creates a new session and attaches automatically. It uses numerics to name the sessions.
tmux new-session -s <sessionName>
:new -s <sessionName>
creates a new session with the name <sessionName> and attaches automatically.
tmux new-session -A -s <sessionName> || if <sessionName> does not exist, creates a new session with the name
<sessionName> and attaches automatically. Otherwise, it attaches to <sessionName>. | 't6'
tmux new-session -s <sessionName> -n <winName> || creates a new session with the name
<sessionName>, a window with the name winName, and attaches automatically. | 't61'
tmux ls
tmux list-sessions
lists all tmux sessions in terminal.
ctrl+b s || shows all sessions, while you are in a session. It allows you to switch to a different session. | 't8'
ctrl+b w || shows all sessions and windows, and tabs, while you are in a session. It allows you to switch to a different session. | 't9'
ctrl+b (|| moves the previous session. | 't10'
ctrl+b )|| moves the next session. | 't11'
tmux a
tmux at
tmux attach
tmux attach-session
attaches to the last used session
tmux a -t <sessionName>
tmux at -t <sessionName>
tmux attach -t <sessionName>
tmux attach-session -t <sessionName>
attaches to a session named <sessionName>
tmux kill-session|| kills the last active session. | 't14'
tmux kill-session -t <sessionName>|| kills session with the name <sessionName>. | 't15'
:new kill-session || kills the current session. | 't16'
tmux kill-session -a || kills all sessions, except the last one used. | 't17'
tmux kill-session -a -t <sessionName> || kills all sessions, except session named <sessionName>. | 't18'
ctrl+b d || detaches from the current session. | 't19'
ctrl+b $ || renames the current session. | 't20'
Window
ctrl+b c || creates a new window in the current session |'tw1'
ctrl+b , || renames current window (type the new name next in the tmux ribbon) |'tw2'
ctrl+b & || closes current window |'tw3'
ctrl+b w || shows all sessions and windows, and tabs, while you are in a session. It allows you to switch to a different session. | 'tw4'
ctrl+b p || goes to the previous window (based on the order in the tmux ribbon) |'tw5'
ctrl+b n || goes to the next window (based on the order in the tmux ribbon) |'tw6'
ctrl+b <single digit number:0..9> ||goes to window by number. It only works for windows 0 to 9. |'tw7'
ctrl+b l || switches to the last active window |'tw8'
:swap-window -s <sourceNum> -t <destNum>|| swaps window number sourceNum with destNum |'tw9'
:swap-window -t <nPosition>|| moves current window to the left by nPosition |'tw10'
:move-window -s <Source>:<positionNumber1> -t <Destination>
tmux move-window -s <Source>:<positionNumber1> -t <Destination>
moves the window in positionNum1 from Source to the end of windows in the Destination session
:move-window -s <Source>:<positionNumber1> -t <Destination>:<positionNumber2>
tmux move-window -s <Source>:<positionNumber1> -t <Destination>:<positionNumber2>
moves the window in positionNum1 from Source to positionNum2 in the Destination session.
It fails if positionNum2 is already occupied.
:move-window -r || renumbers windows in the current session. If some window indices are missing
after closing a window, this commands reorders the window numbers.|'tw15'
tmux set-option -g renumber-windows on|| renumbers windows after closing a window | 'tw16'
Pane
ctrl+b ;|| toggles between the last active pane | 'tp0'
ctrl+b %
:split-window -h
splits the current window horizontally, by creating two vertical panes
ctrl+b "|| toggles between the last active pane | 'tp0'
ctrl+b "
:split-window -v
splits the current window vertically, by creating two horizontal panes
ctrl+b m|| marks the active pane (highlighted). It can be used to move a pane. see join-pane. | 'tp7'
ctrl+b M|| clears the marked pane | 'tp71'
ctrl+b !
:break-pane
moves the active pane to a new window. See join-pane to move it back.
:join-pane <optional -h or -v>|| moves the marked pane to the active pane in the
current window. Optionally, you can add -v to add the marked pane horizontally, or -h for vertical
merge. See ctrl+b m. | tp8
:join-pane -s <sourceWinNum> -t <destWinNum> || moves the pane from
sourceWinNum to destWinNum. It only works if the source and destination pane have only one pane.
To redo the join pane, run :break-pane. If you are already on the sourceWinNum, you can skip -s <sourceWinNum>. | 'tp6'
ctrl+b {|| moves the active pane clockwise (moves it to the left, if there is no option to move
left, moves it up. If it is already the top pane, it goes back to the end). | 'tp9'
ctrl+b }|| moves the active pane counter-clockwise (moves it to the right, if there is no option to
move right, moves it down. If it is already the bottom pane, it goes back to the top). | 'tp10'
ctrl+b ↑/↓/←/→|| switches that active pane using arrows | 'tp11'
:setw synchronize-panes|| synchronizes the same command across all panes in the current window.
It helps when you want to run the same command on multiple servers. To turn it off, run the same
command again. | 'tp12'
ctrl+b space|| toggles various layouts for the panes in the current window | 'tp13'
ctrl+b o|| moves the active pane to the next pane in the current window | 'tp14'
ctrl+b q|| shows pane numbers | 'tp15'
ctrl+b q 0..9|| switches the active pane by number | 'tp16'
ctrl+b z|| maximizes (zooms in) the active pane. To unmaximize, press the same command again. | 'tp17'
ctrl+b ctrl+↑/↓/←/→|| resizes the pane in up/down/left/right direction.
You can hold control, then press b and repeatedly the arrow key to get the desired size. | 'tp18'
ctrl+b x|| closes the active pane | 'tp19'
Copy/Paste
In tmux, buffers are the internal clipboards tmux uses to store copied text. You can think of as a stack
of copied text snippets. tmux can store multiple buffers at once. As
you make more copies, it pushed down the stack. To see the list of buffers, you can use list-buffers.
Buffers are shared between different sessions.
:show-buffer || shows the content of buffer 0 | 'tc1'
:list-buffers || lists the content of all buffers | 'tc2'
:capture-pane || captures/copies the entire visible content of the pane in buffer | 'tc3'
:choose-buffer || shows all buffers and pastes the selected buffer | 'tc4'
:load-buffer <fileName.txt> -b <bufferNum> || loads the file in the
buffer <bufferNum>. If you omit -b, it goes to buffer 0. | 'tc50'
:save-buffer <fileName.txt>|| saves the buffer in <fileName.txt> | 'tc5'
:delete-buffer -b <bufferNum>|| deletes the content of buffer <bufferNum> | 'tc6'
copy to tmux buffer, use keyboard only (without mouse): :set mode-keys vi
activates vim keyboard shortcuts in tmux. It is necessary to use the vim shortcuts, to
navigate through tmux pane. It is easier if you add this to your tmux.config file as stated
in the previous section.ctrl+b [
enters the copy mode.
These vim shortcuts allow you to navigate in the pane
(full list of vim shortcuts):
PgUp/PgDn || moves one page up/down in the copy mode | 'tv1'
g/G || moves top/bottom line in the copy mode | 'tv2'
↑/↓/←/→ (k/j/h/l)|| scrolls top/bottom/left/right in the copy mode | 'tv3'
/ || enters the forward search mode | 'tp23'
? || enters the backward search mode | 'tp24'
n/N || searches forward/backward for the word entered with / or ? in the copy mode | 'tp25'
space || sets the beginning of the selection | 'tp26'
ctrl+b ] || pastes the content of buffer_0. This is, of course, only for the tmux panes. | 'tp27'
q || quits the copy mode | 'tp28'
copy to clipboard, use keyboard only (without mouse):
set -g set-clipboard on || sets the clipboard for tmux. If you follow the instruction to
copy to tmux buffer, it automatically adds that to the clipboard.
You need to have xclip installed for linux to use this feature. If not, install it as follows:
sudo apt install xclip for Ubuntu, or sudo dnf install xclip for Fedora, CentOS, or Red Hat. | 'tv1'
copy to tmux buffer, use mouse: if the mouse option is set for tmux, you can select from the active pane,
even if you are not in the copy mode. To paste the buffer, in the pane: ctrl+b ].
copy to clipboard, use mouse: if you set the mouse on, and set the clipboard, you can directly select
and copy to clipboard, even if you are not in the copy mode.