# Boost Productivity with tmux: Master Terminal Sessions

> You can find the Chinese version of this article at [終端機 session 管理神器 - tmux](https://medium.com/larry-blog/tmux-33a24e595fbc).

As a developer, you should be familiar with the Terminal. Whether you want to create git branches, run the scripts, or ssh to the remote machine, you will need to use the Terminal.

I'd like to introduce you to a great tool called **tmux**. It's a terminal multiplexer with many useful features like **multi-windows**, **window panes,** and **session saving**. Most importantly, tmux can make your terminal stylish and **professional** even if you're not doing anything.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684324197088/0cd3b6f3-6743-48ea-85bf-88212ba0522b.png align="center")

## How to install tmux

On **Mac OS**, you need to first install [**Homebrew**](https://brew.sh), and then use it to install tmux with the following command.

```bash
brew install tmux
```

On Debian/Ubuntu Linux, simply run the following command.

```bash
sudo apt-get install tmux
```

Once you've installed tmux, run the command `tmux` and you will see a **green status bar** appear at the bottom of the terminal.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684324350450/71e853fc-8b03-4930-83c3-1dc32ca2ead0.png align="center")

## Basic Concepts of tmux

### session

Every time you run `tmux`, a new independent session is created.

### window

A window refers to the entire screen as shown in the screenshot below, and there can be multiple windows in a session.

### pane

As shown in the following screenshot, a window can be split into multiple panes. This functionality is typically used to monitor numerous programs simultaneously.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684324358039/699ca642-0fb1-4fcc-b239-c6a7130b43f7.png align="center")

## Basic Keybindings

When using tmux, you must press **the prefix** key before any keybinding. By default, the prefix is set to `<Ctrl+b>`.

### Pane

Let's try the keybinding together!

* `<Ctrl+b> + "`: Split pane with vertical layout
    
* `<Ctrl+b> + %`: Split pane with horizontal layout
    
* `<Ctrl+b> + <Arrow Key>`: Switch to other panes
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684393345326/3daebe44-0f39-411c-bfa2-ae76acef25cd.gif align="center")

* `<Ctrl+b> + <Space>`: Toggle between pane layouts
    
* `<Ctrl+b> + x`: Close the current pane
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684393372828/d2e3088b-502d-45ac-a876-b026da232a48.gif align="center")

### Session

* `<Ctrl+b> + d`: Detach from the current session
    
* `$ tmux at`: Attach to the last session
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684393381790/7d440f9f-b00b-4a85-959a-e407469b60e3.gif align="center")

## Common scenarios

### Scenario 1 - Running a program and Monitoring the CPU usage simultaneously

This is the most common scenario for me. I usually use **two panes** to run a program and display the top/htop result simultaneously.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684324788304/e60cc056-e811-4f87-8b6d-3fa5e06ca01f.png align="center")

### Scenario 2 - Writing a program in Vim with Real-Time results

This scenario is similar to the previous one. You can create a small pane to run the program while editing the source with Vim. If you're using a tool like **nodemon**, it will automatically re-run the script when you update it, so you don't need to leave Vim.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684324794821/edf0f060-c1c2-4b09-93f9-9ab06ceeb6f0.png align="center")

### Scenario 3 - Keeping remote SSH sessions with tmux

Without tmux, the command will stop running if you run a command on a remote machine using SSH but the connection is lost.

To avoid this problem, you can install tmux on the remote machine. With a tmux session, even if your ssh connection is lost, your program will continue to run because **it's in the tmux session**.

## Summary

The article is a simple introduction to tmux. As a tool I can't develop without, I want to share this super tool with you through this article.

Since this article is only an introduction for beginners, I didn't list all the commands and keybindings here. If you'd like to learn more about tmux, you can check out [this cheat sheet](https://tmuxcheatsheet.com/). I hope that using tmux can make your development more efficient, and also make your terminal look super-cool!
