Tmux is an open-source terminal multiplexr for Unix-like operating systems. It allows multiple terminal sessions to be accessed simultaneously in a single window.

Intorduction

When you program on the Unix-like terminal, you might not feel comfortable to mess around with multi-windows, especially more than 5 windows. It is quite difficult to operate with multi-windows. In this situation, we can use tmux terminal multiplexer to allows us to access multiple terminal sessions simultaneously.

In this short post, I am sorting out some basic concept, related configuration and commands for Tmux and how to use a Tmux. I hope these informations will be helpful to anyone who is interested in Tmux. :)

Prerequistes

Make sure your Unix-like terminal have been installed Tmux. Currently, the latest release version of tmux is 3.2a and could run on Linux, OS X and Solars, etc.

Install Tmux From GitHub Version Control

1
2
3
4
5
6
# Clone Tmux Repo:
$ git clone https://github.com/tmux/tmux.git
# Auto Build Tmux Source Code and Installation:
$ cd tmux
$ sh autogen.sh
$ ./configure & make

The Concept of Tmux

Tmux has sessions, windows, and panes. The hierarchy is that Tmux could have multiple sesions, a session could have multiple windows, a window could have multiple panes. User could follow certain conventions or rules to customize Tmux on your Unix-like terminal. For example, we could create one session for lanuching the long-running process. In this session, we could create multiple windows, and each window would be used for each specific task for the long-running process. In the windows, in order to improve to be more efficient, we could also create muiltiple panes for purpose such as editting the relevant configurations and launching the process, and process mointoring.

Here is the window with three panes operating on Ubuntu:

Tmux Cheatsheet and Shortcuts

There are three ways to issue commands to tmux:

  • Command-line: directly enter the command line that prefaced by tmux within tmux session
  • Shortcuts: use prefix key, which is CTRL-b by default and then interpret the keystroke following the prefix as a tmux shortcut
  • Command-mode: enter command mode by pressing the prefix followed by :

Common Tmux Command-Line

To start the Tmux, you could simply type the following commands in your console.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Create a new session:
$ tmux new -s <SessionName>

List sessions:
$ tmux ls

Kill session:
$ tmux kill-session -t <SessionName>

Kill all sessions but keep the current:
$ tmux kill-session -a

Attach to a specific session:
$ tmux attach -t <SessionName>

Attach the last session:
$ tmux attach
$ tmux a

Detach from the session:
$ tmux detach
$ tmux -d

For example, to get a list of all tmux sessions that you created before, you could just type "$ tmux ls" and check all the session name, when to be created, and how many windows be created in the session.

Common Tmux Shortcuts

In tmux session, hit the prefix "CTRL+b". Tmux will interpret the keystroke following the prefix as a tmux shortcut. You could simply type following keystrokes.

Function Shortcut
Display an interactive session list Prefix + s
Switch to the previous session Prefix + (
Switch to the next session Prefix + )
Detach from the current session prefix + d
Rename a session in tmux prefix + $
Create a new window Prefix + c
Rename a window Prefix + ,
Split windows horizontally Prefix + %
Split windows vertically Prefix + "
Zoom in on the active pane Prefix + z
Switch to another pane Prefix + arrow key
Force to kill an unresponsive process in a pane Prefix + x
Switch to another layout Prefix + space
Move the pane out of current window Prefix + !
Swap current pane with the page from the left Prefix + {
Swap current pane with the page from the right Prefix + }
List shortcuts Prefix + ?

Common Tmux Command-Mode

In tmux session, hit the prefix "CTRL+b" and also follows with :. Tmux will open a command prompt at the bottom of the screen, which accept tmux commands. You could simply type following tmux commands.

Function Tmux Command
Start new session with the name new-session -s SessionName
Display an interactive window list list-windows
Split windows horizontally split-window -h
Split windows vertically split-window -v
Synchronize all the panes in a window setw synchronize-panes

You may find and learn more information from OpenBSD: tmux

Tmux Configuration

The basic tmux could be configured to personalize the tmux experience that supercharges your tmux and builds comfortable terminal environment. I'm sharing my simple .tmux.conf file as template. These uses remapping, mouse-mode, and bind command, etc. You can try this as your .tmux.conf after backing up yours or as the reference. The configuration file should be located under the home directory.

.tmux.conf
# reload config file (change file location to your the tmux.conf you want to use)
bind r source-file ~/.tmux.conf

# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-q
bind-key C-q send-prefix

# split panes using | and - 
unbind '"'
unbind %
bind | split-window -h 
bind - split-window -v 

# switch panes using Alt-arrow without prefix
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# Enable mouse mode (tmux 2.1 and above)
set -g mouse on

# Shift arrow to switch windows
bind -n S-Left  previous-window
bind -n S-Right next-window

# Use Alt-arrow keys without prefix key to switch panes
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# Copy-Paste
setw -g mode-keys vi

#  modes
setw -g clock-mode-colour colour5
setw -g mode-style 'fg=colour1 bg=colour18 bold'

# panes
set -g pane-border-style 'fg=colour19 bg=colour0'
set -g pane-active-border-style 'bg=colour0 fg=colour9'

# statusbar
set -g status-position bottom
set -g status-justify left
set -g status-left ''
set -g status-right '#[fg=colour233,bg=colour10] %m/%d/%y #[fg=colour230,bg=colour8] %H:%M:%S '
set -g status-right-length 50
set -g status-left-length 20

# No delay for escape key press
set -sg escape-time 0

# Activity Monitoring
setw -g monitor-activity on
set -g visual-activity on

You may find and learn more information from OpenBSD: tmux

Tmux Plugins and Tools

Like Vim, to add new tmux plugins, we can either manually install themes to employ a pluging manager. It is recommended to install Tmux Plugin Manager and further add new plugins. There are two tmux plugins that can help with restart tmux which running programs when you shut down your computer: Tmux Resurrect

First, we need to clone TPM repo:

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

Then we configure new plugins to .tmux.conf,
.tmux.conf
#--------------
# Install Tmux plugins
#--------------
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run -b '~/.tmux/plugins/tpm/tpm'
...
Finally, reloading tmux config to install the plugins by Command-Mode (:source-file ~/.tmux.conf) or Shortcut (Prefix + I). If the installation of plugin was successful, we would see the following information.
display statement
TMUX environment reloaded.
Done, press ENTER to continue.

Save and Restore Tmux Environment

Tmux Resurrect supports to save and restore all the details from your tmux environment. The shortcut of saving is Prefix + Ctrl + s and the shortcut of restoring is Prefix + Ctrl + r.

You can find more information about this feature on Tmux Resurrect.

=========== To be continued… ==========

Reference

Feel free to leave the comments below or email to me. Any other pieces of advice are always welcome. :)