# Tmux cheat sheet

Whether you're starting up an elaborate Raspberry Pi [home lab](https://opensource.com/article/20/6/kubernetes-raspberry-pi) or you're managing a building full of workstations, sometimes you need to do the same task on multiple hosts.
There are many ways to automate tasks across systems.
[Ansible](https://opensource.com/article/19/2/quickstart-guide-ansible), for instance, ensures all systems are in the same state, and sometimes a simple [cron](https://opensource.com/article/17/11/how-use-cron-linux) job will do.
But sometimes you just need to run commands manually.
For that, you need [tmux](https://opensource.com/article/17/2/quick-introduction-tmux), a single terminal that puts you in control of multiple command prompts.

[Tmux](https://github.com/tmux/tmux) is an open source application that adds layers (or "windows", in tmux terminology) to your terminal window so that you can open more than one terminal in a single desktop window.
The result is a tabbed interface (without the physical tabs), so you can flip from one open terminal to another without resorting to using the mouse the way you have to when switching from one tab in Firefox to another.
This might seem redundant.
After all, modern terminal applications, such as GNOME Terminal and Konsole, have tabs built in by default, and even some very old terminals, such as [rxvt](https://opensource.com/article/19/10/why-use-rxvt-terminal), have the ability to add a tabbed interface.
Tmux, however, adds in the ability to split a window into panes, with each pane containing a separate terminal, and each terminal can be logged into a separate host.
You can build an array of terminals with just a few keyboard shortcuts or a rudimentary [tmuxinator](https://opensource.com/article/20/1/tmux-console) script.

<img src="https://opensource.com/sites/default/files/uploads/productivity_15-1.png" />

## Install tmux

On Linux and BSD, you can install tmux from your software repository or [ports tree](https://opensource.com/article/19/11/pkgsrc-netbsd-linux)
On Mac, use [Homebrew](https://opensource.com/article/20/6/homebrew-mac).

For example, on RHEL or Fedora:

```
$ sudo dnf install tmux
```

## Start tmux

To start tmux, open a terminal and type:

```
$ tmux
```

When you do this, the obvious result is that tmux launches a new shell in the same window, with a status bar along the bottom.
There's more going on, though, and you can see it with this little experiment.
First, do something in your current terminal to help you to tell it apart from another empty terminal:

```
$ echo hello
hello
```

Now press **Ctrl+B** followed by **C** on your keyboard.
It might look like your work has vanished, but actually you've created what tmux calls a *window* (which can be, admittedly, confusing because you probably also call the terminal you launched a *window*).
Thanks to tmux, you actually have two windows open, both of which you can see listed in the status bar at the bottom of tmux.
You can navigate between these two windows by index number.
For instance, press **Ctrl+B** followed by **0** to go to the initial window:

```
$ echo hello
hello
```

Press **Ctrl+B** followed by **1** to go to the first new window you created.

You can also "walk" through your open windows using **Ctrl+B** and **N** (for Next) or **P** (for Previous).

## Tmux trigger and commands

The keyboard shortcut **Ctrl+B** is the tmux trigger.
When you press it in a tmux session, it alerts tmux to "listen" for the next key or key combination that follows.
Tmux shortcuts, therefore, are all prefixed with **Ctrl+B**.

You can also access a tmux command-line and type tmux commands by name, too.
For example, to create a new window the hard way, you can press **Ctrl+B** followed by **:** to enter the tmux command-line.
Type ``new-window`` and press **Enter** to create a new window.
This does exactly the same thing as pressing **Ctrl+B** **C**.

## Splitting windows into panes

Once you have more than one window created in tmux, it's often useful to see them all in one window.
You can split a window horizontally (meaning the split is horizontal, placing one window in a North position and another in a South position) or vertically (with windows located in West and East positions).

To create a horizontal split, press **Ctrl+B** followed by **"** (that's a double-quote).

To create a vertical split, press **Ctrl+B** followed by **%** (percent).

You can split windows that have been split, so the layout is up to you and the number of lines in your terminal.

[Golden ratio](tmux_golden-ratio.jpg)

Sometimes things can get out of hand.
You can adjust a terminal full of haphazardly split panes using these quick presets:

*  **Ctrl+B** **Alt+1** Even horizontal splits
*  **Ctrl+B** **Alt+2** Even vertical splits
*  **Ctrl+B** **Alt+3** Horizontal span for the main pane, vertical splits for lesser panes
*  **Ctrl+B** **Alt+3** Vertical span for the main pane, horizontal splits for lesser panes
*  **Ctrl+B** **Alt+5** Tiled layout

## Switching between panes

To get from one pane to another, press **Ctrl+B** followed by **O** (as in *other*).
The border around the pane changes colour based on your position, and your terminal cursor changes to its active state.
This method "walks" through panes in order of creation.

Alternately, you can use your arrow keys to navigate to a pane according to your layout.
For example, if you've got two open panes divided by a horizontal split, you can press **Ctrl+B** followed by the **Up** arrow to switch from the lower pane to the top pane.
Likewise, **Ctrl+B** followed by the **Down** arrow switches from the upper pane to the lower one.

## Running a command on multiple hosts with tmux

Now that you know how to open many windows and divide them into convenient panes, you know nearly everything you need to know to run one command on multiple hosts at once.
Assuming you have a layout you're happy with, and each pane is connected to a separate host, you can synchronize the panes such that the input you type at your keyboard is mirrored in all panes.

To synchronize panes, access the tmub command-line with **Ctrl+B** followed by **:**, and then type **setw synchronize-panes**.

Now anything you type at your keyboard appears in each pane, and each pane responds accordingly.

## Download our cheat sheet

It's relatively easy to remember **Ctrl+B** to invoke tmux features, but the keys that follow can be difficult to remember at first.
All built-in tmux keyboard shortcuts are available by pressing **Ctrl+B** followed by **?** (exit the help screen with **Q**).
However, the help screen can be a little overwhelming for all its options, none of which are organized by task or topic.
To help you remember the basic features of tmux, as well as many advanced functions not covered in this article, we've developed a tmux cheatsheet.
It's free to download, so get your copy today.

[LINK TO THE CHEAT SHEET]()