Better Tmux

Feb. 5, 2022

Brief Overview

Tmux is a teriminal multiplexer for Linux systems. It allows you to have multiple terminal sessions and panes all in one terminal window. This improves productivity and speed when multitasking on the command line. Tmux is very powerful, but its keybindings aren’t very intuitive. In this post I’ll be going through the steps I made to adjust my tmux configuration to something more usable.

The tmux configuration file

There is a default tmux configuration file located in /usr/share/tmux, but we can override this file by creating a new one in our home directory:

$ touch ~/.tmux.conf

Now that we created that file, all of the changes I mention below will go in that file.

Better Prefix

The prefix in tmux is a keyboard shortcut which tells tmux that the following keystroke should be interpreted as a command (more on that later). The default prefix for tmux is CTRL + b, which is not only strange, but hard to press with one hand as well. Instead of this, I switched it to CTRL + Space which made it much easier to press. You can add that to your configuration file as shown below:

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

Note that we must first unbind the original key before assigning a new one

Better Window Splitting

In tmux, you can split your current terminal window pane vertically and horizontally. By default, tmux uses % and " to split panes, which isn’t very intuitive. I changed these keybindings to the dash and pipe, which makes a little more sense. Add these lines to your .tmux.conf:

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

Better Window and Session Renaming

Tmux allows you to rename terminal windows and even the entire tmux session itself. Tmux uses $ and , to rename sessions and windows, respectively. I changed these to r for windows and R for sessions:

unbind $
unbind ,
bind r command-prompt -p "Rename Window: " "rename-window '%%'"
bind R command-prompt -p "Rename Session:" "rename-session '%%'"

Easier Window Switching

Normally, switching the active window pane requires you to use the prefix keybinding and then pressing an arrow key, but this can get annoying if you need to use the arrows several times. I changed this to just holding down the ALT key and using the arrows:

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

Improved Copy/Paste

When using tmux, I ran into several issues when using the copy/paste ability. The settings below will allow you to copy and paste from your system clipboard with ease. To enter copy/paste mode, use CTRL + Space, then [. From here, you can use the arrow keys to move your cursor to reach the beginning of what you want to copy. Then, press Space to begin highlighting the text you want to copy. Move the arrow keys to select the desired text. Finally, press Enter to copy the selected text.

set -g mouse on #Allows you to scroll up through terminal output with your mouse wheel
set -g set-clipboard on
setw -g mode-keys vi
bind -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xsel -i --clipboard"

Conclusion

There are plenty of other features that can be changed in tmux, but many of it’s features might not be used day-to-day. I showed the settings you’d most likely want to change as you would use these the most. I may add to this post if I find anything useful. Enjoy!