05. Customizing and Installing Plugins tmux

Now that we have a good understanding of how to manage sessions, windows and panes, let's learn how to customize tmux.

Always use tmux!

A good habit to form is to always have tmux running whenever you open a shell. We can have tmux run upon startup by adding this snippet into our ~/.bash_profile

if [[ ! $TERM =~ screen ]]; then
  tmux attach -t base || tmux new -s base

Editing Your Configuration File

To set tmux's system-wide settings on a Linux system, edit the /etc/tmux.conf file. For more user-specific settings, edit ~/.tmux.conf.

1) Reloading the Config File

To reload the configuration file, type:

$ tmux source-file ~/.tmux.conf

You can also bind the r key to easily reload as a keystroke. Place the following in your ~/.tmux.conf file:

bind r source-file ~/.tmux.conf

2) Changing the prefix key

One of the first things users can do is to change the prefix key from <Ctrl-b> to <Ctrl-a>. Now that we know how to rebind the prefix key, we'll refer to your binding as prefix instead of Ctrl-b.

# Sets the prefix to Ctrl-a
set -g prefix C-a
# Releases binding from Ctrl-b
unbind C-b
bind C-a send-prefix

The -g indicates a global preference, meaning it applies all around tmux.

3) Pane Switching

It could be cumbersome having to use the prefix key every time you want 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

The M is the meta key (oftentimes the alt-key). Now you can simply hold onto alt+[Up/Down/Left/Right] to move among panes.

4) Enabling Mouse Mode

Sometimes you'll miss those GUI mouse-activated controls. We can re-active them using these three mouse modes:

# Enable mouse control
set -g mouse-select-pane on
set -g mouse-resize-pane on
set -g mouse-select-window on

Now you can:

  1. Click into a pane to activate it.
  2. Resize a pen by clicking and dragging the borders.
  3. Select a window by clicking on the tab.

5) Status Line

You can also configure the status line to select what it should display, and whether it should be on or off.

You can set the date with variables from strftime. For example, %c will display the date and time.

set -g status-right "Code Snipcademy %c"
set -g status-bg white
set -g status-fg black
Changing the custom color and status bar is easy as 1,2,3!
Changing the custom color and status bar is easy as 1,2,3!

We have a color choice among black, red, green, yellow, blue, magenta, cyan, white or default.

You can also use the following variable keys for a more dynamic status bar.

Hostname of local host.
Session name.
Current window title.
Literal #.
First line of command's output.

6) Intuitive Pane Splitting

If you're anything like myself, you may find it difficult to remember that % is for splitting horizontally and " is for splitting vertically. We can map the commands to - and | instead.

unbind %
unbind '"'
bind | split-window -h
bind - split-window -v

Installing Plugins

Plugins are a fast and easy way to obtain features without writing tens of lines of code. In fact, most of what we configured above are covered in some plugin. Let's learn how to install the tmux-pain-control plugin as an example. Feel free to browse through the list of tmux plugins for more plugin ideas.

Plugin Manager

To get started, download the tmux Plugin Manager (tpm):

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

You will now be set to install any plugins.

Installing Your First Plugin

Now open ~/.tmux.conf with Vim:

set -g @tpm_plugins '                   \
tmux-plugins/tpm                    \ 
tmux-plugins/tmux-pain-control      \
run-shell '~/.tmux/plugins/tpm/tpm'

To install the plugin, place your cursor over the plugin, then press prefix I. You should get a window pop up like this (except with install statuses):

Installing plugins in tmux.
Installing plugins in tmux.

With this plugin, we can:

  • move along our panes with prefix [h/j/k/l], which is similar to Vim controls.
  • split panes with - and |.
  • resize panes with prefix [H/J/K/L]

Remember that if there's a good nifty shortcut or trick, there's most likely a plugin for it. If you have a good idea for customization, try first looking for a plugin - it'll make your life easier!

What's your favorite configuration or plugin? Share in the comments below!

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources