Making tmux suck less
Tags: tooling, tutorial, linux
Published: 2022-07-16 15:12:20

tmux [is a great tool](https://dev.to/dizzyspi/the-argument-for-terminal-multiplexers). But it's basically unusable out of the box. Try it - fire it up in a bare environment without a tmux config file. You'll have no colors. You'll have weird keybindings issues. The vim clipboard will be broken. Copying text with your mouse will be broken (unless you really _did_ want to copy random bits of text from all of your panes, and not just the active one). In short, you'll have a terrible user experience.

I foolishly thought tmux would inherit all of the niceties of my base terminal emulator. As it turns out, tmux behaves a lot more like a standalone terminal than an add-on to your existing one. I can respect a tool that assumes nothing about its use, but it does mean you have to do some configuring to make it work well for you. Here's what I did:

## 1. Enabling colors

Add this to your .tmux.conf

```bash
set -g default-terminal "xterm-256color"
```

## 2. Fixing weird keybind issues

For me, adding this to my config solved it:
```bash
set-window-option -g xterm-keys 
```

It's been a while, so I'm not totally sure what problem it was that it solved, but I _think_ [it was this](https://stackoverflow.com/questions/15445481/mapping-arrow-keys-when-running-tmux)

## 3. Highlighting and copying text with the mouse

Credit to [this SO post](https://unix.stackexchange.com/questions/318281/how-to-copy-and-paste-with-a-mouse-with-tmux)

Add this to your .tmux.conf:

```bash
set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi    C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-vi    C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down
```

and install xclip
```bash
sudo apt install xclip
```

Turning mouse mode on will also enable you to scroll each pane with the mouse wheel, and select panes with the mouse.

## 4. Set prefix to `
I prefer the prefix to be a single key. I thought using \` as the prefix and enabling entering it when pressing the key twice quickly was an elegant solution.
```bash
unbind C-b
set -g prefix `
bind-key ` send-prefix
```

## 5. Add a theme

While not strictly necessary, adding a theme to tmux really does make it look way cooler. On a functional note, though, some themes update what information is shown in the bottom status bar by default, which can really improve the user experience.

I went with a simple powerline theme from the [tmux-themepack](https://github.com/jimeh/tmux-themepack).

## Putting it all together
Here's my .tmux.conf:

```bash
set-window-option -g xterm-keys on
set -g default-terminal "xterm-256color"
unbind C-b
set -g prefix `
bind-key ` send-prefix

source-file "${HOME}/repos/tmux-themepack/powerline/default/purple.tmuxtheme"

set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi    C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-vi    C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down

# To copy, left click and drag to highlight text in yellow, 
# once you release left click yellow text will disappear and will automatically be available in clibboard
# # Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -selection c"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"
```