Skip to content

Tmux

This article briefly introduces tmux, and provides a recommended startup config to make tmux easier to use.

Why Tmux (Terminal Multiplexer)?

Linux is a pretty popular server solution. It is used by many tech giants to build its backend data centers, and also small businesses as their backend infra.

To interact with Linux, developers typically use terminal and ssh. They use them to issue command to control the Linux server.

It all sounds pretty sweet until developers started to work on more complicated tasks, e.g., developing large scale softwares. There are two major problems:

  • In each ssh session, you are only allowed to run one single foreground program at any time.
  • If your ssh session is down, you lose the foreground program in that session.

This is definitely not desirable. When doing software development work, we always need to have multiple tasks going on at the same time, e.g., editing code, version control, data base operations, running tests. However, in a ssh session, you are only allowed to run one single foreground program. What make it worse is that it could be lost anytime if you ssh session is reset.

Tmux perfectly addressed the above issues by introducing the following design:

Tmux Design

In this design, instead of running in the ssh session, the programs on the server side (e.g., editors, shells) runs within the tmux session. When the session is reset or lost, none of the programs will be affected. Also, multiple programs can run within the same tmux session.

Installation

Installing tmux is easy. On Linux, you can do:

sudo apt update && sudo apt install tmux

On mac, you can do:

brew install tmux

Tmux - Cheatsheet

CommandDescription
tmuxStart tmux
tmux lsDisplay sessions
tmux a /attach-sessionRestore tmux session
tmux a -t mysessionAttach to a session with the name mysession
tmux attach -dDetach an already attached session
tmux new -s mysessionStart a new session with name mysession
tmux kill-ses -t mysessionKill session with name mysession
tmux kill-ses -aKill all session but the current
tmux new -s mysession -n mywindowStart session with mysession and window mywindow

Sessions

CommandDescription
Ctrl-b
-s mysession
Start a new session with name mysession
Ctrl-b
Kill current session
Ctrl-b dDetach from tmux
Ctrl-b $Rename session
Ctrl-b sSwitch session
Ctrl-b wSession and Window Preview
Ctrl-b sShow all session
Ctrl-b (Move to previous session
Ctrl-b )Move to next session

Windows

Windows are basically tabs in tmux session. They are listed on the bottom of the screen by default. They are extremely useful, because they provide an elegant way to manage all your programs.

COMMANDSDESCRIPTION
Ctrl-b cCreate a window
Ctrl-b ,Rename Window
Ctrl-b &Close current window
Ctrl-b [0-9]Switch between windows using numeric index
Ctrl-b ArrowsSwitch between windows using arrow keys
Ctrl-b wSession and Window Preview
Ctrl-b nGo to the next window
Ctrl-b pGo to the previous window
Ctrl-b lToggle last active window
Ctrl-b
-s [0-9] -t [0-9]
Swap windows

Panes

Panes allow you to split a single window into multiple areas, where each area can run a separate program. This is helpful if you prefer to see multiple programs running in the same screen.

By default, each window has one pane by default. You can split each pane vertically or horizontally.

COMMANDSDESCRIPTION
Ctrl-b xClose the current pane
Ctrl-b ;Toggle last active pane
Ctrl-b zMaximize the current pane (toggle zoom)
Ctrl-b %Split windows horizontally
Ctrl-b ”Split windows vertically”
Ctrl-b {Move the current pane left
Ctrl-b }Move the current pane right
Ctrl-b spaceToggle between pane layouts
Ctrl-b Ctrl-arrow keysResize panes
Ctrl-b arrow keysSwitch between panes (left, right, up, down)
Ctrl-b !Convert pane to a window
Ctrl-b qShow pane numbers
Ctrl-b q [0-9]Select pane by number

General Commands

COMMANDSDESCRIPTION
Ctrl-b ?List keys bindings
Ctrl+b
List keys bindings
tmux list-keysList keys bindings
tmux infoShow every session, window, pane
Ctrl-b PageUp/PageDownTo scroll in a window
Ctrl-b : source-file /path/to/fileTo reload the configuration file
tmux -S /tmp/your_shared_session chmod 777 /tmp/your_shared_sessionTo start a shared session

Copy Mode

Key BindingDescription
Ctrl-b [Enter copy mode
qExit copy mode
Arrow Up/DownScroll up or down in copy mode
SpaceStart selection
EnterCopy selected text
Ctrl-b ]Paste copied text
Ctrl-b PgUp/PgDownScroll through the scrollback buffer
Ctrl-u/Ctrl-dScroll a half page up or down
Ctrl-b =List all windows and their current sizes
Ctrl-b
-U/D/L/R [N]
Resize the pane Up/Down/Left/Right by N lines or cells
Ctrl-b ]Search forward in the scrollback buffer (press again for next match)
Ctrl-rEnter search mode to search backward in the scrollback buffer
H/J/K/LMove the cursor left/down/up/right in copy mode (vim-like navigation)

Here’s a recommended config for tmux to make it easier to use. Source: Dreams of Code

set-option -sa terminal-overrides ",xterm*:Tc"
set -g default-terminal "xterm-256color"

# Change Prefix
unbind C-b
set-option -g prefix C-Space
bind-key C-Space send-prefix

set -g mouse on

# Set the base index for windows to 1
set -g base-index 1
set -g pane-base-index 1
set-window-option -g pane-base-index 1
set-option -g renumber-windows on

# Set the limit for command history
set-option -g history-limit 5000

# Bind | to split the window horizontally at the current path
bind | split-window -hc "#{pane_current_path}"

# Bind - to split the window vertically at the current path
bind - split-window -vc "#{pane_current_path}"

# Bind C-c to save the tmux buffer to the clipboard
bind C-c run "tmux save-buffer - | xclip -i -sel clip"

# Bind C-v to paste the clipboard into the tmux buffer
bind C-v run "tmux set-buffer $(xclip -o -sel clip); tmux paste-buffer"

# Bind r to reload the tmux configuration and display a message
bind r source-file ~/.config/tmux/tmux.conf \; display "Reloaded!"

bind -n M-H previous-window
bind -n M-L next-window

set-window-option -g mode-keys vi

bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi C-v send-keys -X rectangle-toggle
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel

bind '"' split-window -v -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"


# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'christoomey/vim-tmux-navigator'
set -g @plugin 'catppuccin/tmux'
set -g @plugin 'tmux-plugins/tmux-yank'

run '~/.tmux/plugins/tpm/tpm'