Give Your Terminal Super Powers: tmux Cheatsheet!
Published: 2019-10-31 03:57:33

tmux is an application that I use in my terminal to manage several programs running at once. I've used it on Linux and Mac, it's one of the first things I install on a new computer along with [Zsh](https://www.zsh.org) and [asdf](https://github.com/asdf-vm/asdf).

The tmux wiki refers to tmux as a "terminal multiplexer;" basically, it lets you run several programs in one terminal window (and do some other really cool stuff).

It's one of those productivity tools that takes a certain amount of upfront investment to become comfortable. It's not quite as hard to get used to as Vim, but you do have to remember a handful of hotkeys.

# Installing tmux

If you want to get your hands dirty with tmux, you'll have to pop open a terminal window and install it.

If you're using Ubuntu, you can install it with a simple `sudo apt-get install tmux`. 

For those of you using Mac OS, you can install tmux with `brew install tmux`.

I haven't done development on a Windows machine in years, so I'm not sure how to get it running in that environment. If someone does, please leave a comment so I can link to it here, thanks!

## Leader key

In tmux, pretty much every command starts out with the Leader Key, sometimes also called the prefix. By default, you have to press `ctrl + b`.

For most tmux commands, you'll press the leader key, then the hotkey for the command. Unlike many hotkeys, you don't need to keep the leader key held down while pressing the hotkey.

# Sessions

### Starting a tmux session

tmux sessions are the interface for using tmux. When you start your terminal, you'll need to launch a tmux session.

This is easy, just type `tmux` into your terminal, and you're off!

### Creating a named session

Sometimes it is useful to give a tmux session a name, so that you can detach from it, and re-attach to it later.

`tmux new -s some-session-name`

### Renaming a session

`ctrl + b` `$`

### Detaching from a session

You can detach from a session and return to your standard shell without closing the session. This allows you to pick up your work later.

`ctrl + b` `d`

### Listing existing sessions

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/kld8lrolqjwwmgpnemza.png)

You can list existing sessions by name with:

`tmux ls` or with `tmux list-sessions`

### Attaching to a session

Once you've detached from a session, you can pick up where you left off by attaching to it.

Attach to the most recent session:

`tmux a`

Attach to a session by name:

`tmux a -t some-session-name`

If you currently attached to a session you can hop between sessions too.

Attach to the previous session:

`ctrl + b` `(`

Attach to the next session:

`ctrl + b` `)`

### Kill a session

`tmux kill-ses -t some-session-name`

### Kill all other sessions

If you want to kill all the sessions other than the one you are currently using, tmux makes that super simple:

`tmux kill-ses -a`

# Panes

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/x7z0deoplan4b9z21qr3.png)

This is the most widely used feature of tmux. When you want a terminal with several windows running different processes, for example: a server, git, and Vim. You'd use a few panes.

The basics of pane splitting are easy!

### Split a pane vertically

`ctrl + b` `%`

### Split a pane horizontally

`ctrl + b` `"`

### Navigating panes

You can navigate between your panes with the arrow keys. This is the most basic way of moving across your window:

`ctrl + b` `←`
`ctrl + b` `↑`
`ctrl + b` `→`
`ctrl + b` `↓`

### Jump to the most recent pane

`ctrl + b` `;`

### Jump to next pane

`ctrl + b` `o`

### Close current pane

`ctrl + b` `x`

### Show pane numbers

Panes can be identified by numbers, you can get a list of those numbers with this shortcut:

`ctrl + b` `q`

### Swap to a pane by number

`ctrl + b` `q` `0` (replace 0 with preferred pane number)

### Resize current pane

This is one of the few cases in which you should hold the leader key down instead of releasing it!

`ctrl + b + ←`
`ctrl + b + ↑`
`ctrl + b + →`
`ctrl + b + ↓`

### Move the current pane

To the right:

`ctrl + b` `}`

To the left:

`ctrl + b` `{`

# Windows

If you need a new set of panes, you can create a window. A window is kind of like a tab.

### Convert a pane into a window

`ctrl + b` `!`

### List all windows

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/lfytjtfd7o9izgt0sjxd.png)

`ctrl + b` `w`

### Select window by number

`ctrl + b` `0` (replace 0 with preferred window number)

### Create a new window

`ctrl + b` `c`

### Navigate to the previous window

`ctrl + b` `p`

### Navigate to the next window

`ctrl + b` `n`

### Close the current window

`ctrl + b` `&`

# Command mode

Like Vim, tmux has a command mode.

To start command mode, use this:

`ctrl + b` `:`

# Get help

`ctrl + b` `?`

This is a cheatsheet for me, but hopefully, others find it useful. I'll probably expand it as I think of additional shortcuts I use frequently.

If there is a tmux shortcut you use frequently, feel free to add it in the comments!

### There's more...

I'm writing [a lot of articles](https://jh.codes) these days, I run a [podcast](https://www.devpath.fm), and I've started sending out a [newsletter digest](https://pages.convertkit.com/674caf55d4/f30f7753a7) about all of the awesome stories I'm hearing.

You can also follow me on [Twitter](https://twitter.com/jakeherrington), where I make silly memes and talk about being a developer.