Feb 10, 2020 4 min read

tmux for Beginners: A Quick and Easy Introduction

tmux for Beginners: A Quick and Easy Introduction
Table of Contents

Boost your productivity with this tutorial in how to use tmux. Protect yourself against unexpected disconnects and improve development workflows.

tmux: What It Is and Why You Should Use It

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?

tmux stands for "terminal multiplexer". As a terminal multiplexer, tmux lets you use one terminal like its many terminals simultaneously.

What does this mean exactly?  Well, it means that you can initiate one terminal/SSH connection and, from within that single terminal connection, create multiple terminal sessions. These sessions can then be arranged and viewed in parallel in a window manager that tmux provides.

Why You Should Use tmux

Ultimately, tmux's ability to spawn multiple terminal sessions from a single terminal connection is brought about by tmux's ability to create persistent terminal sessions.

These persistent sessions are not only useful in and of themselves, but they also allow you to do some interesting things. The following are some use cases/benefits of running tmux.

tmux Keeps Your Linux Sessions and Terminal Connections Alive

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

tmux is useful for more than just protecting you against unexpected terminal disconnects and can be useful for other things in DevOps/IT operations. 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 in which you may be working on more than one project:

Develop from Anywhere: How to Set Up a Remote Development Server
Work from anywhere and develop remotely on any desktop. A beginner’s guide for how to set up a remote development server.

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

Installing tmux is so easy, it barely justifies its own heading. To install tmux, simply execute the following command:

sudo apt install tmux

Essential tmux Commands

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

To start a tmux session, simply enter the following command into your terminal:

tmux new -s session_name
Creating a new session in tmux
Creating a new session in tmux
The green bar at the bottom of the terminal shows that you are in a tmux session.
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

When you are done and wish to leave the current tmux session, enter the following command:

Ctrl + B followed by D
tmux session detached
tmux session detached

List tmux Sessions

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:

tmux ls
List of tmux sessions
List of tmux sessions

Reattach tmux Session

When you've identified the tmux session you wish to connect to, use the following command to reattach to it:

tmux attach-session -t <tmuxSessionNameHere>
Reattaching to running tmux session
Reattaching to running tmux session
Reattached to tmux session
Reattached to tmux session

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.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to The Engineer's Workshop.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.