Customize TMUX To Use It Effectively!

Bhavik Nahar
6 min readSep 24, 2023

This article will go through customizing Tmux so you can use it effectively. It will go through detailed steps on how to change the bind-keys, how to switch windows/panes by just clicking into them, how to set up tmux logging to save the log in the same directory, and much more.

Hi, I’m Bhavik! I have been working as an Application Security Analyst for 2 years. Over these years, I have come up with some customized preferences of my own, and I would love to share it with you.

Introduction

Tmux is an open source Terminal Multiplexer. It allows multiple sessions in a single window. Due to the vast majority of plugins and ease of access, Tmux has become quite popular.

I started using Tmux after a friend of mine suggested me to use it. I have been using Tmux for almost 3 years now and it has made my life so easy. I will not be switching to any other terminal multiplexer any time soon.

Diving Deeper!

Let’s dive into customizing our Tmux. Tmux comes pre-installed in most Linux distributions, however if not, it can be installed using the following command.

┌──(bufferedsec㉿kali)-[~]
└─$ sudo apt install tmux

After installing tmux, we need to setup the tmux configuration file. This file has to be placed in the home [~] directory and must be named “.tmux.conf”. Let’s start editing this file.

Changing the Key Binds

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

By default the Tmux prefix is CTRL + B. Prefix means a combination of keys to use before any action can be performed. For example, to open a new window, the combination is [Prefix (CTRL + B) + C]. However, it feels weird to use the default key-bind. Hence, I changed my prefix to [CTRL + A]. The above 3 lines will set the prefix key-bind to [CTRL + A] and unbind the default prefix [CTRL + B].

Quality of Life

set -g history-limit 9999999
set -g mouse on
unbind -n MouseDrag1Pane

The default Tmux history/scrollback buffer is 2000. That means, if any command has more than 2000 lines of output, it would be chopped off at the last 2000 lines. Therefore, we increase the tmux scrollback to a large number so we don’t lose any terminal output.

I really hate when you scroll up on the mouse in a tmux session, it scrolls through the commands history (commands ran) and not the buffer. Therefore, in the above code, we switch off the command history on scrolling up. Now on scroll up, we can scroll through the terminal output.

As a side benefit, this also sets the mouse so we can click into any panes or windows and switch to them, without needing to use the Prefix and window number or arrow keys.

Leveraging Mouse Click to Switch Between Panes/Windows

VIM Key-Binds

set-window-option -g mode-keys vi

I use VIM as my terminal editor. Being used to it, having the tmux key binds same helps me a lot. After changing this, we can search through the tmux history by using VIM key binds.

To search through the terminal output we can use the same VIM keys — ? for searching above and / for searching below. In Tmux, enter the scroll mode by sending Prefix + [ and then just type ? and the tmux pane will change to include an input field. Type your search term and click enter. Now you can search through terminal output with no issues. Similarly, for searching down, use the Prefix + /.

Searching Through Terminal Output

Tmux Plugins

Tmux offers a lot of plugins. Plugins can be installed manually or by using the Tmux Plugin Manager. As per the installation instructions, we clone the repository in the specified location and edit our tmux configuration file accordingly.

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

After running the above command, we will see the tmux plugin manager in the destination directory. Now we edit our tmux configuration file to include the tpm.

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


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

The first 2 lines denote what plugins we want to use and the last line is to initialize tmux plugin manager. We source the TPM by typing the following in bash.

tmux source ~/.tmux.conf # Sourcing the Tmux Plugin Manager

Our Tmux Plugin Manager is set!

Moving onto tmux logging, we install tmux logging by adding the plugin name in the .tmux.conf file. We add the following lines right after the TPM.

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

set -g @logging-path '#{pane_current_path}'
set -g @save-complete-history-path '#{pane_current_path}'
set -g @screen-capture-path '#{pane_current_path}'

The first line denotes the Tmux-logging plugin. The next 3 lines are for saving the tmux logging files in the current working directory. By default, tmux-logging saves the log files in the home directory. This can become very clobbered if using multiple panes in different paths. Using the above configuration, the log files will be saved in the same directory.

Now open Tmux session by typing the following.

tmux new -s <NAME>

Since, we have only setup the plugin configuration, we will run the following command to install the plugins.

Prefix (CTRL+A) + I

TMUX Plugin Manager Fetching the TMUX-Logging Plugin

This will install the plugin and we are ready to go!

Using the Setup!

We can now use the following key-binds in TMUX.

Remember Prefix => [CTRL + A]

  • Prefix + c (Open a new Window)
  • Prefix + (Open a horizontal pane)
  • Prefix + % (Open a vertical pane)
  • Prefix + , (Rename the Window)
  • Mouse Click into any Window or Pane to switch to it.
  • Prefix + [ (Scrollback mode)
    - Click space key to start copying.
    - Click Enter key to save the copied text to TMUX buffer.
    - Click Prefix + ] to paste the recently copied text from TMUX buffer.
    - Click Prefix + = to view the entire TMUX buffer.
    - Click Prefix + ? to search above in the terminal output.
    - Click Prefix + / to search below in the terminal output
  • Prefix + SHIFT + p (Start/Stop logging in the current pane)
  • Prefix + ALT + p (Save visible output on the current pane)
  • Prefix + ALT + SHIFT + P (Save all the output on the current pane)

Final Review!

This is what our .tmux.conf file should look like!

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

set -g history-limit 9999999
set -g mouse on
unbind -n MouseDrag1Pane

set-window-option -g mode-keys vi

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

set -g @logging-path '#{pane_current_path}'
set -g @save-complete-history-path '#{pane_current_path}'
set -g @screen-capture-path '#{pane_current_path}'

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

Conclusion

Everybody has their preference on using different applications that best suit their needs and ease of access. I use tmux in my daily routine as a way to stay organized when working on Linux based distributions.

I hope this blog helps you too! And I appreciate you taking the time to read.

Thank you!

--

--