Things I use: tmux

« back to all blogs

tmux is an absolutely fantastic piece of software, and one I use daily - even to write this blog! So, here’s my short letter of appreciation to tmux.

It turns out that the last time I wrote anything here was exactly two years ago. It also so happens that at the end of this month I’ll be switching jobs, so I have at least two good reasons to take stock and look back on some of the things I made, learned and used. I’m not exactly ready to start writing thirteen-page tutorials again just yet, so for now I’ll focus on some of the software and hardware I use and love.

And the first one absolutely must be tmux.

What is tmux even?

tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen.

This is what the original README for tmux will tell you. This description, while perfectly accurate, might cause you to shrug, eyes rolling.

So allow me to explain a bit more. If most of your daily work is not done within a terminal, tmux may not be of that much interest to you. After all, if you’re hanging out in an IDE such as Visual Studio or RubyMine, most of the things that can be done with a console are instead done with keyboard shortcuts or (*shudder*) buttons.

I’m most comfortable in a console. You’ll regularly find me running a session with some docker-compose things up, another where I’m running a webserver of some kind, yet another with a rails console, yet another with a guard watching specs, probably at least one for git and one-off commands, then another occupied by neovim… you get the picture. If terminal sessions were trees, Greenpeace would send me strongly worded letters every other day, ‘cause I go through them like nobody’s business.

Admission of guilt: up until about four years ago these were all tabs in a terminal emulator. I’d switch to one, to the other, to the third, and back to the first one again. And since iTerm’s keyboard shortcuts, while decent, had the propensity to slip my mind, a lot of that switching was done with the touchpad.

Enter tmux

tmux gives your shell itself the ability to create tabs. That in and of itself might not sound like all that much, but these can also be split horizontally, vertically, and each of these splits can be split again, and so on and so forth.

Here’s how that looks like:

I’m writing this post in Vim, while Hugo - the static site generator I use to build it - runs in top left, serving it locally so I can check everything as I go. And top right is a dmesg watch, mostly to show off that I can split vertically too ;)

You may also spot a little “[1] 1:nvim” down at the bottom. This essentially means “window 1, tab 1, and the process’ name is nvim”. I could have a lot of these windows and a lot of these tabs - using exactly one terminal emulator window. Neat.

This gets even better on remote

Locally this might not seem like such a huge boon - I could, after all, find a terminal emulator that has proper tabs, and allows arranging its windows in this manner. I’m sure I’ve seen one on a colleague’s computer at some point, didn’t ask for the name back then though.

But consider this: what if I have to hop on a remote machine to do some debugging, or some adjustments to a project I keep fully on that machine (e.g. something Raspberry Pi-based)? I’d have to open a new ssh session in each of these tabs which is a bother. Not to mention I could actually put the poor thing under undue stress.

Enter tmux again. I can just run a single tmux session on remote, and have a windowed, tabbed experience regardless. Editing code, watching logs, running some one-offs - all with the same shortcuts my muscle memory already knows.

Let’s talk shortcuts

Here’s the thing: the default shortcuts for tmux are arcane. I could not get used to them whatsoever. However, Gregory Pakosz, a gentleman and a scholar I’m sure though we’ve never met, put together a config file with much saner defaults.

Some highlights include Vim-like navigation, so <Leader>hjkl work kind of like you would expect - they move you between windows. Also splitting horizontally is <Leader>s and vertically <Leader>v. The default settings for these include such easy to memorize keys as | and [, which don’t look like anything to me.

Of course, being the customization fan that I am, I made a few tweaks over the years - you can compare and contrast in my dotfiles.

I use this

And I recommend you check it out. It’s not for everyone: there are still a lot of shortcuts to memorize, they aren’t always obvious, and - especially on some Linux distros - I’ve run into situations where the packaged tmux version was way too old for my config. This is easily fixed by compiling a version from git, but that’s within the comfort zone of an even narrower group of people.

This also comes with investment. Getting rid of your old workflow and moving to this one will hurt, and you’ll get less productive before you improve. I know I’ve mentioned Vim a couple of times already, but I find it a fitting analogy - learning Vim is also hard and make you less productive in the short term, but then the investment starts paying off.

I’m grateful that tmux exists and that I was accidentally exposed to it. It improved my workflow and my productivity. Now you’ve been exposed to it, and who knows? Maybe you’ll find it useful. :)


Top image credit: https://pxhere.com/en/photo/1116555 (CC0)

« back to all blogs