Better homes and tmuxing

Introduction

While almost all interactions with computers and devices nowadays is through a graphical user interface, or GUI (point and click mouse actions, multi-touch interfaces), command-line interactions are still quite popular in the IT world. Microsoft is one organization that sees the benefit of command-line interaction and provides a great, modern command-line interface called PowerShell (interesting fact: the core of most Microsoft Server applications uses PowerShell '€” the graphical interface simply translates mouse clicks into command-line actions).

Command-line access is usually done through a system console, or terminal.

tmux is a terminal multiplexer '€” a tool for nesting several terminal sessions inside a master terminal. There are several benefits to doing something like this; the most basic is simple multi-tasking. Just as some people like working with several Word documents open, some administrations work with several terminals open.

Another benefit is remote access. If I'm working on a remote server and I want to use several terminals for multi-tasking, I would ordinarily have to open several terminals on my desktop and log in to the remote server on each terminal. With tmux, I can simply log in to a remote server with one terminal and then spawn several other terminals within it.

A final example benefit of tmux is the ability to do remote session saving. Building off of the last example, if my session is disconnected or if I leave the office and want to continue working at home, I can simply disconnect from tmux, reconnect at home, and all of my terminals are as I left them.

So, with these benefits in mind, here's how I use tmux.

Installation

If you're using a Mac, you can install tmux using homebrew. Ubuntu users can install it by doing:

sudo apt-get install tmux

Usage

I hesitate to repeat information that's already been explained better elsewhere. For some great tutorials on how to use tmux, checkout this and this.

My Personal Configuration

After reading those two tutorials, you should have a basic idea of how to use tmux. The following are some configuration options that I've built over the past year of using tmux every day. Maybe they'll be helpful to others.

All options should go in your .tmux.conf file.

set-option -g prefix C-a

Binding with ctrl-a

The default tmux action key, ctrl-b, is a little odd for those who are accustomed to screen (like myself), which used ctrl-a.

bind a send-prefix

While ctrl-a is familiar to screen users, it's also used for other commands as well, such as moving your cursor to the beginning of the line. By setting ctrl-a to your action key, other ctrl-a commands now no longer work. This configuration option fixes this by allowing you to press ctrl-a-a to restore those actions.

Implementing changes in your current tmux

bind r source-file ~/.tmux.conf ; display "Reloaded!"

Any changes made in your .tmux.conf require tmux to be restarted. This configuration option allows you to press ctrl-a-r to have all changes immediately implemented in your current tmux session.

Splitting your window

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

These options allow you to split your tmux window horizontally and vertically by using the | and – keys.

Colour and formatting

set -g status-left-length 32
set -g status-right-length 150
set -g status-fg white
set -g status-bg colour234
set -g window-status-activity-attr bold
set -g pane-border-fg colour245
set -g pane-active-border-fg colour39
set -g message-fg white
set -g message-bg colour234
set -g message-attr bold
set -g status-left '#[fg=colour235,bg=colour252,bold] #S #[fg=colour252,bg=colour238,nobold] #[fg=colour245,bg=colour238,bold] #H #[fg=colour238,bg=colour234,nobold] '
set -g window-status-format "#[fg=white,bg=colour234] #I #W "
set -g window-status-current-format "#[fg=colour234,bg=colour39] #[fg=colour25,bg=colour39,noreverse,bold] #I #W #[fg=colour39,bg=colour234,nobold] "
set -g status-right '#[fg=color39] %Y-%m-%d %H:%M'

These options configure the colouring and format of the tmux window to look like this:

tmux pic

Navigating between panes and windows

unbind m
unbind n
unbind M
unbind N
bind -nr M-m select-pane -t :.+
bind -nr M-n select-pane -t :.-
bind -nr M-N prev
bind -nr M-M next
bind -r m select-pane -t :.+
bind -r n select-pane -t :.-
bind -r N prev
bind -r M next

These options control how to navigate between panes and windows.

Setting these changes on a Mac

The actual key bindings might not be for everyone, so feel free to change them. In order to get these options to work on a Mac, you have to make sure the Option key is configured as the Meta key. In iTerm2, this can be done by setting the following in Preferences:

tmux pic3

Note that the selection is for +Esc and not Meta. Because that makes sense.

With that out of the way, you can now navigate between windows by using Meta-N and Meta-M and navigate between panes with Meta-n and Meta-m. Again, it's not for everyone, but it somehow feels very intuitive for me.

Maximizing a pane

unbind Up
bind Up new-window -d -n tmux-zoom ; swap-pane -s tmux-zoom ; select-window -t tmux-zoom
unbind Down
bind Down last-window ; swap-pane -s tmux-zoom ; kill-window -t tmux-zoom

Sometimes I'll be working in a pane and want to "maximize" to take up the entire window. These options do this. Ctrl-a-UpArrow and Ctrl-a-DownArrow will now maximize and minimize a pane.

Setting up vi

set -g status-keys vi
setw -g mode-keys vi
bind Escape copy-mode
bind -t vi-copy v begin-selection
bind -t vi-copy y copy-selection

I'm a vi user. These options enable vi-style navigation.

Using your mouse to select

set -g mode-mouse on
set -g mouse-select-pane on
set -g mouse-select-window on
set -g mouse-resize-pane on
set -g mouse-utf8 on

These options allow you to use your mouse to select panes, windows, and text. But be warned: it can sometimes be very buggy and cause your tmux session to output gibberish. To help with this, I have these options:

bind b set -g mode-mouse off ; set -g mouse-select-pane off ; set -g mouse-select-window off ; set -g mouse-resize-pane off
bind B set -g mode-mouse on ; set -g mouse-select-pane on ; set -g mouse-select-window on ; set -g mouse-resize-pane on

When the mouse interaction starts causing problems, I can use ctrl-a-b and ctrl-a-B (note: these are two distinct commands using the lowercase and uppercase letter) to turn the mouse support off and on.

Conclusion

So that's it. Nothing terribly fancy. Most of my effort has gone into making sure I can navigate between windows and panes quickly and intuitively. This is because, on average, my tmux session has three windows, each containing three to four panes. This is repeated four to five times, depending on how many remote servers I'm logged into. Using this in combination with a modern Mac Thunderbolt display with 2560×1440 resolution makes for an extremely enjoyable CLI experience.

If you're interested in working tmux into your daily workflow, I recommend starting with a blank .tmux.conf file and slowly adding options as you see the need. Trying to use all of the above options straight away is way too much to remember and learn at once.