Tools

Tools: tmux

tmux is a terminal multiplexer. It consists of a tmux server that is capable of handling multiple sessions and a tmux client that is the user interface to sessions. Each session consists of multiple windows and each window can have multiple panes. Each pane represents a seperate terminal. You can easily jump between sessions, windows, and panes. You connect to a session using the tmux client which is a CLI. You can disconnect from a session and allow that session to continue running in the backgroud which allows you to reconnect at a later time. You can also connect to a tmux server running on a different machine. It is wonderous stuff.

If you spend a good chunk of your day in front of a terminal using the command line, tmux will make you more productive. It’s another one of those tools that you don’t have to learn, but if you do it pays off in multiples.

The creation of tmux sessions can be scripted. A common setup is to create an alias to your common session configurations so that you can be ready to start working on your project by issuing a single short command that is aliased to your tmux script. The script that executes will either connect to an already running tmux session with that name or create the session with the desired windows and panes and also kick off any programs that need to be running (editor, webserver, etc.). No more spending a few minutes getting your windows set up and cd’ing around your code base like crazy, one short command and you are ready to go.

Sessions

Working with sessions from the command line

The Prefix: sending commands to tmux

In general, what you type into tmux is passed through to the underlying terminal. To allow passing commands to tmux itself you enter the <prefix> key combination followed by a tmux command.

The default prefix is <ctrl>+b. It is easier to type <ctrl>+a (especially if you remap caps lock to ctrl) and you can set that up in the tmux configuration file (described below) using the following.

unbind C-b
set -g prefix C-a

The examples on this page use <prefix> to indicate entering the prefix key combination.

For example, <prefix>t displays the time in the center of the active panel.

To send <ctrl>+a itself to the underlying terminal (which you might want to do as <ctrl>+a does useful things like move to the start of the line when editing commands) add the following to your tmux configuration. Then enter the prefix twice, <prefix><prefix>.

bind C-a send-prefix

Working with sessions from within tmux

Windows

Each tmux session consists of multiple windows (you can think of windows like tabs). Windows are numbered starting from 0, but you can change this to start from 1 by adding the following to your tmux configuration file.

set -g base-index 1

Working with windows from the command line

Working with windows in tmux

The available windows, along with their numbers and names, are listed on the status line. The default window name is the name of the program that is executing in the active pane of the window.

Panes

Panes are used to break a window into multiple areas. Each pane is a seperate terminal.

Working with panes in tmux

The following configuration setting is useful to display pane numbers long enough that you can actually select the one you want to jump to (value is in milliseconds).

set -g display-panes-time 3000

Command mode

<prefix>: will enter command mode. Here you can type tmux commands. The shortcuts above are a convenient way to issue these commands. However, many commands take options which can’t be supplied using the shortcuts so you will need to enter command mode for some types of work.

tmux key bindings

<prefix>? will display all valid key bindings. Use <space> to page through the results and q to quit.

tmux configuration

tmux configuration is handled using the .tmux.conf file in your home directory. I keep tmux configuration very light (mostly because I have not dug that deep) and there are a few examples from my configuration in this post.

You can see my current .tmux.conf file on Github. This is largely inspired from the tmux 2 book referenced below, but includes various “tricks” discovered on sources like StackExchange.

tmux scripts

tmux scripts are insanely useful. They are really shell scripts that issue a number of tmux commands to establish a tmux session.

My canonical use case is to create a tmux script for each project I am working on. This script will check if the tmux session already exists and attach to the session if it exists. If the session does not exist, it will create the session with two named windows. The first window is named editor and the script will move to the project root directory and open Vim with a directory listing of the project root directory. The second window will have three panes. A left side pane which I typically use to interact with Git (and issue the git status command as a starte) and two stacked panes on the right side. The top pane will be the command line of the project root and the bottom pane will often be running a server of some type. I then create an alias in my shell configuration to execute this script.

If I’m working on project X, I might have an alias tpx (tmux project X) that executes the tmux script and lands me at the editor (or wherever I left off) ready to go. I am working in seconds with minimal thinking required.

You can see my tmux script template on Github.

Resources

$ man tmux the man page for tmux is great,

<prefix>? lists all the tmux keybindings (<space> to page down and q to quit)

tmux 2: Productive Mouse-Free Development, by Brian P. Hogan
https://learning.oreilly.com/library/view/tmux-2/9781680502374/
This was the book that got me using tmux well. It’s a wealth of information and many of the ways I use tmux were inspired from the ideas in this book.

Tools

Web

Languages

Data