Tmux is a terminal multiplexer which is typically used as a window manager. It organizes multiple sessions into windows, and within each window you can have multiple panes, each running some program.

Among many other uses, I typically use tmux for two reasons:

  1. Keeping multiple sessions open, typically one per physical host (one session for my laptop, one session for my home server, one session for my AWS server, …)
  2. Long-running background tasks on a server that I may lose connection from. Because tmux keeps running sessions in the background even when you detach from it, it’s a nice wrapper for kicking off a multi-hour compile job and disconnecting from your server.

Hierarchy

  • Background process: server
    • Outermost layer: sessions
      • Inner: windows
        • In each window: panes

Configuration

Tmux supports RC-like configuration files.

# file: ~/.tmux.conf

# change escape key from control-B to control-A
unbind-key C-a
set -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix

# enable pointer click support
set -g mouse on

# set the terminal scrollback length (in lines)
set -g history-limit 10000

# milliseconds error messages are displayed for
set -g display-time 4000

# how often the status bar updates (seconds)
set -g status-interval 5

# terminal selection
set -g default-terminal "screen-256color"

# ... i don't remember
set -g focus-events on
setw -g aggressive-resize on

set -g status-right-length 80

Plugins

Tmux Plugin Manager is a plugin manager for Tmux which is straightforward to install and configure.

See also

Installation (also see the project README):

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

Enabling plugins is straightforward, only adding to the .tmux.conf file:

# ... previous .tmux.conf contents

set -g @plugin 'tmux-plugins/tpm'

# enable plugins here.  for example:
set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'briansalehi/tmux-acpi'
set -g @plugin 'tmux-plugins/tmux-yank'

# if you need to configure any plugins, do so here
set -g @acpi_icon_thermal_cold 'C '
set -g @acpi_icon_thermal_hot 'H '
set -g @acpi_icon_battery_full 'F '
set -g @acpi_icon_battery_low 'L '
set -g status-right 'Bat: #{acpi_battery_percentage} | #{cpu_bg_color} CPU: #{cpu_percentage} #{acpi_thermal_status} | %a %Y-%m-%d %H:%M:%S '

# start TPM
run '~/.tmux/plugins/tpm/tpm'

Scripting and commands

Tmux can be scripted via Bash (or any other scripting language) by simply sending commands to the tmux server. For example, this is a setup script I use to open tmux with:

Example warning

You don’t have to organize your sessions like this. It’s just an example.

# file: autostart.sh
#!/bin/bash
 
tmux new-session -d -s 'local' -n 'admin'
 
. shared/Projects/edgegraph/.tmux-startup.sh
 
ssh_session() {
    tmux new-session -d -s "$1" -n 'ssh'
    tmux send-keys -t "=$1:=ssh" "ssh $2" Enter
}
 
ssh_session 'minisrv' '192.168.30.142'
ssh_session 'microsrv' '192.168.30.144'
 
tmux a
# file: shared/Projects/edgegraph/.tmux-startup.sh
#!/bin/bash
 
cd shared/Projects/edgegraph
 
tmux new-session -d -s 'edgegraph' -n 'dev'
tmux send-keys -t '=edgegraph:=dev' 'vim' Enter
tmux send-keys -t '=edgegraph:=dev' ':vert ter' Enter
tmux send-keys -t '=edgegraph:=dev' 'git status' Enter
sleep 0.5
tmux send-keys -t '=edgegraph:=dev' C-w Right ':Ex' Enter
tmux send-keys -t '=edgegraph:=dev' ':Vex' Enter
 
cd ../../..

When the first file is run, it:

  • Creates a new “local” session for whatever purpose
  • Calls a project-specific setup file for working on Edgegraph (which creates its own session, and sets up Vim)
  • SSH’s into a couple servers

and finally attaches to the tmux server.

Moving windows between sessions

:move-window -s ses1:idx -t ses2

where

  • ses1 is the name of the originating session you want to move a window from
  • idx is the name/index of the window within that session you want to move
  • ses2 is the name of the destination session you want to move the window to

Killing stuck processes

  • Windows: prefix + &

Shutting down

tmux kill-server to cleanly exit all (if you are not in any session). pkill -f tmux to uncleanly exit.