Tmux
Tmux
tmux
tmux (https://tmux.github.io/) 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 (https://github.co
m/tmux/tmux/wiki/FAQ).
1 Installation
Install the tmux (https://archlinux.org/packages/?name=tmux)
package. Optionally, install tmux-bash-completion-git (https://aur.ar
chlinux.org/packages/tmux-bash-completion-git/)AUR to provide
bash completion functions for tmux.
2 Configuration
By default, tmux looks for user-specific configuration at
$XDG_CONFIG_HOME/tmux/tmux.conf followed by
~/.config/tmux/tmux.conf , as of 3.2 (https://github.com/tmux/tmux/blob/a
5f99e14c6f264e568b860692b89d11f5298a3f2/CHANGES#L145). A global
configuration file may be provided at /etc/tmux.conf 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.
https://wiki.archlinux.org/title/Tmux 1/21
13/11/24, 16:02 tmux - ArchWiki
Key bindings may be changed with the bind and unbind commands in tmux.conf .
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
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.
https://wiki.archlinux.org/title/Tmux 2/21
13/11/24, 16:02 tmux - ArchWiki
vi mode:
emacs mode:
Esc
tmux.conf
Also, if tmux messes up, you can force tmux to assume that the terminal support 256
colors, by adding the following alias in .bashrc:
tmux supports 24-bit color. If your terminal supports this mode (see [1] (https://github.c
om/termstandard/colors/blob/master/README.md)), add it to the
terminal-features setting.
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
To enable xterm-keys, add the following line in the configuration file:
tmux.conf
set-option -g xterm-keys on
If you enable xterm-keys in your tmux.conf , 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
https://wiki.archlinux.org/title/Tmux 4/21
13/11/24, 16:02 tmux - ArchWiki
$ 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:
# border colors
set -g pane-border-style fg="colour255"
set -g pane-active-border-style fg="colour33"
https://wiki.archlinux.org/title/Tmux 5/21
13/11/24, 16:02 tmux - ArchWiki
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
[email protected] ):
/etc/systemd/system/[email protected]
[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=multi-user.target
Tip:
You may want to add WorkingDirectory=custom_path to
customize working directory. If set to ~ , the home directory of the user
specified in User= is used.
Add -v tmux arg to generate tmux client and server logs in
WorkingDirectory.
If you want to import environment variables related to graphical session,
such as DISPLAY , WAYLAND_DISPLAY , or SESSION_MANAGER ,
change WantedBy option to graphical-session.target , and add
After=graphical-session.target under [Unit] section.
Alternatively, you can create a script that runs automatically after the
desktop environment, window manager, or Wayland compositor loads
https://wiki.archlinux.org/title/Tmux 6/21
13/11/24, 16:02 tmux - ArchWiki
Alternatively, you can place this file within your systemd/User directory (without
User=%I and by replacing multi-user.target with default.target in
WantedBy ), for example ~/.config/systemd/user/tmux.service . 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.
3 Session initialization
You can have tmux open a session with preloaded windows by including those details in
your ~/.tmux.conf :
To start a session with split windows (multiple panes), include the splitw command
below the neww you would like to split; thus:
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:
https://wiki.archlinux.org/title/Tmux 7/21
13/11/24, 16:02 tmux - ArchWiki
# initialize sessions
bind F source-file ~/.tmux/foo
bind B source-file ~/.tmux/bar
4 Clipboard integration
Tip: The tmux plugin tmux-yank (https://github.com/tmux-plugins/tmux
-yank) provides similar functionality.
4.1 On Xorg
The first possibility is using xsel (https://archlinux.org/packages/?na
me=xsel):
Emacs style:
Vim style:
Emacs style:
https://wiki.archlinux.org/title/Tmux 8/21
13/11/24, 16:02 tmux - ArchWiki
Vim style:
4.2 On Wayland
Make sure to have wl-clipboard (https://archlinux.org/packages/?
name=wl-clipboard) installed.
Emacs style:
Vim style:
First, you will need to download the perl script and place it into urxvts perl lib:
wget http://anti.teamidiot.de/static/nei/*/Code/urxvt/osc-xterm-clipboard
mv osc-xterm-clipboard /usr/lib/urxvt/perl/
You will also need to enable that perl script in your .Xdefaults:
https://wiki.archlinux.org/title/Tmux 9/21
13/11/24, 16:02 tmux - ArchWiki
~/.Xdefaults
...
*URxvt.perl-ext-common: osc-xterm-clipboard
...
Next, you want to tell tmux about the new function and enable mouse support (if you
have not already):
~/.tmux.conf
...
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
tmux list-windows
The output may look like this (two windows with 3 panes and 2 panes layout)
https://wiki.archlinux.org/title/Tmux 10/21
13/11/24, 16:02 tmux - ArchWiki
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/default.yml
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
~/.bashrc
https://wiki.archlinux.org/title/Tmux 11/21
13/11/24, 16:02 tmux - ArchWiki
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"
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 ~/.tmux.conf :
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:
https://wiki.archlinux.org/title/Tmux 13/21
13/11/24, 16:02 tmux - ArchWiki
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:
# http://forums.gentoo.org/viewtopic-t-836006-start-0.html
# Works because bash automatically trims by assigning to variables and by passing arg
uments
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
https://wiki.archlinux.org/title/Tmux 14/21
13/11/24, 16:02 tmux - ArchWiki
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
setw -g aggressive-resize on
~/.bashrc
function rsc() {
CLIENTID=$1.`date +%S`
tmux new-session -d -t $1 -s $CLIENTID \; set-option destroy-unattached \; attach-s
ession -t $CLIENTID
}
function mksc() {
tmux new-session -d -s $1
rsc $1
}
https://wiki.archlinux.org/title/Tmux 15/21
13/11/24, 16:02 tmux - ArchWiki
~/.tmux.conf
## update the TERM variable of terminal emulator when creating a new session or attac
hing a existing session
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-2
56color'
~/.zshrc
## workaround for handling TERM variable in multiple tmux sessions properly (by Nicho
las Marriott)
if [[ -n ${TMUX} && -n ${commands[tmux]} ]];then
case $(tmux showenv TERM 2>/dev/null) in
*256color) ;&
TERM=fbterm)
TERM=screen-256color ;;
*)
TERM=screen
esac
fi
https://wiki.archlinux.org/title/Tmux 16/21
13/11/24, 16:02 tmux - ArchWiki
~/.tmux.conf
source .tmux.conf
#!/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.
set -g set-titles on
set -g set-titles-string "#T"
https://wiki.archlinux.org/title/Tmux 17/21
13/11/24, 16:02 tmux - ArchWiki
https://wiki.archlinux.org/title/Tmux 18/21
13/11/24, 16:02 tmux - ArchWiki
6 Troubleshooting
This tricks the terminal emulator into thinking tmux is a full screen application like pico
or mutt[5] (https://superuser.com/questions/310251/use-terminal-scrollbar-with-tmu
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.
If you want to scroll with your mouse wheel, ensure mode-mouse is on in .tmux.conf
set -g mouse on
https://wiki.archlinux.org/title/Tmux 19/21
13/11/24, 16:02 tmux - ArchWiki
For mouse wheel scrolling as from tmux 2.1 try adding one or both of these to
~/.tmux.conf
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 (https://bbs.archlinux.org/viewtopic.php?id=84157&p=1)
Screen and tmux feature comparison (https://www.dayid.org/os/notes/t
m.html)
powerline (https://github.com/Lokaltog/powerline), a dynamic statusbar
for tmux
Plugins for tmux (https://github.com/tmux-plugins)
Oh My Tmux! (https://github.com/gpakosz/.tmux)
Tutorials
https://wiki.archlinux.org/title/Tmux 20/21
13/11/24, 16:02 tmux - ArchWiki
https://wiki.archlinux.org/title/Tmux 21/21
13/11/24, 15:58 » Linux Magazine
Magical Window
Article from Issue 152/2013
Author(s): Mike Schilli
Instead of manually rearranging windows in a development
environment time and time again, the Tmux terminal
multiplexer can restore them from a configuration script.
Tmux runs on all major platforms. If you work on a Linux desktop but use a MacBook on
the road, you will find the same windows and panes in both environments.
Servers and Clients
An automatically launched Tmux server keeps track of active sessions, and the user can
remotely control them at the command line. The tmux command creates new sessions or
closes them later, adds more windows or deletes them, integrates or removes panes, and
scales panes up or down. For developer environments with multiple windows and panes,
it makes sense to save the time-consuming configuration in a source code repository and
display it again, when needed, with a single command.
https://www.linuxpromagazine.com/index.php/layout/set/print/Issues/2013/152/Perl-Tmux-Configuration 1/5
13/11/24, 15:58 » Linux Magazine
Tmux can also launch applications in panes, for example, your favorite editor, which in
turn loads a project file. Another pane could start the test suite, and a third could use
tail to display the end of a log file.
Changes Desirable
A newly installed Tmux has some weird keyboard shortcuts. To make sure keystrokes are
interpreted by the intermediate terminal multiplexer – and not by the applications running
in a Tmux session, like the editor – you need to type what is known as a prefix. A vanilla
Tmux installation uses Ctrl+B by default, a keyboard shortcut that not only causes
repetitive strain injury with frequent use but also is a common Vi command, and thus a
no-go.
Fortunately, Tmux lets you change everything, so experienced users will quickly put
together their own .tmux.conf file after the install; my file is documented in my GitHub
repository [3]. It is ultimately only based on general recommendations, such as setting a
screen-compatible prefix with Ctrl+A and using vim-style combinations for the rest:
H=left, J= down, K=up, L=right. This removes the need to enter Emacs-style text
commands to toggle between panes; instead, you can use <Prefix> H to go to the left-
hand panel.
Of course, customized shortcuts have a couple of drawbacks; for example, everyone
speaks a different Tmux dialect and flaps around like a bug on its back in anyone else's
environment. However, Tmux understands written commands, introduced with the
<Prefix>: keyboard shortcut (prefix followed by a colon), which you type in the opening
status line.
Tmux also understands the same commands at the shell prompt, meaning your own
scripts for creating a window group can be used globally. For example, the Perl script in
Listing 1 issues the
Listing 1
tmux-setup
tmux kill-session
command with the session name log4perl in line 27, if the user runs the script with the
-k option. The command opens a connection to the Tmux server (if it is running), finds
the session, and then winds it up.
Once Only
The -v (verbose) option tells Listing 1 to set the Log4perl configuration to the $DEBUG
logging level; $INFO would mean far less talk. To avoid the need for the script to write out
every single Tmux command, the tmux() function in lines 68-74 calls tmux via the shell.
The tap() function from the CPAN Sysadm::Install module triggers this. Because I
wanted to avoid having to use parentheses when calling tmux(), line 12 declares it as a
function in the current namespace.
Line 32 calls source to parse the configuration file .tmux.conf from the user's home
directory. Tmux should actually do this automatically, but the version I used didn't comply
https://www.linuxpromagazine.com/index.php/layout/set/print/Issues/2013/152/Perl-Tmux-Configuration 2/5
13/11/24, 15:58 » Linux Magazine
– this looks like a bug. Additionally, in line 36, it uses the tmux has-session command
to discover whether the Tmux session to be established is already running and drops
down to attach in line 64 if the session is running and the script only needs to join it.
If the session is unknown, the new-session command in line 44 creates a new session
named log4perl. The session name refers to the Log4perl project in whose Git
repository it acts as a developer environment. The -d (detach) parameter at the end of
the Tmux command stipulates that the script should not immediately enter the session
and display the GUI but wait patiently for the attach command in line 64 before showing
the user anything.
1 2 Next »
Price $2.95
(incl. VAT)
Print Issues
Digital Issues
SUBSCRIPTIONS
https://www.linuxpromagazine.com/index.php/layout/set/print/Issues/2013/152/Perl-Tmux-Configuration 3/5
13/11/24, 15:58 » Linux Magazine
Print Subs
Digisubs
US / Canada
UK / Australia
Related content
Multi-Tasking
Operate multiple terminal sessions in a single window.
more »
FOSSPicks
This month Graham reviews Bela, Quickemu, GPU-Viewer, Maestral, Termux and
AnLinux, Space Cadet, and more!
more »
FOSSPicks
Sparkling gems and new releases from the world of Free and Open Source Software
more »
Remote Pair Programming
The best way to save money on software development is to get it right the first time.
Pair programming, an agile technique, saves development costs by putting two coders
to work on the same code. Visual Studio Code and tmate bring the promise of pair
programming to remote workers.
more »
Light Browsing
https://www.linuxpromagazine.com/index.php/layout/set/print/Issues/2013/152/Perl-Tmux-Configuration 4/5
13/11/24, 15:58 » Linux Magazine
This Chromium port can run inside any console, with minimal resources, and is a great
tool for making old computers really useful – and learning programming along the way.
more »
https://www.linuxpromagazine.com/index.php/layout/set/print/Issues/2013/152/Perl-Tmux-Configuration 5/5
13/11/24, 21:28 » Linux Magazine
Split Windows
The split-window statements in lines 47 and 48 with the -h (horizontal) and -v
(vertical) options create additional panes in the window by dividing the window
horizontally and vertically. Tmux numbers windows and panes from 0 up – but be careful,
a frequently used Tmux configuration option changes the start point of the enumeration to
1!
In other words, to address the second pane of the first window in the log4perl session
of a newly installed Tmux, line 55 uses log4perl:0.1; it then issues send-keys to run
the command for the test suite, followed by a closing C-m (for Return). However, you
could just as easily use vagrant ssh to change to a Vagrant VM, as described in the
previous issue of this Perl column [4], and ensure that the test suite is running in a virtual
cleanroom. Line 51 finds the first two .pm files in the hierarchy of the selected Perl project
and passes in their names to the vim editor with the -p option, which displays them in
tabs in the left-hand pane (Figure 2).
Installation
Tmux is included in many distributions' repositories; on Ubuntu, the command
will do the trick. Because the Tmux developers seem to have been very busy of late,
users will want to go for version 1.7 at least – or even 1.8 – to take advantage of all the
features introduced in this article. If you prefer a more DIY approach, you can pick up the
source code [2] and build your own version. The only important dependencies are for
libevent (at least 1.4.14 or 2.0) and ncurses. A Bash script available at GitHub [5]
automates the build process.
Brian Hogan's book [6] provides useful tips for common tasks – from developer
environments through pair programming, wherein two developers in two locations type in
different panes in the same session. Even better: If you check the script into your source
control system, the next engineer can simply fire up the development environment and
start working productively right away.
Mike Schilli
https://www.linuxpromagazine.com/index.php/layout/set/print/Issues/2013/152/Perl-Tmux-Configuration/(offset)/3 1/4
13/11/24, 21:28 » Linux Magazine
Mike Schilli works as a software engineer with Yahoo! in Sunnyvale, California. He can be
contacted at mailto:[email protected]. Mike's homepage can be found at
http://perlmeister.com.
Infos
« Previous 1 2
Price $2.95
(incl. VAT)
Print Issues
Digital Issues
https://www.linuxpromagazine.com/index.php/layout/set/print/Issues/2013/152/Perl-Tmux-Configuration/(offset)/3 2/4
13/11/24, 21:28 » Linux Magazine
SUBSCRIPTIONS
Print Subs
Digisubs
US / Canada
UK / Australia
Related content
Multi-Tasking
Operate multiple terminal sessions in a single window.
more »
FOSSPicks
This month Graham reviews Bela, Quickemu, GPU-Viewer, Maestral, Termux and
AnLinux, Space Cadet, and more!
more »
FOSSPicks
Sparkling gems and new releases from the world of Free and Open Source Software
more »
Remote Pair Programming
https://www.linuxpromagazine.com/index.php/layout/set/print/Issues/2013/152/Perl-Tmux-Configuration/(offset)/3 3/4
13/11/24, 21:28 » Linux Magazine
The best way to save money on software development is to get it right the first time.
Pair programming, an agile technique, saves development costs by putting two coders
to work on the same code. Visual Studio Code and tmate bring the promise of pair
programming to remote workers.
more »
Light Browsing
This Chromium port can run inside any console, with minimal resources, and is a great
tool for making old computers really useful – and learning programming along the way.
more »
https://www.linuxpromagazine.com/index.php/layout/set/print/Issues/2013/152/Perl-Tmux-Configuration/(offset)/3 4/4
13/11/24, 16:00 An introduction to Tmux
Perl.com
(/)
An introduction to Tmux
Feb 24, 2016 by David Farrell
Tmux (https://tmux.github.io/) is a terminal multiplexer: it’s like a power-up for terminal programming.
You can manage several terminals under a session, split terminal screens, detach and re-attach sessions
and much more. If you do most of your programming at the command line, you’ll find using a terminal
multiplexer invaluable.
Setup
First you’ll need to install Tmux via your package manager or download (https://tmux.github.io/) it. Tmux
is highly configurable but the first change I’d recommend is to ssh, not Tmux. Make ssh “keep alive” for
all connections by adding this to ~/.ssh/config :
host *
ServerAliveInterval 300
ServerAliveCountMax 3
If the file doesn’t exist, create it. This configuration instructs your local machine for all user ssh sessions
to send a server alive message every 300 seconds to keep the ssh session alive. If the local machine
sends 3 unanswered messages, it will disconnect the session. You should tweak these settings to suit
your needs: for instance by restricting the host to specific domains you can have different settings per
domain. If you have a slow or unreliable internet connection, consider changing ServerAliveInterval
to a lower number to send more frequent messages.
https://www.perl.com/article/an-introduction-to-tmux/ 1/11
13/11/24, 16:00 An introduction to Tmux
If you have permission on the servers you use, you can update them with a similar configuration, in
/etc/ssh/sshd_config :
ClientAliveInterval 300
ClientAliveCountMax 3
$ tmux
Ctrl-b is the prefix combination. Press the Ctrl key AND the letter b at the same time. When inside a
Tmux session, the prefix is nearly always pressed before the shortcut key to trigger a command.
The prefix combination is really important. Once you get the hang of the prefix combination, you can
pretty much bootstrap yourself into learning Tmux with just one command. To display a list of Tmux
commands, type: Ctrl-b ?
That means press Control and b together, release, then press ? . Tmux should display:
https://www.perl.com/article/an-introduction-to-tmux/ 2/11
13/11/24, 16:00 An introduction to Tmux
You can also change the prefix combination (see the Config options section). If you do that, remember to
use your prefix combination instead of Ctrl-b in the examples below.
Window control
Let’s look at a key Tmux feature: windows. They’re similar to tabs in browsers. Each one is a different
terminal from where you can run different commands at the same time. To create a new window
press Ctrl-b c . You can cycle between windows: Ctrl-b n for the next window and Ctrl-b p takes
you to the prior window. Ctrl-b w lists all windows in a session and lets you select which one to make
active (using the arrow keys and enter).
If you know the window number you can also jump straight to it with Ctrl-b # replacing “#” with the
window number. By default they begin at 0, not 1!
You might be wondering what’s the benefit of using Tmux windows over tabbed terminals. First, with
regular terminals if the window manager crashes, you’ll lose the terminals as well. This won’t happen
with Tmux: it will keep the terminals running in the background and you can re-attach a new terminal to
them at any time. Windows can also be subdivided into panes, all running pseudo-terminals. Let’s look
at them now.
https://www.perl.com/article/an-introduction-to-tmux/ 3/11
13/11/24, 16:00 An introduction to Tmux
Pane control
Panes are great. You can split a window horizontally, vertically and with any dimensions you like. Have
you ever wanted to quickly look up a man page whilst coding? Instead of dropping back to the terminal,
looking up the man page and then foregrounding your editor, just open a new vertical pane, like this:
Now you can read the man page and code at the same time; you can even copy and paste between the
two panes. Much more convenient!
Recently I was processing a huge set of data; I arranged my Tmux window with 3 panes running the data
processing and 1 pane monitoring the server resources with htop (http://hisham.hm/htop/). This server
is almost overloaded:
https://www.perl.com/article/an-introduction-to-tmux/ 4/11
13/11/24, 16:00 An introduction to Tmux
The arrows ←↑→↓ represent the arrow keys, just use one of these. For example to jump to a pane on the
right, you’d press Ctrl-b → . The change pane size controls are a little different. To make that work you
first have to have more than one pane. Next press Ctrl-b and keep the control key held down. Now
you can repeatedly press an arrow key to change the pane size.
Ctrl-b ! is one of my favorite features. It pops the current pane out of the window and moves it to its
own window. This is wonderful if you find yourself doing some unrelated activity in one pane and want
to re-organize your setup.
Pressing esc will exit scroll mode. You should know you’re in scroll mode because an orange line count
appears in the top-right corner of the pane.
Once you’re in scroll mode, you can move the cursor using the arrow keys and page up and down. By
default Tmux doesn’t retain much history, but you can change that (see the Config options section).
You can copy and paste in scroll mode. This is useful when you have split screens as a regular highlight
and copy using the mouse won’t work across vertically split panes.
https://www.perl.com/article/an-introduction-to-tmux/ 5/11
13/11/24, 16:00 An introduction to Tmux
To copy, position the cursor where you want to start copying. Press Ctrl-space to begin highlighting
the text to copy. Press Alt-w to copy the highlighted text. Pressing Ctrl-b ] will paste the copied text.
There are ways to make copy and paste easier: Tmux has a “vim like” copy mode (see the Config options
section and the man (http://man.openbsd.org/OpenBSD-current/man1/tmux.1) page for details).
Shortcut You can jump into scroll mode and page up in one fell swoop with Ctrl-b PgUp (thanks to
Ludovic Tolhurst for the tip).
Bonus feature - search
If you took the time to learn scroll mode, you deserve something extra, something special. Here is your
prize: you can search the Tmux buffer! Just enter scroll mode with Ctrl-b [ and then press Ctrl-r .
Type your search text and press enter. Tmux will jump to the last match it finds. You can press n to
jump to the next match or Shift-n to jump back one match.
There is also Ctrl-s to search down the buffer, but I hardly ever use it.
Session control
Sessions are one of the most useful features of Tmux. They let you group multiple terminal processes
into a single Tmux session which can be worked on (attached), put into the background (detached) and
discarded as you see fit. Programmers will often have different sessions for different projects. Because
Tmux operates under a client-server architecture, even if the original terminal that started Tmux dies or
your desktop GUI crashes, the Tmux session will be preserved, along with all of the terminal sessions in
it.
Detach your Tmux session with Ctrl-b d . This will return you to a regular terminal prompt.
$ tmux ls
0: 1 windows (created Thu Jan 28 08:15:20 2016) [190x50] (attached)
2: 2 windows (created Thu Jan 28 09:11:59 2016) [190x50]
https://www.perl.com/article/an-introduction-to-tmux/ 6/11
13/11/24, 16:00 An introduction to Tmux
This shows that I have two Tmux sessions running, one of which is attached to a terminal window
already. To attach to a session just use the attach command at the terminal prompt:
$ tmux attach
By default Tmux attaches to the next unattached session (“2”) in this case. If I have many different
sessions and want to attach to a particular one, I can specify it with -t for target:
$ tmux attach -t 2
Config options
The file ~/.tmux.conf is a plaintext file used by Tmux for local config. This is what mine looks like:
Tmux starts with a tiny scroll history, so I like to bump that up. The modern encoding and colors are
there to jive with my terminal setup - they may even be the default Tmux settings by now. By default
Tmux uses a login shell, so every new pane will execute .bash_profile . I prefer to disable that behavior
and just launch regular non-login shells.
Tmux has hundreds more options: many users will switch to a different shell like zsh, enable pane
switching with the trackpad, display custom data in the Tmux footer (like weather info!) and so on.
Copying other programmers’ conf files (https://github.com/search?utf8=%E2%9C%93&q=.tmux.conf) is a
great way to learn and experiment.
To reload your .tmux.conf within a Tmux session, type: Ctrl-b : then source-file ~/.tmux.conf .
Tmux resources
The official Tmux website (https://tmux.github.io/) is a good starting point with summary information, a
changelog, downloads and a link to the extensive man page (http://man.openbsd.org/OpenBSD-
current/man1/tmux.1). The Arch Linux tmux documentation (https://wiki.archlinux.org/index.php/Tmux)
covers advanced features and troubleshooting tips. The book tmux - Productive Mouse-Free
Development (https://pragprog.com/book/bhtmux/tmux) by Pragmatic Bookshelf is thorough
introduction to Tmux.
https://www.perl.com/article/an-introduction-to-tmux/ 7/11
13/11/24, 16:00 An introduction to Tmux
tooling (/categories/tooling) command-line (/tags/command-line) multiplexer (/tags/multiplexer) screen (/tags/screen)
sysadmin (/tags/sysadmin) terminal (/tags/terminal) terminator (/tags/terminator) tmux (/tags/tmux)
(/authors/david-farrell/)
David Farrell
(/authors/david-farrell/)
David is a professional programmer who regularly tweets (https://twitter.com/perltricks) and blogs
(https://blog.dnmfarrell.com) about code and the art of programming.
Browse their articles (/authors/david-farrell/)
https://www.perl.com/article/an-introduction-to-tmux/ 8/11
13/11/24, 16:00 An introduction to Tmux
Feedback
Something wrong with this article? Help us out by opening an issue or pull request on GitHub
(https://github.com/perladvent/perldotcom/blob/master/content/article/an-introduction-to-tmux.md)
https://www.perl.com/article/an-introduction-to-tmux/ 9/11
13/11/24, 16:00 An introduction to Tmux
View on X
2024: James Green (jkg) - I Adopted a Dist, and This is What Happened
(https://www.youtube.com/watch?v=1o9T34hBPUU)
8 puntos | comentar (https://www.reddit.com/r/perl/comments/1gpjmu1/lpw_2024_james_green_jkg_i_adopted_a_dist_and/)
(https://www.reddit.com/r/perl/comments/1gp86sm/repeated_keyboardbarcode_scanner_console_input/) Repeated
Keyboard/Barcode Scanner Console Input
(https://www.reddit.com/r/perl/comments/1gp86sm/repeated_keyboardbarcode_scanner_console_input/)
6 puntos | 3 comentarios (https://www.reddit.com/r/perl/comments/1gp86sm/repeated_keyboardbarcode_scanner_console_input/)
2024: Rolf Langsdorf (LanX) - Hold My Place - Automating SQL placeholders by de-interpolating strings
(https://www.youtube.com/watch?v=EhFrANyGbsY)
8 puntos | comentar (https://www.reddit.com/r/perl/comments/1gor3au/lpw_2024_rolf_langsdorf_lanx_hold_my_place/)
(https://www.reddit.com/r/perl/comments/1gnzn0c/lpw_2024_dave_lambley_cloudy_perl_how_it_looks_now/) conferences
LPW 2024: Dave Lambley - Cloudy Perl, how it looks now (https://www.youtube.com/watch?
v=80ZL3hacZAs)
18 puntos | comentar (https://www.reddit.com/r/perl/comments/1gnzn0c/lpw_2024_dave_lambley_cloudy_perl_how_it_looks_now/)
(https://www.reddit.com/r/perl/comments/1goax8d/plenvperlbuild_from_homebrew_have_stopped_working/) plenv/perl-
build (from Homebrew) have stopped working
https://www.perl.com/article/an-introduction-to-tmux/ 10/11
13/11/24, 16:00 An introduction to Tmux
(https://www.reddit.com/r/perl/comments/1goax8d/plenvperlbuild_from_homebrew_have_stopped_working/)
2 puntos | 3 comentarios (https://www.reddit.com/r/perl/comments/1goax8d/plenvperlbuild_from_homebrew_have_stopped_working/)
Site Map
Home (/)
About (/about)
Authors (/authors)
Categories (/categories)
Tags (/tags)
Contact Us
To get in touch, send an email to [email protected], or submit an issue to tpf/perldotcom (https://github.com/tpf/perldotcom/issues) on
GitHub.
(https://github.com/perladvent/perldotcom)
License
This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License (https://creativecommons.org/licenses/by-
nc/3.0/).
(https://creativecommons.org/licenses/by-nc/3.0/)
Legal
Perl.com and the authors make no representations with respect to the accuracy or completeness of the contents of all work on this website and
specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. The information published on this
website may not be suitable for every situation. All work on this website is provided with the understanding that Perl.com and the authors are not
engaged in rendering professional services. Neither Perl.com nor the authors shall be liable for damages arising herefrom.
https://www.perl.com/article/an-introduction-to-tmux/ 11/11