On a recent One on One chat with Brad we got to talking about tmux. Brad was interested to hear that I use it every day on my local development machines, which isn’t what first comes to mind as a use case for tmux.
So I thought it worth briefly sharing why I started using tmux for local development a year ago and fell in love with this super awesome little tool.
What is tmux?
The name “tmux” is a contraction of “terminal multiplexer”. It is commonly thought of as a tool you use when working on a remote server as it saves opening up multiple terminal (ssh) sessions to the machine when you’re trying to do a few things at once.
Maybe you’ve got a script you need to run that takes a while and at the same time you want to monitor its output to a log file with a command like
tail -f. Perhaps all the while editing some other config files or scripts you’re getting ready to run next.
Normally you’d fire up three or more separate instances of your terminal application and ssh into the server with each one. With tmux you only need one ssh connection to the server.
The great thing about tmux is it allows you to have multiple panes open at the same time, each with their own shell running, but using the same single ssh connection.
Not only that, you can also have multiple “windows” open at the same time, a bit like tabs with more panes in them.
A huge benefit of using tmux on a remote server is that if your internet connection should die for any reason, that tmux session keeps on truckin’ on the server. This means your long-running job that you just kicked off and forgot to
nohup and background with stream redirection just keeps on going too. All you need to do is
tmux attach once your internet is back and you’ve ssh’d into the server again, you’ll have all the same panes and windows there waiting for you.
Why Use tmux Locally?
I’ve just outlined a few of the most obvious reasons that you would use tmux when logged into a remote server, but given that internet connection problems can’t stop you using the command line on the machine where you’re sat, and that there’s plenty of ways to rearrange multiple terminal windows on said machine, why do I use tmux locally?
The biggest reason for me is having the ability to almost instantly get my preferred command line “workspace” set up as soon as I fire up my terminal emulator.
In the above gif you can see me firing up Alacritty (my terminal emulator) for the first time in almost a week after having a bit of time off. I then bumped up the font size so you can see what’s going on. I was able to attach to my already running “dev” tmux session to quickly see the last WP Offload Media Acceptance Tests run I kicked off before my break. I also checked which WP Offload SES PR I last reviewed as its git branch name is in the shell prompt and last unit tests run results were still available. Everything was just as I left it, even though there was no terminal emulator window open for days.
I have a tmux window for each of the projects I’m currently working on, each generally has at least a couple of panes where the current working directory is within the project’s source tree or related folders. I hop between the projects with a quick
Ctrl-A N, where “N” is the number of the window. So to get to my WP Offload Media stuff I just hit
On my “admin” window I usually have htop open in one pane as it’s handy for quickly seeing what’s hogging my machine’s resources if things start getting slow (usually Docker or Time Machine). Otherwise, the other two panes are generally used for updating my
.dotfiles, reading man pages, or general support stuff like
curl -I https://cdn.example.com/some/customers/bucket/object.png.
Before using tmux I used to use Magnet for rearranging terminal windows after MercuryMover went away. It wasn’t ideal for getting things set up nicely. When trying to get to a specific window quickly without having to resort to my trackpad it was near impossible, and I have always struggled to get tiling terminal emulators to work how I like. tmux acts more like a tiling window manager, giving you lots of ways to get to and rearrange your panes and window. If you’re trying to reduce how much you use a mouse or trackpad you’ll be very happy.
In fact, after using tmux for just a little while, I started looking at tiling window managers on my Linux machines, eventually settling on the most excellent Qtile. And as you may have noticed from the screenshots in this article, I’ve just recently started using a tiling window manager on macOS too! So be warned, using tmux day in day out could lead you to wanting to massively improve your entire desktop experience!
What Happens When You Reboot?
“Ok smarty pants, what happens when you reboot your machine and that tmux session dies? Where’s your nice comfortable multi-window workspace now?” I hear you say?
In the above gif I willfully kill my beloved “dev” tmux session, create it anew with
tmux new -s dev, and with
Ctrl-A Ctrl-R I then resurrect the last automatic save of my previous session as squirrelled away by the tmux-continuum plugin.
There is one little fly in the ointment that I’ve not yet been able to sort out on macOS, and that is the first pane in the first window always gets renamed to “reattach-to-user-namespace” instead of “admin”. Even though I’ve turned off the settings related to renaming windows based on what’s currently running in my
~/.tmux.conf, this always happens when resurrecting and that program is run to ensure copy/paste between tmux and the macOS clipboard works as expected. I think it’s possibly related to how I use the tmux-yank and tmux-sensible plugins ¯\_(ツ)_/¯. It’s not a big deal though, I rename the window once and then don’t need to think about it for weeks at a time usually.
Otherwise it’s incredibly useful to have all my shell sessions restored with the current working directories as I left them. And as seen in the gif, if there was a program running that tmux-resurrect deams safe to restart like
vim, then they come back to life too!
Another thing I love about using tmux as my primary means of accessing the command line is its excellent support for resizing.
In the above screenshot my main local terminal window now only takes up half of the screen, there’s two other terminals open in the other half, one logged into a server created with SpinupWP, and the other my ancient Mac mini running NixOS.
Even with the bumped up font size I’ve used for your viewing pleasure 😉, tmux has smoothly resized its panes and kept all the ratios intact.
If I want to see more of a pane while still keeping the window a little more cramped than normal, I can “zoom” the current pane with
I love that feature, great for quickly reviewing log files (etc.) that have long lines that would normally wrap in a smaller terminal pane.
Vim Key Navigation
While I’m no expert, and still only know enough key commands to get me by, I’ve been using Vi and Vim for a little over 23 years now, so it’s how my hands do text editing. Apart from using Vim for general text editing, I generally use “vim mode” in everything I can, including in my IDE and the bash command line.
As such it’s great that if I want to scroll back up or move around in the output of my current tmux pane, I can simply hit
Ctrl-A [ and then use the usual Vim keys such as
Ctrl-F for paging up and down without leaving the keyboard. All the other usual Vim navigation keys work too.
There’s a lot of keyboard shortcuts for getting around and performing actions in tmux, and you can customize them to your liking easily.
What Are You Waiting For?
Hopefully that’s enough of an overview of why I think tmux is the bee’s knees for not just remote command line work but also on your local development machine.
If you’ve not tried tmux before, what are you waiting for?! It’s easy to install on pretty much any Unix like machine through whatever package manager you use, e.g.
pacman, and even on Windows!
If you give tmux a go for the first time let us know how you get on in the comments, and if you’re already a daily tmux user why not share your tips and tricks in the comments too?