Terminal Multitasking with Tmux


As software developers, we use terminal emulators for tasks like compiling, version control, and remote machine management. Normally, we
do these concurrently in different terminal windows.

If you're using iTerm 2 for macOS, you can use its tabs and split panes feature. But
for any other Linux/Unix and SSH terminal emulators, you can use tmux (terminal multiplexer).

Tmux is an alternative to GNU Screen that allows you to multiplex several virtual consoles
in a single window, useful for running multiple CLI programs at the same time.

Installation

Package Manager

Install the tmux package using your system's package manager.

macOS (Homebrew)

brew install tmux

Ubuntu/Debian

sudo apt-get install -y tmux

From Source

git clone https://github.com/tmux/tmux.git
cd tmux
sh autogen.sh
./configure && make

Important: requires at least a working C compiler, make, autoconf, automake, pkg-config as well as libevent and ncurses
libraries and headers.

How to Use

Type tmux in your terminal to start a new session. The display will be cleared and a status bar with time and date will appear at the bottom
of the terminal screen indicating tmux is running.

You send commands to tmux via shortcut keys, which are prefixed by Ctrl-b. That is, you press the prefix (Ctrl-b) keys first then the
desired shortcut command.

Basic Commands

Below are the default basic commands in Tmux. This can be configured in a tmux.conf file in your $HOME directory.

Windows

Active windows are listed on the tmux status bar.

Command Shortcut
Create window Ctrl-b, c
Rename current window Ctrl-b, ,
Close current window Ctrl-b, &
Next window Ctrl-b, n
Previous window Ctrl-b, p
Last active window Ctrl-b, l
Select window by number Ctrl-b, 0...9

Panes

Panes are divisions/splits of independent virtual terminals inside a window.

Command Shortcut
Split pane vertically Ctrl-b, %
Split pane horizontally Ctrl-b, "
Switch to last active pane Ctrl-b, ;
Move current pane left Ctrl-b, {
Move current pane right Ctrl-b, }
Switch to pane to the direction Ctrl-b, or or or
Close current pane Ctrl-b, x
Toggle pane zoom (aka maximize) Ctrl-b, z
Move to next pane Ctrl-b, o
Convert pane to window Ctrl-b, !
Switch to pane by number Ctrl-b, q, 0...9

Misc

Command Shortcut
Enter command mode Ctrl-b, :
Show shortcuts list Ctrl-b, ?

More Shortcuts

For advanced shortcuts, you can visit this cheat sheet for reference.

Configuration

This Arch Linux Wiki contains a comprehensive guide on how
to configure your tmux setup such as adding or modifying shortcut keys, layouting tricks, Vim/Emacs keybinding,
mouse support, etc.

Asciinema

Here's a demo of tmux in action.