Spotlight
05 September, 2019

Software Spotlight: tmux

Software Spotlight: tmux

If you're into web- and software development it's likely that at some point you've opened a terminal.

Since one terminal is often not enough tabs may be a useful addition. But at some point not even tabs are enough; switching between tabs which all belong to the same project is just annoying - but as of today a lot of projects require multiple processes to be run; one for running your application, one to watch log files, one for running commands on the fly and eight docker containers where you know what at least five of them are used for.

But having a tab for every single process can become overwhelming pretty quickly.

More Tabs!

And restarting your computer means re-opening all your tabs. That's no fun.

tmux, a terminal multiplexer

To compensate for this tab insanity you can use tmux. tmux is a multiplexer which allows for multiple terminals controlled from a single screen.

It's available for Linux and OS X (via brew install tmux). It might be possible to run tmux on Windows - but I have honestly no experience with that.

What it does

tmux offers a hierarchical set of features regarding terminal control; sessions, tabs and panes where a session can has multiple tabs and a tab can have multiple panes.

A very important feature tmux provides is the support of plugins. My absolute favorite plugin (and an absolute must-have in my opinion) is tmux-resurrect which makes it possible to persist our tmux configuration through system restarts.

Last but not least it's highly customizable in it's appearance which allows for some personal touch.

How I use it

To get an idea at how this can be used let's take at a look at how I use it:

Increases the hacker-ish look of your work by 120%

I use two different sessions, one for personal projects and one for work. My personal session (named dev) has different tabs for all my private projects. The tab shown in the screenshot is my blog and has four different panes: one for starting the Docker container for Commento (during development I don't need the -d flag to run the container in the background), one for Gatsby, one for Ghost-related commands and one for "everything else".

Every time I start my computer and open the terminal the first thing I do is to reload my tmux configuration, jump to the tab with the project I want to work on and I'm ready to go.

If I want to work on an other project all I need to do is to open it in my IDE and switch to the proper tab in tmux. All tabs are properly configured for their respective project - there's no need to manually cd into multiple directories using multiple tabs or terminals.

Usage

tmux is mainly controlled by shortcuts. These shortcuts use a specific prefix (ctrl+b by default) followed by the shortcut you want to execute. My personal most used shortcuts are:

  • ctrl+b + w to switch between sessions and tabs
  • ctrl+b + arrow keys to switch between panes
  • ctrl+b + c to create a new tab
  • ctrl+b + , to rename a tab
  • ctrl+b + r to restore my sessions (via the resurrect plugin)
  • ctrl+b + x kill the current pane
  • ctrl+b + | split pane vertically (custom, default is %)
  • ctrl+b + - split pane horizontally (custom, default is ")

A lot of people like to change the prefix key to ctrl+a instead of ctrl+b - but that's up to you. A useful cheat sheet with shortcuts can be found on GitHub by MohamedAlaa.

Configuration

As you may have noticed my tmux looks slightly different than the default greenish matrix-like look. By having a .tmux.conf file in your home directory you can customize many things like the look, plugins or some general settings.

My personal .tmux.conf can be found on GitHub. Let's talk about some neat configurations within this file:

Window title

set-option -g set-titles on
set-option -g set-titles-string "#S / #W"

This will cause your window title to follow equal <session name> / <tab name>.

Splitting shortcuts

By default the shortcuts for splitting up panes are % and ". I've never been able to tell the difference and hence simply rebound these to:

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

Which is way easier to remember; | splits the pane vertically, - horizontally.

Kevin

Kevin

I make stuff. Mostly functional, occasionally shiny, stuff.