DEV Community

Cover image for Making tmux suck less
Dizzyspiral
Dizzyspiral

Posted on

Making tmux suck less

tmux is a great tool. 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

set -g default-terminal "xterm-256color"
Enter fullscreen mode Exit fullscreen mode

2. Fixing weird keybind issues

For me, adding this to my config solved it:

set-window-option -g xterm-keys 
Enter fullscreen mode Exit fullscreen mode

It's been a while, so I'm not totally sure what problem it was that it solved, but I think it was this

3. Highlighting and copying text with the mouse

Credit to this SO post

Add this to your .tmux.conf:

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
Enter fullscreen mode Exit fullscreen mode

and install xclip

sudo apt install xclip
Enter fullscreen mode Exit fullscreen mode

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.

unbind C-b
set -g prefix `
bind-key ` send-prefix
Enter fullscreen mode Exit fullscreen mode

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.

Putting it all together

Here's my .tmux.conf:

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"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)