A Not-So-Elevator Pitch for Tmux

A brief presentation of Tmux, why you should care, how I use it and how you could too
R
Bash
Tutorial
Author

Gninahophin Jean-Pierre Axel

Tmux for the Profane: My Experience

I started using Tmux about two years ago. To be honest, at the time, I didn’t really understand what it was or what problem it was trying to solve. Back then, I was trying to use Vim with an external REPL (IPython, R). I discussed this in detail in another article, but the gist of it is that you can use tmux send-keys to send text into a Tmux pane (your REPL).

Because I wasn’t using SSH much during that period, I didn’t feel the need to explore it in greater depth. It seemed like the only thing everyone cared about was that Tmux allowed you to get back to your work on a remote server if your SSH session crashed.

Now, however, it is an essential part of my workflow, and I’m going to pitch it to you as only someone who truly cares about you could.

So, what is Tmux?

According to Wikipedia, Tmux is a terminal multiplexer for Unix-like systems. If you had a stroke reading that, it’s okay. I had one too; you will recover eventually.

A multiplexer is essentially a light switch with extra steps. Let’s say you have two energy sources powering your house: a potato and a solar panel. At some point, you might want to draw power from the solar panel instead of the potato to illuminate your home. And that’s what Tmux does for you at its core: it allows you to switch sources for your terminal.

TL;DR: You can execute several programs from the same terminal emulator instance.

BUT WAIT THERE IS WAY MORE. But let’s start with:

Tmux from a Vim User’s Perspective

If you’re familiar with Vim, Neovim, Helix, or even… Emacs, Tmux should feel like home… after changing some stupid keybindings, of course. (You can look at my Tmux config on GitHub for that.)

There is a command mode where you can execute… commands! There is also a copy mode akin to Vim’s visual mode. For the non-Vimmer, this means you can copy and paste from your terminal without your mouse, making you ten times faster than the speed of light. There are also sessions, windows, and panes.

  • Sessions: A session is an abstract layer you can name, allowing you to group several windows together.
  • Windows: Inside a session, you can have multiple windows.
  • Panes: Inside each window, you can open several panes.

Finally, there is even an equivalent for the Leader Key: the Prefix. By default, it’s Ctrl-B. This prefix key enables you to use a bunch of shortcuts and squeeze out every remaining femtosecond of productivity available.

Sessions

Sessions are persistent. This is why everyone loves Tmux with SSH.

The typical use case is the following: you SSH into a remote machine to perform a crucial step in some mystical pipeline, and suddenly, your connection breaks… Now your server swiftly terminates all your running processes. The Vim buffer you were writing in goes straight to /dev/null hell, and because you’re obviously not using nohup, and you have nobackup in your config, all your work is LOST.

BUT, this will never happen to you. Why? Because you’re obviously making a git commit for every single word you write. And even if you weren’t, if you use a Tmux session and get disconnected, Tmux will simply preserve your running processes—including that Vim instance where you were writing code so terrible you were afraid to commit it.

Organizing Your Work with Windows and Panes

Panes are the easiest part of Tmux to understand. Essentially, they allow you to split the screen within your current Tmux session. Assuming you need to run four monitoring programs and also need active shell access, you don’t need five instances of your favorite terminal. You can simply create a session called “monitoring”:

tmux new -s monitoring

This will create a session containing one window with an index number. You can check the number with Prefix q (by default, that is Ctrl-B q).

You can split your screen vertically or horizontally using: * Ctrl-B % (Vertical split) * Ctrl-B " (Horizontal split)

I have these remapped to Ctrl-B | and Ctrl-B - for clarity, as the symbols visually match the split direction.

However, five panes on the same window can feel cluttered. You may want to move some of them to another window.

You can create a new window with Prefix c. (“C” for “create”—this one actually makes sense!).

There are several other shortcuts to move swiftly between your sessions, windows, and panes. An excellent public cheatsheet can be found here: https://tmuxcheatsheet.com/

Copy Mode

Copy Mode is the other killer feature of Tmux. As a programmer, a big part of my workflow is copy-pasting… error messages. Depending on your terminal, whether you are using SSH or not, or if you have xclip/xsel installed, this can be quite annoying. Tmux has a nice solution: Copy Mode.

You enter it by pressing Ctrl-B [. You can then start your selection by pressing Space, move around to visually select the text you need, and copy/exit with Ctrl-B ].

Crucially, it supports a lot of Vim keybindings by default. It is extremely powerful because it exists above the programs you might be running. You can essentially copy objects you don’t natively have access to in your TUI (Terminal User Interface). If it’s on the screen, you can probably copy it.

Hopefully, I managed to convince you to give Tmux a try if you’re not already using it!