Basic setup of Terminal Multiplexer (TMUX)

Basic setup of Terminal Multiplexer (TMUX)

After setting up Oh-My-Zsh as out default shell, the natural flow, goes to set up a multi-screen environment. Nowadays, most of the GUI consoles that comes with Gnome, KDE, etc. have support for tabs, however XTerm and PuTTY and others do not. iTerm 2 on Mac OS has native support for TMUX setup, which can be enabled easily. I usually try to get my setup as platform-agnostic as possible. With this type of setups it’s extremely easy to go overboard, so I modify the bare essentials only. Feel free to use the final config file and build on top of it or switch shortcuts freely. If this post sparks your interest, might be worth setting up a tmux plugin manager and unleash the beast, so to say. However, this is more of a professional down-to-earth quick setup.

So here are my main requisites:
– Easily create a new shell window
– Easily do a vertical/horizontal split
– Easily close a window or a split panel
– Easily switch between windows and split panels
– Easily resize panels
– Get a notification when an activity finishes
– Mouse support

Statusbar setup:
– Automatic window names, based on process
– Date & Time
– Machine name
– Number of users on the machine
– CPU load over the past 5 minutes

So here we go, I’m doing this on Ubuntu 16.04, Once you open the shell, check you if you have it installed, running

which tmux

if yes, check the version using

tmux -V

if it’s > 2.1. Otherwise, you’ll need to google the mouse setup, as it differs for versions before 2.1. If you don’t have tmux installed:

sudo apt-get install tmux

for Debian/Ubuntu or “sudo apt-get update” to make sure you have the latest version.
For Mac OS:

brew install tmux

or

brew upgrade tmux

respectively.
Now, in your home folder, you should have a file .tmux.conf which will be the main object of editing.
I keep, the default Ctrl + B for the main commands, as if I start using TMUX on a different machine, I’m not completely lost or forget the shortcuts, thus I’ll keep the creation of a new window, vertical and horizontal panels to:

Ctrl + B & N (window)
Ctrl + B & % (vertical)
Ctrl + B & " (horizontal)

Firstly, add in the .tmux.conf file:

# Automatically set window title
set-window-option -g automatic-rename on
set-option -g set-titles on

#Default terminal colors and history
set -g default-terminal screen-256color
set -g history-limit 10000

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

to get the boring stuff out of the way. The above makes sure the window titles are based on the running process, support 256 colors and the command history file tracks the last 10000 commands, lastly remove the delay on Esc key press.

Then, I want to be able to use to switch focus on mouse click and
change windows on Shift + Left/Right Arrow and switch panes on Alt + Arrow:

#Enable mouse mode
set -g mouse on

# Shift + Left/Right Arrow to switch windows
bind -n S-Left previous-window
bind -n S-Right next-window

# Alt + Arrow to switch pane
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

Resizing is key, so I’ve mapped it to Control + Arrow key.

# Resize pane on Ctrl + Arrow
bind -r C-Up resize-pane -U
bind -r C-Down resize-pane -D
bind -r C-Left resize-pane -L
bind -r C-Right resize-pane -R

When something finishes, you want to know, when to switch to a window, so you can use:

# Visual Indicator if an activity completes in a window
setw -g monitor-activity on
set -g visual-activity on

That’s the windows and panes bit really.
The status bar is where you go wild with plugins, custom commands. Anything that outputs text can essentially be put in the status bar. The below lines set the background color to green and font to black.

# Status bar
# selected window as a square with date time, show 5min cpu, users, machine
set-window-option -g status-left " #S "
set-window-option -g status-left-fg black
set-window-option -g status-left-bg green

Now, we want to set up the Number of users on the machine, CPU load in the past 5 minutes, Hostname, Date and time.
All of this is in the first line below:

set-option -g status-right '#(uptime | cut -d "," -f 2,4) / #H / %H:%M %d-%b'
set-window-option -g status-right-fg black
set-window-option -g status-right-bg green

set-window-option -g window-status-format " #I: #W "

set-window-option -g window-status-current-format " #I: #W "
set-window-option -g window-status-current-fg green
set-window-option -g window-status-current-bg black

That’s it really. As easy as copy-pasting. In case you want to learn the rest of tmux’s commands, you can go to https://tmuxcheatsheet.com/.

A common question is how to close a window or a pane, you can do

Ctrl + B & X, then press Y

However the quickest way is to do just

Ctrl + D

You can download the entire config file HERE, if you don’t want to follow the steps and directly put it in your home folder.

I have only tested this on Xterm and Konsole in Ubuntu, should work fine in iTerm2, however, haven’t tested it myself. The end results looks as below:

Leave a Reply