tmux, oh-my-zsh and aliases: 3 tips to suck less at Unix terminal

Matteo Barbieri
Jun 21 · 8 min read

If you have less than 30 seconds, here’s the core of this article!

TL;DR

  1. For the love of God, always use tmux (https://github.com/tmux/tmux)
  2. bash is ugly AF: use zsh instead, and especially oh-my-zsh (and its themes) (https://ohmyz.sh/)
  3. Use aliases to save A LOT of time.

Context

If I’m not on a call with someone telling them to either use headphones or mute themselves when they’re not speaking, I spend a lot of my time at work using the terminal. Whether I’m training a model, writing code, or something else, my screen is usually filled with a black rectangle with text on it.

Because of this, at some point in my life I decided to spend some time trying to make it a nice environment with colors, plugins and various quality of life improvements.

And boy, what a change.

It’s one of those things that you don’t notice until you have them, and then you ask yourself how the hell did you manage to survive without until now.

I’d like to be very clear: these are not changes that make your terminal look prettier (although that’s definitely a nice thing), they are significant improvements that allow you to do whatever you’re doing faster and better. Let’s check them out.

Photo by Martin Katler on Unsplash

tmux

If a good chunk of your job requires using the terminal, chances are that you’re going to need more than one prompt available: you’ll have one to run some scripts, one open on the directory where the data is stored, maybe a couple for monitoring the machine’s resources and possibly more. Normally this would require you to open as many instances of your terminal as the number of tasks that you’re currently undertaking. That’s not great, since this will force you to spend a non-trivial amount of time pressing ALT-TAB to move between all those windows, which by the way are potentially indistinguishable from each other, so good luck with that. Plus, it clutters your desktop as you probably don’t need to look at more than one or two simultaneously. And that’s where tmux comes in very handy.

tmux is a terminal multiplexer, that is a sort of an additional layer which runs on top of an existing shell session. Once you run tmux on your one terminal instance, a new tmux session will be created.

A new tmux session. Image by author.

The only visual difference with respect to a regular terminal session is the green bar at the bottom, but from here you have access to a lot of options that allow you to do all of your work from a single terminal instance.

Within a singletmuxsession, it is possible to have multiple windows, that is independent screens if you will, which in turns can contain one or more panes, which are the actual interactive shells. This means that instead of having multiple terminals open for the various tasks, it is now possible to organize them in separate tmuxwindows within the same session, name them and jump from one to the next without having to shuffle through all of them hoping to end up on the right one by chance.

Moreover, within the same window it is possible to split the screen into several independent panes, which allow you to keep an eye on multiple things at once, if needed.

The beauty of multiple panes. Image by author.

But the best has yet to come: tmux actually uses a server/client paradigm, you can actually detach from an existing session without terminating it, so that you can reattach to it in the future. In practice, this means that once your workday is over you simply leave the session you have started, with all of its windows and panes still running, and the next day you can simply reattach to it and pick up from where you have left off, without having to recreate all windows.

This is particularly useful if you have to work on a remote machine where you need to have several shells open at the same time: with tmux you can first access that machine via ssh, and once you’re there create a session. There are several good reasons to do this:

  • Every new session, window, and pane will be created on the remote machine you’re accessing, so you won’t have to do ssh again every time you create one of those.
  • If your connection drops, all processes within the session will keep running as if nothing happened (on a “regular” ssh session, those would be interrupted), and once the connection is restored you can simply re-join the session. This is particularly important if you’re running any kind of task that takes a significant amount of time to complete, such as training a neural network.

I hope I’ve convinced you to give it a try, it has been a game-changer for me and now I can’t imagine working without it.

Anyway, this was just the main course, let’s see what else is on the menu.

[oh-my-]zsh

On a standard Linux installation (let’s take Ubuntu for instance), the default terminal looks something like this:

A very sad terminal. Image by Author.

The default shell is bash: simple, fast, does the job, not particularly fancy.

Well, that’s actually quite the understatement: it is rather ugly and does not really give you any information aside from “what folder you’re currently in”. Don’t get me wrong, if you don’t spend too much time on it and basically open a terminal only if forced, that’s all right, but chances are that if you have been reading this far you probably need to work with it for a non-trivial amount of hours every week.

If that’s the case, you might want to consider investing a bit of time in making it a more comfortable environment, same as you would adjust your workstation or office desk in any other type of job.

My suggestion for this is simple: ditch bash, switch to zsh and install oh-my-zsh, a framework that comes with a ton of plugins that will make your shell experience much, much better. Here’s an example of what your shell could look like using one of the themes available for one of oh-my-zsh’s most popular plugins (powerlevel10k):

This is how your terminal could look like. Image from https://github.com/Powerlevel9k/powerlevel9k/wiki/Show-Off-Your-Config

Much better, right? Aside from the nice little icons (you will need to install a special set of fonts for those to work, but that’s pretty easy to do and the instructions are on the plugin’s page) and the date and time on the right, you can see the git plugin in action on a couple of those lines (the stuff in yellow), showing the state of the repository you’re in. It might not seem much, but being able to see at a glance in which branch you are, if you have new or modified files in your repo, or if you have unpushed commits that still need to be pushed, will likely save you from some nasty headaches in the future.

And git is just one of the services that has a dedicated plugin: there’s one for showing the currently active conda environment, there’s one for kubernetes, one for nodejs, and many more!

Whatever your job is, oh-my-zsh has something to make your life in the terminal a little bit better, and definitely gives you something less boring to look at. And while this might seem a secondary aspect, consider that if you’re spending several hours each day looking at the terminal, it does make a difference whether it’s an ugly black box or a more colored, cheerful one. I mean, look at how cute the house icon is!!!

aliases

Photo by Vlad Hilitanu on Unsplash

Last, but not least, let’s talk about aliases.

An alias is a custom-defined shortcut for an arbitrarily long command. The syntax is pretty straightforward: alias la='ls -lah'. Now every time you use la in the terminal (at the beginning of the prompt), the content of the alias will be executed instead. Your default shell probably already has some default aliases set up (la is probably one of them, although not necessarily in the exact form above), but you can define your own. In practice, the best way to do that is to put them in a file .aliases (one alias per line) in your home folder and make sure that it’s sourced whenever you open a new terminal window (I think this happens by default for many shells, anyway check your {bash,zsh}rc).

The best thing about aliases however is that they can (and should) be used for partial commands as well, as in at the beginning of longer commands that you will then complete with additional arguments. The prime example for this is aliasing conda activate and conda deactivate to .a and .d respectively. Activating an environment then becomes as easy as .a pytorch.

Examples for smart uses of aliases are:

  • the aforementioned shortcut to activate/deactivate conda environments
  • one-letter access to programs that you use often (I have v for vim)
  • add or improve commonly used commands for variations with custom options enabled by default (for reasons that have been lost in time, I have replaced tmux with tmux -2. No idea why)
  • quick ssh access to specific remote servers (make sure you have set up key authentication for maximum comfort):
    alias s.gipsy-danger='ssh matteo@gipsy-danger'

While this might not seem much, it will actually save you a lot of time. Not just because you’re going to type fewer characters on the keyboard, but mainly because shorter commands means fewer typos.

Bonus feature: people will think you’re a wizard! 🧙🏼

There are however cases in which an alias is not the right solution: if you need something that requires multiple lines of code to be written. For that, you’ll need to write a shell script of some sort. Hey, nothing’s free!

That’s all for now. I have a few more tricks up my sleeve that humanity might benefit from, if you enjoyed this content stay in touch. And pimp your terminal, life’s too short for monochrome screens!

Geek Culture

Proud to geek out. Follow to join our 1M monthly readers.