10 practical examples of tmux configuration with examples


Tips and Tricks

By now if you have been reading this tutorial, you will realize that some of the Prefix key combinations with tmux are not user friendly. The end user will have to struggle some times to get the key strokes properly. To overcome this problem we will build a basic configuration file for our environment and then use this configuration file across the tutorial

You can also access tmux cheatsheet with 50+ shortcuts commands and key combinations

 

Pre-requisite

I hope you are already aware of the basics of tmux, if not I would recommend to check the follow article before going for the tmux configuration
Complete tutorial on tmux commands with examples from scratch

 

tmux configuration file (tmux.conf)

  • By default, tmux looks for configuration settings in two places.
  • It first looks in /etc/tmux.conf for a system-wide configuration.
  • It then looks for a file called .tmux.conf in the current user’s home directory.
  • If these files don’t exist, tmux simply uses its default settings

Currently we will create user specific tmux configuration file for my root user

# touch ~/.tmux.conf

 

1. Change the Prefix combination

The first thing which we will do is change our Prefix combination. Using ctrl+b is little hard to trigger as the button in the keyboard are quite far already so we will change this to ctrl+a

NOTE:
You can use third party tool use as autohotkey on Windows to map your Capslock key to ctrl so it will be more easier for you to access the Prefix combination

To change the prefix combination add following in your ~/.tmux.conf

# Setting the prefix from C-b to C-a
set -g prefix C-a
  • Here we’re using the -g switch, for "global" which sets the option for all tmux sessions we create.
  • It is not necessary but we can use the unbind-key, or unbind command, to remove a keybinding that’s been defined, so we can assign a different command to this key later.
  • Let’s free up Ctrl-b like this in our ~/.tmux.conf:
# Free the original Ctrl-b prefix keybinding
unbind C-b

Changes to the file aren’t read by tmux automatically.

So if you’re editing your .tmux.conf file while tmux is running, you’ll either need to completely close all tmux sessions, or enter tmux’s Command mode with Prefix : and type this whenever you make a change: source-file ~/.tmux.conf
Now for any new sessions you can use Ctrl+a as your new Prefix combination

 

2. Change the default delay

The default delay value between Prefix combination and command is very less and sometimes doesn't work so we can increase the delay value

To change the default delay value we add following in our ~/.tmux.conf file

#setting the delay between prefix and command
set -s escape-time 5

Now for all the new tmux sessions you will get 5 seconds to enter the command key stroke after hitting the Prefix

 

3. Set windows and panes index

By default we know all the windows or panes start with index 0. So you can define your own index value which would be considered as the base every time you open a new window or pane in a tmux session

To define a custom index value for new windows add following in ~/.tmux.conf file

# Set the base index for windows to 1 instead of 0
set -g base-index 1

To define a custom index value for new panes add following in ~/.tmux.conf file

# Set the base index for panes to 1 instead of 0
set -g pane-base-index 1

Now you can start a new session and verify the index base value

 

4. Create shortcut to reload configuration file

Every time you modify your tmux configuration file, you must either

  • shutdown all tmux sessions and restart them
  • Use source-file ~/.tmux.conf on all the sessions

to reload the configuration file

Let’s create a custom keybinding to reload the configuration file. The bind command defines a new keybinding. You specify the key you want to use, followed by the command you want to perform.

Let’s define Prefix r so it reloads the .tmux.conf file in the current session. Add this line to your ~/.tmux.conf file.
HINT: When you reload the file, you might not always be able to tell that anything changed, but you can use the display command to put a message in the status line.

# Set bind key to reload configuration file
bind r source-file ~/.tmux.conf \; display ​"Reloaded!"

Now you can just use Prefix with r key to reload the configuration file and the status bar should display as "Reloaded!" so you know that new configuration file changes has been applied

 

5. Splitting Panes

The default combination of Prefix % is very difficult to hit because it involves 4 keystrokes Ctrl+a Fun+5 so we can define a cstom bind combination to split the panes.
We’ll set the horizontal split to Prefix | and the vertical split to Prefix -

# splitting panes with | and -
bind | split-window -h
bind - split-window -v

 

6. Enable Mouse Support

  • We know that tmux is completely keyboard driven but at times mouse usage can come handy for easier movements.
  • Sometimes it’s nice to be able to scroll up through the terminal buffer with the mouse wheel, or to select windows and panes, especially when you’re just getting started with tmux. To configure tmux so we can use the mouse, we need to enable mouse mode.
# Enable mouse support
set -g mouse on

This setting configures tmux so it will let us use the mouse to select a pane or resize a pane, let us click the window list to select a window, or even let us use the mouse to scroll backwards through the buffer if your terminal supports it.

 

7. Change default editor

The status-key option lets us modify how we move our cursor around while typing within the tmux command prompt. The default for tmux is to use the Emacs mode keys, so if you are an Emacs user, you may be all set. tmux also tries to help out and might, based on environment variables, switch to one group or the other by default.

To switch to vi editor you can use

# Set vi as the default editor
set -g status-keys vi

 

8. Change status bar background and foreground color

The default color of the status bar is a shade of green. We can change the default background and foreground of the status bar using

# set the status line's colors
set -g status-style fg=white,bg=blue

HINT: Some terminals support the full xterm palette of 256 colors, some only support 16, and some don't support any colors. Most have their own flavor of colors due to terminal color themes, so what you specify as blue may not be rendered on the screen as blue at all.

 

9. Highlighting active window

When you have multiple windows, by default we can identify the active window by checking the asterisk sign. We can also assign a custom background colour for the active window which can help us determine easily:

# Set different background color for active window
set -g window-status-current-bg magenta

 

10. Show available Options

Now there are bunch of options which you can configure with tmux as per your requirement and to ease your life. To get the list of supported options your need to start a tmux session

# tmux

Next detach the session with Prefix d. Now on the terminal you can use different commands to view the supported options:

To get list of global options

# tmux show-options -g

To get the list of window options

# tmux show-options -w

To get the list of server options

# tmux show-options -s

So you can add your custom values and bind keys in the tmux.conf configuration file.

 

Conclusion

In this tutorial we learned about tmux configuration options, I have only covered limited options but there are vast number of possible configuration changes such as display related, colors, etc. All these options can be checked using show-options.

Lastly I hope the steps from the article to configure tmux on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

References

I have used below external references for this tutorial guide
learning Tmux

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

2 thoughts on “10 practical examples of tmux configuration with examples”

Leave a Comment