tmux: Terminal Multiplexer Guide
tmux: Terminal Multiplexer Guide
org/title/Tmux
tmux
tmux ([Link] is a "terminal multiplexer: it enables a number of terminals
(or windows), each running a separate program, to be created, accessed, and controlled from a
single screen. tmux may be detached from a screen and continue running in the background, then
later reattached."
tmux is an ISC-licensed alternative to GNU Screen. Although similar, there are many differences
between the programs, as noted on the tmux FAQ page ([Link]
ki/FAQ).
1 Installation
Install the tmux ([Link] package.
2 Configuration
By default, tmux looks for user-specific configuration at
$XDG_CONFIG_HOME/tmux/[Link] followed by ~/.config/tmux/[Link] , as of
3.2 ([Link]
a3f2/CHANGES#L145). A global configuration file may be provided at /etc/[Link]
though by default Arch does not ship such a file.
After splitting a window into multiple panes, a pane can be resized by the hitting prefix key (e.g.
Ctrl+b ) and, while continuing to hold Ctrl , press Left / Right / Up / Down . Swapping
panes is achieved in the same manner, but by hitting o instead of a directional key.
Key bindings may be changed with the bind and unbind commands in [Link] . For example,
the default prefix binding of Ctrl+b can be changed to Ctrl+a by adding the following
commands in your configuration file:
unbind C-b
set -g prefix C-a
bind C-a send-prefix
1 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
Tip
Quote special characters to use them as prefix. You may also use Alt (called Meta) instead of
Ctrl . For example: set -g prefix m-'\'
To create a new window you can use Ctrl+b c and move forward one window with Ctrl+b n
and backwards one window with Ctrl+b p .
tmux has a find-window option & key binding to ease navigation of many windows:
Ctrl+b [
You can navigate the buffer as you would in your default editor.
vi mode:
emacs mode:
Esc
2 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
[Link]
Also, if tmux messes up, you can force tmux to assume that the terminal support 256 colors, by
adding the following alias in .bashrc:
For example, if you use the Alacritty terminal, you would add:
For other terminals, replace alacritty above with the relevant terminal type as stored in
$TERM .
2.3.3 xterm-keys
3 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
[Link]
set-option -g xterm-keys on
If you enable xterm-keys in your [Link] , then you need to build a custom terminfo to
declare the new escape codes or applications will not know about them. Compile the following with
tic and you can use "xterm-screen-256color" as your TERM:
$ tic -c xterm-screen-256color
"xterm-screen-256color", line 16, terminal 'xterm-screen-256color': resolution of use=screen-256color-bce
failed
$ tic xterm-screen-256color
2.4 Theming
Tmux can be themed however in order to do so, first one needs to know the color codes. This can
be achieved by executing the below command, which will print the color codes together with a
sample of such color:
The color codes printed using the above command can then be used to change the tmux color
scheme. The following is an example for how one can change the colors of the status bar:
4 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
# border colors
set -g pane-border-style fg="colour255"
set -g pane-active-border-style fg="colour33"
Furthermore, any customization attached to your tmux session will be retained and your tmux
session can be made to persist even if you have never logged in, if you have some reason to do that
(like a heavily scripted tmux configuration or shared user tmux sessions).
The service below starts tmux for the specified user (i.e. start/enable with
tmux@[Link] ):
/etc/systemd/system/tmux@.service
[Unit]
Description=tmux session for user %I
[Service]
Type=forking
User=%I
# WARNING: use %I instead of %u here; %u=root in system services.
ExecStart=/usr/bin/tmux new-session -s %I -d
ExecStop=/usr/bin/tmux kill-session -t %I
[Install]
WantedBy=[Link]
5 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
Tip
Alternatively, you can place this file within your systemd/User directory (without User=%I and
by replacing [Link] with [Link] in WantedBy ), for example
~/.config/systemd/user/[Link] . This way the tmux service will start when you
log in, unless you also enable systemd/User#Automatic start-up of systemd user
instances. The user service will stay active on logout due to the default explained in
systemd/User#Kill user processes on logout for this method.
/etc/systemd/user/tmux@.socket
[Unit]
Description=Terminal Multiplexer Activation Socket (listen on: %i)
Documentation=man:tmux(1)
[Socket]
ListenStream=/tmp/tmux-%U/%i
SocketMode=0600
DirectoryMode=0700
[Install]
WantedBy=[Link]
/etc/systemd/user/tmux@.service
[Unit]
Description=Terminal Multiplexer (listen on: %i)
Documentation=man:tmux(1)
Requires=tmux@%[Link]
[Service]
Environment=TMUX_SERVER_ARGS=
ExecStart=tmux -D $TMUX_SERVER_ARGS
ExecStartPost=tmux -L%i start
Slice=app-tmux-%[Link]
6 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
[Install]
WantedBy=[Link]
Also=tmux@%[Link]
(Or plop them into ~/.config/systemd/user .) This creates a tmux process per service
instance, each with a separate socket. tmux -D starts a non-forking tmux server, which is
correctly passed the activation FD; but this process will not execute the configuration file
directives. tmux start runs a transient client which forces the server to execute the session, if
any.
To enable just the activation, run systemctl --user enable tmux@[Link] ; start the
client normally, with tmux -L NAME (just tmux when NAME is default ). Socket activation
alone simply ensures that the tmux server is moved into its own slice (
[Link]/[Link] ). This is equivalent to what systemd devs suggest (see
systemd-run(1) § EXAMPLES ([Link]
XAMPLES), under screen), and prevents it from being killed by the session-cleanup
mechanism ([Link] This
also allows acting on the whole group, e.g. systemctl --user freeze [Link] .
To enable the server startup, run systemctl --user enable tmux@[Link] . If your
user systemd instance is lingering, this runs tmux on boot; otherwise, on login. To have the
server initialise and run programs, define sessions in your tmux config file.
Note that tmux started this way inherits your systemd environment; unless spawning login shells
for every pane, consult Systemd/User#Environment_variables on how to populate it.
/etc/systemd/user/tmux@[Link].d/[Link]
[Service]
Environment="TMUX_SERVER_ARGS=-f %h/.[Link]"
3 Session initialization
You can have tmux open a session with preloaded windows by including those details in your
~/.[Link] :
To start a session with split windows (multiple panes), include the splitw command below the
neww you would like to split; thus:
7 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
would open 2 windows, the second of which would be named foo/bar and would be split vertically
in half (50%) with foo running above bar. Focus would be in window 2 (foo/bar), top pane (foo).
Note
Numbering for sessions, windows and panes starts at zero, unless you have specified a base-
index of 1 in your .conf
To manage multiple sessions, source separate session files from your configuration file:
# initialize sessions
bind F source-file ~/.tmux/foo
bind B source-file ~/.tmux/bar
4 Clipboard integration
Tip
It is possible to copy a tmux selection to the display server clipboard (both primary/secondary
selections), and paste from it into tmux. The following tmux configuration file snippets integrate
X11 and Wayland clipboard/selection with the current tmux selection.
Note
It may be necessary to unbind the "previous window" shortcut with unbind p for the Vim
style examples to work.
4.1 On Xorg
The first possibility is using xsel ([Link]
Emacs style:
Vim style:
8 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
Emacs style:
Vim style:
4.2 On Wayland
Make sure to have wl-clipboard ([Link]
board) installed.
Emacs style:
Vim style:
Note
9 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
First, you will need to download the perl script and place it into urxvts perl lib:
wget [Link]
mv osc-xterm-clipboard /usr/lib/urxvt/perl/
You will also need to enable that perl script in your .Xdefaults:
~/.Xdefaults
...
*[Link]-ext-common: osc-xterm-clipboard
...
Next, you want to tell tmux about the new function and enable mouse support (if you have not
already):
~/.[Link]
...
set-option -ga terminal-override ',rxvt-uni*:XT:Ms=\E]52;%p1%s;%p2%s\007'
set -g mouse on
...
That's it. Be sure to end all instances of tmux before trying the new MiddleClick functionality.
While in tmux, Shift+MiddleMouseClick will paste the clipboard selection while just
MiddleMouseClick will paste your tmux buffer. Outside of tmux, just use MiddleMouseClick to
paste your tmux buffer and your standard Ctrl+c to copy.
$ tmuxinator doctor
10 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
tmux list-windows
The output may look like this (two windows with 3 panes and 2 panes layout)
The Interesting part you need to copy for later use begins after [layout... and excludes ... ] @2
(active). For the first window layout you need to copy e.g.
20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}
~/.tmuxinator/[Link]
name: default
root: ~/
windows:
- default:
layout: 20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}
panes:
- clear
- vim
- clear && emacs -nw
- remote:
layout: 24ab,274x83,0,0{137x83,0,0,3,136x83,138,0,4}
panes:
-
-
The example defines two windows named "default" and "remote". With your determined layout
values. For each pane you have to use at least one - line. Within the first window panes you start
the commandline "clear" in pane one, "vim" in pane two and "clear && emacs -nw" executes two
commands in pane three on each tmux start. The second window layout has two panes without
defining any start commmands.
tmuxinator default
11 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
~/.bashrc
if [ -z "$TMUX" ]; then
tmuxinator default
fi
#!/bin/bash
tmux new-session -d -n WindowName Command
tmux new-window -n NewWindowName
tmux split-window -v
tmux selectp -t 1
tmux split-window -h
tmux selectw -t 1
tmux -2 attach-session -d
urxvt -e bash -c "tmux -q has-session && exec tmux attach-session -d || exec tmux new-session -n$USER -
s$USER@$HOSTNAME"
12 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
▪ Users of fortune may notice that quotes are printed when creating a new panel.
▪ The configuration files for login shells such as ~/.profile are interpreted each time a
new panel is created, so commands intended to be run on session initialization (e.g.
setting audio level) are executed.
To disable this behaviour, add to ~/.[Link] :
Of course, those should not overlap with other applications' hotkeys, such as the terminal's. Given
that they substitute terminal tabbing that might as well be deactivated, though.
It can also come handy to supplement the EOT hotkey Ctrl+d with one for tmux's detach:
Screen and tmux's behaviour for when multiple clients are attached to one session
differs slightly. In Screen, each client can be connected to the session but view different
13 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
windows within it, but in tmux, all clients connected to one session must view the same
window.
This problem can be solved in tmux by spawning two separate sessions and
synchronizing the second one to the windows of the first, then pointing a second new
session to the first.
The tmx script below implements this — the version here is slightly modified to execute
tmux new-window if 1 is its second parameter. Invoked as tmx base_session_name [1]
, it launches the base session if necessary. Otherwise a new "client" session linked to the base,
optionally add a new window and attach, setting it to kill itself once it turns "zombie". Do not
forget to make it executable.
~/bin/tmx
#!/bin/bash
# Modified TMUX start script from:
# [Link]
# Works because bash automatically trims by assigning to variables and by passing arguments
trim() { echo $1; }
base_session="$1"
# This actually works without the trim() on all systems except OSX
tmux_nb=$(trim `tmux ls | grep "^$base_session" | wc -l`)
if [[ "$tmux_nb" == "0" ]]; then
echo "Launching tmux base session $base_session ..."
tmux new-session -s $base_session
else
# Make sure we are not already in a tmux session
if [[ -z "$TMUX" ]]; then
echo "Launching copy of base session $base_session ..."
# Session id is date and time to prevent conflict
session_id=`date +%Y%m%d%H%M%S`
# Create a new session (without attaching it) and link to base session
# to share windows
tmux new-session -d -t $base_session -s $session_id
if [[ "$2" == "1" ]]; then
# Create a new window in that session
tmux new-window
fi
# Attach to the new session & kill it once orphaned
tmux attach-session -t $session_id \; set-option destroy-unattached
fi
fi
14 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
setw -g aggressive-resize on
added to ~/.[Link] . It causes tmux to resize a window based on the smallest client actually
viewing it, not on the smallest one attached to the entire session.
~/.bashrc
function rsc() {
CLIENTID=$1.`date +%S`
tmux new-session -d -t $1 -s $CLIENTID \; set-option destroy-unattached \; attach-session -t $CLIENTID
}
function mksc() {
tmux new-session -d -s $1
rsc $1
}
"mksc foo" creates a always detached permanent client named "foo". It also calls "rsc
foo" to create a client to newly created session. "rsc foo" creates a new client grouped
by "foo" name. It has destroy-unattached turned on so when I leave it, it kills client.
Therefore, when my computer looses network connectivity, all "[Link]" clients
are killed while "foo" remains. I can then call "rsc foo" to continue work from where I
stopped.
~/.[Link]
## update the TERM variable of terminal emulator when creating a new session or attaching a existing sess
ion
set -g update-environment 'DISPLAY SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY TERM'
## determine if we should enable 256-colour support
if "[[ ${TERM} =~ 256color || ${TERM} == fbterm ]]" 'set -g default-terminal screen-256color'
~/.zshrc
## workaround for handling TERM variable in multiple tmux sessions properly (by Nicholas Marriott)
if [[ -n ${TMUX} && -n ${commands[tmux]} ]];then
case $(tmux showenv TERM 2>/dev/null) in
*256color) ;&
TERM=fbterm)
TERM=screen-256color ;;
15 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
*)
TERM=screen
esac
fi
~/.[Link]
source .[Link]
#!/bin/bash
PID=$(pidof $1)
if [ -z "$PID" ]; then
tmux new-session -d -s main ;
tmux new-window -t main -n $1 "$*" ;
fi
tmux attach-session -d -t main ;
tmux select-window -t $1 ;
exit 0
A derived version to run irssi with the nicklist plugin can be found on its ArchWiki page.
16 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
adapt to whatever host you connect to, set the following in ~/.[Link]
set -g set-titles on
set -g set-titles-string "#T"
Tip
A more friendly key-binding is to use Ctrl+b h for splitting horizontally and Ctrl+b v for
splitting a pane vertically, it is also very convenient to remember.
17 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
bind-key v split-window -v
6 Troubleshooting
This tricks the terminal emulator into thinking tmux is a full screen application like pico or
mutt[5] ([Link]
x), which will make the scrollback be recorded properly. Beware however, it will get a bit messed
up when switching between windows/panes. Consider using tmux's native scrollback instead.
Note
This interferes with selection buffer copying and pasting. To copy/paste to/from the selection
buffer hold the shift key.
If you want to scroll with your mouse wheel, ensure mode-mouse is on in .[Link]
set -g mouse on
For mouse wheel scrolling as from tmux 2.1 try adding one or both of these to ~/.[Link]
18 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
-M"
Though the above will only scroll one line at a time, add this solution to scroll an entire page
instead
7 See also
▪ BBS topic ([Link]
▪ Screen and tmux feature comparison ([Link]
▪ powerline ([Link] a dynamic statusbar for tmux
▪ Plugins for tmux ([Link]
▪ Oh My Tmux! ([Link]
▪ tmux Wiki ([Link]
Tutorials
19 of 20 12/9/25, 7:06 PM
tmux - ArchWiki [Link]
20 of 20 12/9/25, 7:06 PM