Video

Want to see the full-length video right now for free?

Notes

Introduction

Tmux is the "terminal multiplexer," which in the creator's own words means:

It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more.

Use Cases

  • Long running processes - When attached to a remote machine via ssh, long-running processes can be started and left running without needing to stay attached via ssh.
  • Pair Programming - Tmux can be used to allow two or more users to attach to the same session and share control providing an efficient pair programming setup.
  • Local Development - By enabling various layouts and communication between multiple processes, tmux can enable enhanced efficiency for local development.

The primary focus of these videos is on using tmux for local development, although the content will easily apply to all contexts tmux can be used in.

Installation

Tmux is a very popular package and should be available on any system package manager. In my case, as an OS X user, this means I can install with:

$ brew install tmux

Initial Commands

The easiest way to start a tmux session is to run the bare tmux command from your terminal.

Once running, most tmux interaction will occur by using tmux's key bindings. By default all tmux key bindings will require a "prefix" key sequence before they are active. This prefix is initially defined as Ctrl-b (Ctrl and b together).

  • <prefix> % => Split window horizontally (to the right of the current pane)
  • <prefix> " => Split window vertically (below current pane)
  • <prefix> o => Jump to other pane

Tmux Configuration File

The tmux configuration file lives at ~/.tmux.conf. Edit it to redefine the prefix key by adding the following lines:

# Use C-s (control and s key at the same time) as the prefix key
unbind C-b
set -g prefix C-s

After saving the file, you can reload the configuration by running:

$ tmux source-file ~/.tmux.conf

Caps Lock to Control

In addition to changing the prefix key, I also recommend remapping your caps lock key to act as a control key so that the tmux prefix can be pressed with your hands still on the home row. Use the following links to help configure this:

Tmux Objects Overview

  • Session - A named collection of one or more windows.
  • Window - A single screen within tmux, similar to tabs in terminal applications. At any given time, a client will be attached to a single window.
  • Pane - A portion of a window running a single process, e.g. Vim, zsh, man, etc. Panes can be oriented either vertically or horizontally and resized as needed.

Workflow Recommendations

  • Use a single client - Although it is possible to run multiple tmux clients by opening multiple terminal tabs or windows, I find it better to focus on one client in a single terminal window. This provides a single high-level abstraction, which is easier to reason about and interact with.
  • One project per session - I will open each project, roughly mapping to a git repository in its own session. Typically I will have Vim in the first window along with a pane for running tests and any background processes like the rails server running in additional windows.
  • One Vim instance per session - In order to avoid conflicts with temp files and buffers getting out of sync, I will only use a single Vim instance per tmux session. Since each session maps to a specific project, this tends to keep me safe from conflicting edits and similar complications.