DEV Community

Cover image for tmux for Beginners: A Quick and Easy Introduction

Posted on • Originally published at on

tmux for Beginners: A Quick and Easy Introduction

Table of Contents

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" and is basically a window manager for terminals.

Why You Should Use tmux

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

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 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 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

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
New session in tmux

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

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

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

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.

Top comments (0)