Groesbeek, view of the 'National Liberation Museum 1944-1945' in Groesbeek. © Ton Kersten
Fork me on GitHub

Switching to tmux

2011-07-24 (104) by Ton Kersten, tagged as sysadm

Some time ago I was fighting my .screenrc again. I wanted to change the status line, but it was hardly possible to read and understand what I typed hardly half a year ago. The screen config file is not exactly poetry.

While searching the web to find how to change the status line I ran into tmux and I thought: "Let's give it a try". And after the very simple compile I started configuring it. What a surprise I was in for. This config file could be read, understood and changed. Man, this is good.

One of the first things I changed was the default Prefix key Ctrl-B. I changed it to Alt-A, so I can still use all the control keys with Vim and in the terminal.

I also installed it on my Macbook and configured iTerm2 to use the "option" key to send meta-chars and that was all. Working in a single shot.

Things I like

  • Horizontal and vertical screen split actually work
  • Good and readable config file
  • Fast
  • Easy to script

Things I don't like

  • I'm missing the serial connection possibility. This is not a show stopper, I can always use screen for that.
  • The default Esc delay. But again no show stopper. Easy to configure.

Scripting with tmux

I created a little script (as a demo) to show how easy it is to script with tmux.

#!/bin/bash
tmux new-session -d -s session_name

tmux new-window -t session_name:1 -n 'local'
tmux new-window -t session_name:2 -n 'login'  'ssh login'
tmux new-window -t session_name:3 -n 'backup' 'ssh backup'
tmux new-window -t session_name:4 -n 'devel'  'ssh devel'

tmux select-window -t session_name:1
tmux attach-session -t session_name

Setting the hostname in the status bar

In my ~/.zshrc I do have the following code to set the hostname of the window in the tmux status bar

precmd()
{
    if [[ ( ${-} == *i* ) && ( ${TERM} == screen* ) ]]
    then
        echo -n "\ek$(hostname -s)\e\\"
    fi
}

My ~/.tmux.conf

#------------------------------------------------------------------------------#
# vi: set sw=4 ts=4 ai:                            ("set modeline" in ~/.exrc) #
#------------------------------------------------------------------------------#
# Config file  : ~/.tmux.conf                                                  #
#                                                                              #
# Author       : Ton Kersten                                   The Netherlands #
#------------------------------------------------------------------------------#

# I use all Alt-Keys instead of Ctrl. This because I use a lot of Ctrl in
# Vim and so on and I don't like to use the Prefix all the time.
# And.... this also works on OSX with iTerm2

# Set that stupid Esc-Wait off, so VI works again
set-option -sg  escape-time 0

# Set the prefix to Alt-A
set-option -g   prefix M-a
bind-key M-a    send-prefix

# All kind of nice options
set-option -g   bell-action any
set-option -g   default-terminal screen
set-option -g   display-panes-colour red
set-option -g   history-limit 100000
set-option -g   message-bg red
set-option -g   message-fg white
set-option -g   mouse-select-pane off
set-option -g   pane-active-border-bg default
set-option -g   pane-active-border-fg red
set-option -g   pane-border-bg default
set-option -g   pane-border-fg cyan
set-option -g   repeat-time 500
set-option -g   visual-activity off
set-option -g   visual-bell on
set-option -g   set-titles on
set-option -g   set-titles-string ' #I-#W '
set-option -g   terminal-overrides 'xterm*:smcup@:rmcup@'
set-option -g   base-index 1
set-option -g   default-path ""

# Screen lock
bind-key C-x    lock-server
bind-key x      lock-server
bind-key -n M-x lock-server
set-option -g   lock-after-time 0
set-option -g   lock-server on
set-option -g   lock-command "vlock"

# statusbar
set-option -g   status-utf8 on
set-option -g   status-interval 5
set-option -g   status-justify left
set-option -g   status-left-length 15
set-option -g   status-left ' #h |'
set-option -g   status-right ' | %Y-%m-%d %H:%M #[default]'

# default statusbar colors
set-option -g   status-fg white
set-option -g   status-bg blue
set-option -g   status-attr bright
set-option -g   status-keys emacs

# default window title colors
set-window-option -g window-status-fg white
set-window-option -g window-status-bg blue
set-window-option -g window-status-attr dim

# active window title colors
set-window-option -g window-status-current-fg yellow
set-window-option -g window-status-current-bg red
set-window-option -g window-status-current-attr bright
#
set-window-option -g mode-fg white
set-window-option -g mode-bg red
set-window-option -g mode-attr bright

# Window options
set-window-option -g utf8 on
set-window-option -g clock-mode-colour blue
set-window-option -g clock-mode-style 24
set-window-option -g monitor-activity on
set-window-option -g xterm-keys on
set-window-option -g automatic-rename on
set-window-option -g aggressive-resize off
#
set-window-option -g window-status-format ' #I-#W '
set-window-option -g window-status-current-format ' #I-#W '

