tmux for Beginners: A Quick and Easy Introduction
Tags: productivity, tutorial, linux
Published: 2020-02-17 14:31:55

---
title: tmux for Beginners: A Quick and Easy Introduction
published: true
date: 2020-02-10 16:05:50 UTC
tags: productivity,tutorial,linux
canonical_url: https://engineerworkshop.com/2020/02/10/tmux-for-beginners-a-quick-and-easy-introduction/
---
## Table of Contents

* [tmux: What It Is and Why You Should Use It](#tmuxMain)
 - [What is tmux?](#what-is-tmux)
 * [Why You Should Use tmux](#why-you-should-use-tmux)
   - [tmux Keeps Your Linux Sessions and Terminal Connections Alive](#tmux-keeps-your-linux-sessions-and-terminal-connections-alive)
   - [Organizing Terminal Sessions](#organizing-terminal-sessions)
* [Install tmux](#install-tmux)
* [Essential tmux Commands](#essential-tmux-commands)
  - [Start/Create a New tmux Session](#start-create-a-new-tmux-session)
  - [Detach from a tmux Session](#detach-from-a-tmux-session)
  - [List tmux Sessions](#list-tmux-sessions)
  - [Reattach tmux Session](#reattach-tmux-session)

## tmux: What It Is and Why You Should Use It <a name="tmuxMain"></a>
I travel a lot for work and almost all of my work is done remotely. I doubt I'm really an exception here since, when you think about it, almost all server administration is handled remotely (not too many sysadmins plug in a monitor and keyboard and pull up a chair to the server rack). We all just SSH into our servers, even when we're in the same physical premises.

The difference though is that, when you're working remotely as opposed to at the office or at home, your wifi connection is notoriously unreliable. Your connection can drop at any time without warning. If you happen to be doing some mission-critical task and you were just using a simple bash shell, guess what, your terminal and whatever processes you happened to be running die with it. If you were connected to a production server and writing data, this can be disastrous.

Thankfully though, you can take out a cheap (free) insurance policy against this unpredictable risk: tmux. So what is tmux and how does it save us?

### What is tmux? <a name="what-is-tmux"></a>
tmux stands for "terminal multiplexer" and is basically a window manager for terminals.

### Why You Should Use tmux <a name="why-you-should-use-tmux"></a>
Ultimately, tmux allows you to keep persistent sessions running across servers. The following are some case-uses/benefits of running tmux.

#### tmux Keeps Your Linux Sessions and Terminal Connections Alive <a name="tmux-keeps-your-linux-sessions-and-terminal-connections-alive"></a>
When you create a new tmux session, a new terminal session is created within it and that session stays alive for the life of the tmux server (i.e. until you reboot) or you explicitly kill the tmux session. That means, if you unexpectedly lose connection, whatever you were running in that tmux session stays alive and continues to chug along, even when you're gone. Pretty great, eh?

But wait, there's more!

#### Organizing Terminal Sessions <a name="organizing-terminal-sessions"></a>
tmux is useful for more than just protecting you against unexpected terminal disconnects and can be a useful productivity tool for developers. Due to tmux's persistence, you can simply detach from one tmux session (keeping whatever it had) and create an additional one. This is particularly useful if you're [running a remote development server](https://engineerworkshop.com/2019/12/01/how-to-set-up-a-remote-development-server/) in which you may be working on more than one project.

For example, I was recently working on a new Handlebars.js theme for Ghost and had deployed a local development Docker container for it on my remote development server. My mom's birthday is coming up in a few months and so I then wanted to start some work on a new project for her present. I simply detached from my Handlebars.js session and created a new one. When I want to switch back, I can switch back to that other session just like I had never left.

## Install tmux <a name="install-tmux"></a>
Installing tmux is so easy, it barely justifies its own heading. To install tmux, simply execute the following command:
```bash
sudo apt install tmux
```
## Essential tmux Commands <a name="essential-tmux-commands"></a>
With tmux installed, let's dive into some of the basic commands to get you started and on your way to using tmux.

### Start/Create a New tmux Session <a name="start-create-a-new-tmux-session"></a>
To start a tmux session, simply enter the following command into your terminal:

```bash
tmux new -s session_name
```

![Creating a new session in tmux](https://dev-to-uploads.s3.amazonaws.com/i/uyktf8lw1dy3qqngfa0d.jpg)
![New session in tmux](https://dev-to-uploads.s3.amazonaws.com/i/mogjfv8vhc43m5upv53d.jpg)

The green bar at the bottom of the terminal shows that you are in a tmux session. Once you are in your tmux session, go about your business in the terminal as you always have.

### Detach from a tmux Session <a name="detach-from-a-tmux-session"></a>
When you are done and wish to leave the current tmux session, enter the following command:

```Ctrl + B followed by D```

![tmux session detached](https://dev-to-uploads.s3.amazonaws.com/i/44uwi7ygj8o80yovp1d4.jpg)

### List tmux Sessions <a name="list-tmux-sessions"></a>
It can be easy to forget what tmux sessions you have running on a given server (especially when you have multiple machines). In order to see a list of your currently running tmux sessions, use the following command:

```bash
tmux ls
```

![List of tmux sessions](https://dev-to-uploads.s3.amazonaws.com/i/i6yio213j0mgqis3t4zb.jpg)

### Reattach tmux Session <a name="reattach-tmux-session"></a>
When you've identified the tmux session you wish to connect to, use the following command to reattach to it:
```bash
tmux attach-session -t <tmuxSessionNameHere>
```
![Reattaching to running tmux session](https://dev-to-uploads.s3.amazonaws.com/i/gasorko8w5nf0yq972wz.jpg)

![Reattached to tmux session](https://dev-to-uploads.s3.amazonaws.com/i/odwttd60setyhsq5tpb2.jpg)

Congratulations, you now know the basics of using tmux. tmux is a powerful utility that can be useful for protecting your terminal sessions from unexpected interruptions and in development workflows by allowing easy context switching.

In future posts, we'll cover more advanced features such as controlling windows and panes.