Become a Linux command line Ninja (Part 1 — TMUX)

Matt
6 min readJun 3, 2023

Note: All of the following content is based on my personal opinion and experience; please feel free to challenge and or disagree with me.

Productivity

When working on the command line, productivity is critical. For me, productivity has always been dependent on three main factors.

  1. How quickly can you identify and input the right commands ?
  2. How quickly you can interpret the results of the last command ?
  3. How long can you maintain focus on a particular task ?

What is TMUX?

I’m glad you asked! So TMUX stands for “Terminal Multiplexer”. It provides all kinds of advanced functionality to a Linux or Unix terminal. I’ll describe some of the use cases below, but it adds a massive amount of really helpful functionality to the standard terminal, whether you are working in Gnome, KDE, XFCE [insert your desktop of choice] or even a classic terminal with no GUI at all.

https://github.com/tmux/tmux/wiki

What is lacking in the standard terminal?

When I was in consulting for a large Linux company, I often found myself using different clients. Some days I’d be using a Mac, others I’d be allowed to use my Fedora laptop. Sometimes I’d be given a Windows laptop Putty, Cygwin — you name it, I was told to get on and use it.

On some occasions I’d be working alone; other times, I’d be working as part of a small team, maybe I’d be working from home using a VPN to connect to a customer’s environment; other times, I’d be on a data centre floor using a terminal of some kind.

Neither my employer nor the customer wanted to hear my gripes, the job needed to get done.

TMUX helped with many of these challenges.

Installing TMUX?

TMUX can be installed on the server or client side. There are advantages to both. If you install it on the server, it simplifies multiple people working together. It also means you can use whatever client you are given.

If you install it on the client, you have more control. Personalising TMUX with plugins and config can make it an incredibly powerful tool for getting things done.

For RHEL / CentOS / Fedora-based systems

$ sudo dnf install tmux

For older RHEL / CentOS-based systems ( 8 and below)

$ sudo yum install tmux

For Debian / Ubuntu

$ sudo apt-get install tmux

That’s it; you’ve installed TMUX.

To test it, we can type tmux into a terminal and see something like this. Congratulations!

Apart from a toolbar at the bottom, not a lot has changed. Right?

TMUX concepts

In order to start being productive with TMUX, we need to understand how it works (a bit).

This diagram (credit to Arcolinux) helps us visualise how these concepts fit together. If you want a deeper dive into these concepts, I recommend visiting them at https://arcolinux.com/everthing-you-need-to-know-about-tmux-panes/

Sessions, Windows and Panes

Sessions

The beauty of sessions is that we can “attach” to existing sessions. If you’ve ever used the screen utility, you’ll understand the power of this. For example if two colleagues are connected onto the same system as the same user, we can have an interactive, screen share for a terminal, without any additional software. I’ve used this on site, helping to debug issues, and even training others. It’s incredibly powerful.

Take this example. In the red window, I create a TMUX session called “matts-session”.

$ tmux new -s matts-session

I can then start running some jobs. In this case, just checking the amount of free memory on my system.

Let’s assume another person who has access to the system wants to dig deeper into my memory consumption on the platform. They want to do this at the same time as me, so we can collaborate on the results / findings.

The user in the blue window can attach to my TMUX session to join in the fun.

The can log onto the target system and type

tmux attach-session -t matts-session

The blue terminal now has an interactive session to join the red user in some diagnostics.

So when the blue user runs the “top” command

Here you can see both terminals together. Both users can input commands and see exactly the same output. This is incredibly powerful when collaborating remotely.

Windows

Windows give us the next level of control. Sometimes we’ll need to want to run multiple commands, maybe multiple SSH connections. These things can be difficult to manage with a single shell.

To solve this, we can create multiple windows. Think of these like tabs in your terminal window.

Here you can see that within my session, I have 2 windows. I can use some TMUX commands to quickly switch between my commands.

Window 1 — running top

Window 2 — inspecting the proc filesystem

Panes

Panes live within a window. By default (and up until now), we’ve only used a single pane in a window. However, it’s often convenient to “split” a pane.

For example, we can monitor some logs while restarting a service. We can edit a script while running and debugging it in the other window.

Here I’m editing a script in one pane, running it in another and monitoring the logs in another.

Hopefully, these really basic examples give you an idea of the power of TMUX.

Running TMUX commands

TMUX, like VI and a number of other tools, require a “command key” to be pressed before you send it commands. This is similar to the VI command / insert modes.

By default, the command key is “CTRL-B”. I’m not going to list all of the possible commands here, but I’d strongly suggest using a cheat sheet until the muscle memory in your fingers takes over. Don’t worry, it will take a little while, but once you have the most used commands in muscle memory, it will improve your performance.

I highly recommend this cheat sheet while you get to grips with it.

https://tmuxcheatsheet.com/

Recap

How quickly can you identify and input the right commands ?

Using TMUX for collaboration is a great way to identify what and where to run commands. Two minds are better than one (usually :) )

How quickly you can interpret the results of the last command ?

Being able to split panes gives you the ability to run the next command while maintaining the output of the last command visible.

How long can you maintain focus on a particular task ?

This is the most important. When using TMUX, lifting your fingers from the keyboard is unnecessary. This means no break in concentration. Therefore your command line experience will flow rather than start/stop as you switch windows/tabs.

What next ? Plugins and profiles

Plugins and profiles allow us to extend the functionality of TMUX. This might be a simple colour scheme, or integration with your favourite application.

I use a set of plugins/profiles called Oh My TMUX, they add just enough extra functionality such as battery life, system time and uptime at the bottom of the pane. This saves me from having to switch windows constantly. Since I’m using Fedora as my daily driver, it’s a really handy addon.

https://github.com/gpakosz/.tmux

I will write a follow-up post about profiles and plugins if people are interested.

--

--