# My tmux configuration

02 February 2016

tmux is a terminal mutiplexer. It lets you have multiples shells running in a single terminal emulator window and it keeps those shells running in the background should you need to close your terminal emulator.

I’ve played around with the configuration quite a bit to find settings that suit my needs. Here’s what it ended up looking like :

This screenshot was done on Mac OS X, using the Terminal app and this Solarized theme.

I figured I’d share my tmux configuration here !

## Installing tmux

tmux is available on Debian. I suggest using the jessie backports version :

`apt -t jessie-backports install tmux`

tmux is also available on Mac OS X using brew :

`brew install tmux`

## tmux.conf

I used screen before tmux, so I configured the prefix key on C-a instead of C-b. tmux has the advantage of being much simpler to configure than screen.

If you want to use this configuration, simply copy the following in ~/.tmux.conf. This file is read by default when tmux starts.

If you simply want to try it out, copy it in a file somewhere else and have tmux load with the -f parameter (`tmux -f ~/tmux-test.conf`).

    # use utf8
    set -g utf8
    set-option -g status-utf8 on
    set-window-option -g utf8 on

    # do not wait on esc key
    set-option -g escape-time 0

    # completely disable automatic rename
    set-window-option -g automatic-rename off

    # basic settings
    set -g default-terminal "screen-256color"
    set -g aggressive-resize off
    set-window-option -g xterm-keys on
    #set-window-option -g mode-mouse off

    # command history
    set -g history-limit 10000

    # messages
    set -g message-bg default
    set -g message-fg red

    # no visual activity
    set -g visual-activity off
    set -g visual-bell off

    # status bar
    set-option -g status-justify centre
    set-option -g status-bg default
    set-option -g status-fg blue
    set-option -g status-interval 5
    set-option -g status-left-length 30
    set-option -g status-left '#[fg=red][ #[fg=white]#H #[fg=red]]#[default]'
    set-option -g status-right '#[fg=red][ #[fg=white]%R %d/%m #[fg=red]]#[default]'

    # modes
    set-option -g mode-bg default
    set-option -g mode-fg blue

    # inactive window format
    set-window-option -g window-status-format '#I:#W#F'
    set-window-option -g monitor-activity on
    #set-window-option -g monitor-content on # not available in tmux 2.0

    # activity in a window
    set-window-option -g window-status-activity-attr dim
    set-window-option -g window-status-activity-bg default
    set-window-option -g window-status-activity-fg yellow

    # content in a window # not available in tmux 2.0
    #set-window-option -g window-status-content-attr dim
    #set-window-option -g window-status-content-bg default
    #set-window-option -g window-status-content-fg red

    # active window format
    set-window-option -g window-status-current-fg white
    set-window-option -g window-status-current-bg default
    set-window-option -g window-status-current-format '#[fg=red](#[default]#I:#W#F#[fg=red])#[default]'

    # reload tmux configuration
    unbind r
    bind r source-file ~/.tmux.conf \; display "Configuration reloaded!"

    # Screen-like keybinds
    unbind C-b
    set -g prefix ^A
    set -g prefix2 ^Q
    bind a send-prefix
    bind q send-prefix

    unbind c
    bind c new-window
    unbind ^C
    bind ^C new-window

    unbind n
    bind n next-window
    unbind ^N
    bind ^N next-window

    unbind A
    bind A command-prompt "rename-window %%"

    unbind p
    bind p previous-window
    unbind ^P
    bind ^P previous-window

    unbind a
    bind a last-window
    unbind ^A
    bind ^A last-window

    unbind [
    bind Escape copy-mode

    unbind w
    bind w list-windows

    unbind k
    bind k confirm-before "kill-window"

    unbind l
    bind l refresh-client

    unbind '"'
    bind '"' choose-window

## Aliases

I also use two functions with tmux (in ~/.bash_aliases).

The first one creates a new “mytmux” tmux session if one doesn’t exist yet, opens 10 shells and selects the first one.

    mytmux() {
      tmux has-session -t mytmux
      if [ $? != 0 ]; then
        tmux new-session -s mytmux -n $(hostname) -d
        tmux new-window -t mytmux:1 -n $(hostname)
        tmux new-window -t mytmux:2 -n $(hostname)
        tmux new-window -t mytmux:3 -n $(hostname)
        tmux new-window -t mytmux:4 -n $(hostname)
        tmux new-window -t mytmux:5 -n $(hostname)
        tmux new-window -t mytmux:6 -n $(hostname)
        tmux new-window -t mytmux:7 -n $(hostname)
        tmux new-window -t mytmux:8 -n $(hostname)
        tmux new-window -t mytmux:9 -n $(hostname)
        tmux select-window -t mytmux:0
      fi
      tmux attach -t mytmux
    }

The second one changes the tmux window name whenever I ssh to a remote host, and switches the window name back to the name of my computer when I logout from the host.

    if [ -n "$TMUX" ]; then
      ssh() {
        if [ $# -le 2 ]; then
          tmux rename-window "${@: -1}"
          command ssh "$@"
          tmux rename-window "$(hostname)"
        else
          command ssh "$@"
        fi
      }
    fi

## Conclusion

That’s all ! As always, please do leave a comment if you’ve found something useful in this article !

## MySQL backup script

## Debian updates with Ansible