What do you think? Discuss, post comments, or ask questions at the end of this article. [More about me]

Skip to end of metadata
Go to start of metadata

tmux is a great terminal multiplexer which allows viewing / working-in multiple terminal windows simultaneously on your server (via SSH for example). 

Guide

This guide is not supposed to be an in-depth guide on using tmux.  It's purpose is to highlight only several of the most-useful (according to me) commands that I use often.

Installing tmux

tmux should be available via your distro's package manager.  To install on Ubuntu, simply:

sudo apt-get install tmux

Installing from source

Although your preferred package manager will generally have tmux, it's version might be a old and not support some of the new features (like changing a pane background colour which is supported from v2.1).  This was the case for the version provided on my Amazon Linux install.  I chose to install from source instead.

Let's first get some dependencies for the build (example here is on Amazon Linux):

sudo yum install automake libevent-devel
sudo yum install git

Installation instructions are provided on tmux official github repo.  Stated here as well:

git clone https://github.com/tmux/tmux.git
cd tmux
./configure && make
sudo make install

Starting a tmux session

To start a tmux session:

tmux

To list all detached tmux sessions:

tmux ls

To re-attach a detached session (session 0 for example):

tmux a -t 0

My .tmux.conf

tmux can be configured to use your own defined shortcuts.  This can be done my saving key bindings and other settings to a .tmux.conf file in your user home folder (do: nano ~/.tmux.conf)

Below is my current .tmux.conf file:

# Needed for using C-Left/Right for window switching with putty
set -g terminal-overrides "xterm*:kLFT5=\eOD:kRIT5=\eOC:kUP5=\eOA:kDN5=\eOB:smkx@:rmkx@"

# create (h)orizontal and (v)ertical panes without prefix
bind -n C-M-n split-window -h
bind -n C-M-m split-window -v

# Resize panes without prefix
bind -n C-M-Down resize-pane -D
bind -n C-M-Up resize-pane -U
bind -n C-M-Left resize-pane -L
bind -n C-M-Right resize-pane -R

# zoom into pane
bind -n C-M-f resize-pane -Z

# Use Alt-hjkl without prefix key to switch panes
bind -n C-M-h select-pane -L
bind -n C-M-l select-pane -R
bind -n C-M-k select-pane -U
bind -n C-M-j select-pane -D

# No delay for escape key press
set -sg escape-time 0

# THEME
set -g default-terminal "screen-256color"

# set inactive/active pane styles
set -g window-style 'fg=colour247,bg=colour236'
set -g window-active-style 'fg=colour250,bg=black'

Theming colours and Putty

If you use Putty to connect via SSH, then you'll need to set the Terminal-type string to xterm-256color:

You also might want to set the bolded text indication to 'Both' as having this set to 'The Colour' makes some ncurses applications (such as the fantastic ranger) a bit harder to read/use:

Using the above .tmux.conf file changes several default key-bindings to:

CommandCommand prefix
Split window horizontallycntrl-alt-n
Split window verticallycntrl-alt-m
Resize pane upcntrl-alt-Up
Resize pane downcntrl-alt-Down
Resize pane leftcntrl-alt-Left
Resize pane rightcntrl-alt-Right;
Zoom current pane (make fullscreen)cntrl-alt-f
Switch to above panecntrl-alt-k
Switch to below panecntrl-alt-j
Switch to left panecntrl-alt-h
Switch to right panecntrl-alt-l

Reloading .tmux.conf without restarting tmux

You can reload your .tmux.conf configuration without restarting (e.g. exiting and starting tmux again) by entering prefix mode (cntrl+b) and entering:

:source-file ~/.tmux.conf

Exiting

To exit a session you can simply "exit" all open terminals.

You can also detach you session by cntrl-b followed by d.

Kill tmux session(s)

To kill a session, first list sessions, first list them, then (killing session 0 example):

tmux kill-session -t 0

References

  1. https://gist.github.com/MohamedAlaa/2961058
  2. https://gist.github.com/spicycode/1229612
  3. https://gist.github.com/limingjie/4975c36d13d0927613e6
  4. https://github.com/tmux/tmux