5 min read

Mastering Tmux: A Professional Guide from Setup to Advanced Usage

Table of Contents

What is Tmux?

Tmux is a terminal multiplexer that allows you to divide your terminal into multiple workspaces, manage remote sessions effectively, and customize the environment for optimal productivity. This guide will take you from a clean slate to a proficient user, covering installation, customization, essential bindings, and advanced session management.

Initial Setup

We’ll begin with a clean Ubuntu container to ensure a consistent experience. First, install Tmux:

sudo apt-get update
sudo apt-get install tmux

To initiate a session, simply type tmux. Tmux uses a prefix key, by default Ctrl+B, to send commands. Pressing the prefix followed by " splits the screen horizontally, while using the prefix and arrow keys allows you to move between panes. Each pane operates as an independent shell process. Close a pane with Ctrl+D or prefix + X. Use prefix + % to split vertically.

Tmux also has a command line, which can be accessed by typing prefix and then :. This prompt supports all prefix commands and more, including window renaming:

:rename-window "My Window Name"

Similarly, you can rename a session using:

:rename-session "My Session Name"

Panes can be zoomed into and out of with the prefix and Z key.

To detach from a session, use tmux detach. Use tmux ls to see currently running sessions. To re-attach to a session use tmux attach followed by the session name, for example tmux attach -t "My Session Name".

Tmux utilizes a hierarchical system of panes within windows, within sessions. Use prefix and the window number to switch between windows. Prefix N or P navigates to the next or previous window, respectively. Sessions can be switched with prefix + ( or ). Prefix + S opens the Tmux session manager.

Customizing Tmux

Tmux configuration is handled via a .tmux.conf file in your home directory or tmux.conf located at ~/.config/tmux/tmux.conf. Let’s create the default option of ~/.tmux.conf.

First, let’s change the prefix to Ctrl+W with the following in the configuration file:

set -g prefix C-w
unbind C-b
bind C-w send-prefix

Apply the changes using:

:source-file ~/.tmux.conf

Now, let’s incorporate the Tmux Plugin Manager (TPM) to manage and install plugins. Clone the repository:

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

Add this line at the bottom of your .tmux.conf to activate TPM:

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

Add the first plugin, tmux-sensible, which provides intuitive bindings. Add this to your .tmux.conf:

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

To install plugins, use prefix + I.

Next let’s address visual appearance by using the Catppuccin plugin which includes a color scheme and status bar design. Add this to your .tmux.conf:

set -g @plugin 'catppuccin/tmux'

Install the plugins with prefix + I and activate the desired flavour:

set -g @catppuccin_flavour 'mocha' # or latte, frappe, macchiato

This is a vast improvement over the default look. It’s a good idea to separate out bindings from the main configuration. You can source a configuration file like reset.tmux by adding the following to the main tmux.conf file:

source-file ~/.config/tmux/reset.tmux

In reset.tmux lets set a few core configuration settings:

set -g base-index 1
setw -g mode-keys vi

To improve copy-pasting, the Tmux Yank plugin is recommended. Add this to your .tmux.conf:

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

Install it using prefix + I.

For session persistence across system restarts, use the resurrect and continuum plugins. Add these lines to your .tmux.conf:

set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-restore 'on'

Install these with prefix + I.

A small visual enhancement is to add a magnifying glass icon next to the window name when the window is zoomed. This can be added conditionally with the Catppuccin plugin.

Enhanced Bindings

Below is an example of useful custom bindings. This guide uses Ctrl+A as the prefix.

  • S: Horizontal split
  • V: Vertical split
  • h/j/k/l: Pane navigation using Vim motions
  • Window numbers: Change windows
  • n/p: Scroll through windows
  • R: Rename a window
  • %: Rename a session

Session Management with SessionX

The included SessionX plugin provides an improved session management experience with fuzzy finding and session previews.

To install SessionX add the following line to your .tmux.conf:

set -g @plugin 'omerxx/tmux-sessionX'

Install via TPM with prefix + I.

SessionX allows:

  • Creating new sessions by name.
  • Deleting existing sessions via Ctrl+D.
  • Renaming existing sessions via Ctrl+R.
  • Jumping to the config at the home directory.

You can configure a hotkey for this plugin using a config line in your .tmux.conf

While SessionX is not a finalized product, it’s a powerful tool that enhances session management by providing previews and quick access to session management features.

Conclusion

This guide provides a professional approach to mastering Tmux. With this foundation, you can explore Tmux’s vibrant community for further customization and plugins that fit your specific needs. Remember, efficiency and productivity are paramount, and mastering Tmux will elevate your workflow.