# Remap keys to my settings
unbind-key M-d      ; bind-key -n M-d   detach-client
unbind-key d        ; bind-key d        detach-client

unbind-key M-c      ; bind-key -n M-c   command-prompt -p "tmux:"
unbind-key M-m      ; bind-key -n M-m   command-prompt -p "Man:" "split-window 'man %%'"
unbind-key Tab      ; bind-key Tab      choose-window
unbind-key M-w      ; bind-key -n M-w   choose-window
unbind-key M-e      ; bind-key -n M-e   choose-session
unbind-key M-t      ; bind-key -n M-t   new-window
unbind-key t        ; bind-key t        new-window
unbind-key M-`      ; bind-key -n M-`   last-window
unbind-key n        ; bind-key n        next-window
unbind-key p        ; bind-key p        previous-window
unbind-key M-n      ; bind-key -n M-n   next-window
unbind-key M-p      ; bind-key -n M-p   previous-window
unbind-key M-right  ; bind-key -n M-right   next-window
unbind-key M-left   ; bind-key -n M-left    previous-window

# Window selection
unbind-key 1        ; bind-key 1        select-window -t 1
unbind-key 2        ; bind-key 2        select-window -t 2
unbind-key 3        ; bind-key 3        select-window -t 3
unbind-key 4        ; bind-key 4        select-window -t 4
unbind-key 5        ; bind-key 5        select-window -t 5
unbind-key 6        ; bind-key 6        select-window -t 6
unbind-key 7        ; bind-key 7        select-window -t 7
unbind-key 8        ; bind-key 8        select-window -t 8
unbind-key 9        ; bind-key 9        select-window -t 9
unbind-key 0        ; bind-key 0        select-window -t 10
unbind-key M-1      ; bind-key -n M-1   select-window -t 1
unbind-key M-2      ; bind-key -n M-2   select-window -t 2
unbind-key M-3      ; bind-key -n M-3   select-window -t 3
unbind-key M-4      ; bind-key -n M-4   select-window -t 4
unbind-key M-5      ; bind-key -n M-5   select-window -t 5
unbind-key M-6      ; bind-key -n M-6   select-window -t 6
unbind-key M-7      ; bind-key -n M-7   select-window -t 7
unbind-key M-8      ; bind-key -n M-8   select-window -t 8
unbind-key M-9      ; bind-key -n M-9   select-window -t 9
unbind-key M-0      ; bind-key -n M-0   select-window -t 10

# Window splitting
unbind-key M--      ; bind-key -n M--   split-window -v
unbind-key M-\      ; bind-key -n M-\   split-window -h
unbind-key \        ; bind-key \        split-window -h
unbind-key |        ; bind-key |        split-window -h
unbind-key -        ; bind-key -        split-window -v

# Pane selection and resizing
unbind-key left     ; bind-key left     select-pane -L
unbind-key up       ; bind-key up       select-pane -U
unbind-key down     ; bind-key down     select-pane -D
unbind-key right    ; bind-key right    select-pane -R
unbind-key C-h      ; bind-key C-h      select-pane -L
unbind-key C-k      ; bind-key C-k      select-pane -U
unbind-key C-j      ; bind-key C-j      select-pane -D
unbind-key C-l      ; bind-key C-l      select-pane -R
unbind-key j        ; bind-key -r j     resize-pane -D 5
unbind-key k        ; bind-key -r k     resize-pane -U 5
unbind-key h        ; bind-key -r h     resize-pane -L 5
unbind-key l        ; bind-key -r l     resize-pane -R 5
unbind-key C-left   ; bind-key -r C-left    resize-pane -L 1
unbind-key C-right  ; bind-key -r C-right   resize-pane -R 1
unbind-key C-up     ; bind-key -r C-up  resize-pane -U 1
unbind-key C-down   ; bind-key -r C-down    resize-pane -D 1

unbind-key @        ; bind-key @        confirm-before kill-window
unbind-key M-r      ; bind-key -n M-r   source-file ~/.tmux.conf
unbind-key q        ; bind-key q        list-keys
unbind-key M-q      ; bind-key -n M-q   list-keys

# Copy mode
set-window-option -g mode-keys vi
set-window-option -g mode-mouse off
set-option buffer-limit 10
unbind-key M-NPage      ; bind-key -n M-NPage       copy-mode
unbind-key M-PPage      ; bind-key -n M-PPage       copy-mode
unbind-key M-i          ; bind-key -n M-i       paste-buffer
unbind-key -t vi-copy M-{   ; bind-key -t vi-copy M-{   begin-selection
unbind-key -t vi-copy M-}   ; bind-key -t vi-copy M-}   copy-selection
unbind-key -t vi-copy Home  ; bind-key -t vi-copy Home  start-of-line
unbind-key -t vi-copy End   ; bind-key -t vi-copy End   end-of-line
unbind-key -t vi-copy b     ; bind-key -t vi-copy b     rectangle-toggle