Working with tmux

Introduction

It's funny. We are often unaware of the tools that might help us the most. Especially in the world of software development -- or, really any industry involving technology -- many projects exist that would help us tremendously, were we only to stumble upon them.

In this article, I hope to introduce you to a tool that I use on a daily basis, but did not know I needed until I tried it: tmux.

(The remainder of this post will be primarily aimed towards those who use Linux/Unix-esque systems during development.)

The Problem

Depending on your terminal or terminal emulator, in addition to any window managers you might have (such as, say, xmonad), your workspace has the potential to become very cluttered; accordingly, your productivity inevitably suffers, and perhaps even your motivation. There are a variety of approaches to solving this issue. There must surely be some sort of objective criteria to determine the one that works for each individual, but I don't know what that is -- so, I'll force empirical testing upon you, the reader, and you can make a decision for yourself. (Besides, it won't take long to arrive at the aforementioned decision. Either this sort of thing will make sense to you, or you'll react in some presumably negative fashion.)

One of the solutions

The approach that tmux takes to solving our problem is to divide the terminal window into.. more terminal windows! That is, it multiplexes your terminal (hence, tmux). Specifically, tmux instantiates an arbitrary number of so-called "psuedoterminals". We could go on without knowing what those are, but I'm going to tell you anyways. Personally, I think that it's quite interesting.

What's a "Psuedoterminal?"

tmux did not invent the concept of psuedoterminals. They've existed for far longer than it has, in fact. To understand what a psuedoterminal is, though, let's make sure we know what a terminal is.

Searching around on our good friend, the Internet, will reveal that a terminal is simply a device meant for two things:

  1. Displaying output (e.g., lines of text);
  2. Accepting input (e.g., terminal commands).

Or, you can simply view a picture of one, and immediately understand what we're talking about.

So, if that's the definition of a computer terminal, what are we typing commands into these days? It is quite likely that you're using a terminal emulator -- a piece of software that is meant to simulate the computer terminals which were once widely used. (You'll still find physical terminals in some applications, though.)

Back to the subject of psuedoterminals: if a terminal, in our particular context, means "an emulated version of a device which accepts input and displays output", then a "psuedo" version of a terminal would have to mean something along the lines of "something that acts like an emulated video terminal, but isn't actually one". Indeed, this is the case: a psuedoterminal is a set of two software "devices" which come together to form a complete terminal emulation. They are called, typically, the master and slave devices.

To put it simply, to create a new psuedoterminal is to instantiate a new emulated terminal, and assign it to a new "master" psuedo-device. At the end of this chain, you end up typing commands in a terminal emulator, which controls a "master" psuedodevice, which itself controls its assigned "slave" psuedodevice. One then sees output from the master psuedodevice, just as they would expect to see from a terminal session.

Now, let's dive in to tmux itself a bit more.

Using tmux

Unlike some ubiquitous software, tmux still needs to be installed on most Linux distributions. How you do this is obviously dependent on which distribution you've chosen, but nearly all of them have pre-built binaries you can quickly grab and install (e.g., with sudo apt-get installtmux,sudo pacman -S tmux,sudo dnf install tmux`, and so on).

After you have installed tmux, running it is as easy as.. running tmux in your terminal emulator (See? Now you know what that is!). Since you haven't configured anything, you'll be greeted by a rather bland environment that may or may not support the functions and colors you just came from.

My environment looks like this, before I split panes and take names.



This can be fixed, of course - tmux offers many configuration options to the user, which can be viewed in its user manual (man tmux). We'll revisit that in a bit, though. For now, let us take a gander at the basic functionality that is already offered to you.

The main thing you'll be doing in tmux is dividing your terminal into several terminals. To do this, you must first send the prefix key (by default, <C-b>) to tmux, and then the command key corresponding to your desired behavior - in this case, you'd either hit the key for vertical splits (by default, %), or horizontal splits (by default,
"). As if by magic, you now have two separate terminal sessions in the same window! We know, now, that it isn't magic, however.

With only this command, you will quickly realize your desire to move between psuedoterminals; this is accomplished by hitting your prefix key and an arrow key that corresponds to the direction of the pane you want to focus on (e.g., <C-b> Left to move to the pane closest to the left from your currently focused pane).

These commands alone are enough to make you quite a lot more organized and productive in many environments (in my humble opinion). However, there are nearly sixty other command keys available to you, and a sizable amount of typed commands (which you may access via tmux [command]).

Of particular interest might be the following command keys:

Space - Cycle through pane layouts M-1 to M-5 - Select a specific pane layout C-[direction] - resize the focused pane by one pixel in direction M-[direction] - see above, but five pixels at a time x - kill this pane (prompts for confirmation) { and } - swap panes around with the prev/next pane [ - allows you to view psuedoterminal history (it actually enters another mode, called copy-mode, whose functions are
documented in the manual; viewing history is one of those functions.)

Be sure to take a look at the rest of them, though. As for commands, I frequent these:

tmux rename-window [name] - renames this window, or accepts -t as a flag for a specific window choose-tree - accepts many flags (-bcusStwW) and allows one to access "tree choice mode", where you may choose windows or sessions from a list. This is handy for working with many different projects at the same time. This command is quite extensive, so take a look at line ~757 of man tmux to understand it.

As mentioned, there are many others that you should read about.

Configuration Options

By now, you've probably discovered things you'd like to change about tmux. Perhaps you want a different preset key? Maybe you want to change the way you move around between panes? Or, do you just want to change the appearance of tmux itself? There is probably a way to
configure tmux in a way that satisfies. Let's run through my configuration file, so you can get a taste for things.

(Note: I based mine mostly off of an existing configuration file, but I can't seem to find it at the moment. However, many similar files exist.)

.tmux.conf

tmux This just makes sure my xterm bindings carry over to tmux.

tmux

Without this option, I get a sad, bland terminal session without the colors I instantiated the psuedoterminal from. This makes everything "just work" (your mileage may vary).

tmux

The reload command-key just sources the tmux configuration file again, when one makes changes and does not wish to close their session. This binds it to r (after hitting the prefix key).

tmux

This option is specifically for the copy-mode I mentioned earlier. You should go read the details on it, like I said earlier! Changes keys to "hjkl" for movement in this mode, for example.

tmux

This allows one to move between panes with vim movement keys instead of the directional arrows.

tmux

I have my Caps Lock key set to be another Left Ctrl, so this allows me to very quickly send the command prefix to tmux using <C-a>.

tmux

You can put whatever you want in the title bar. This one just displays your user, hostname, and alleged I.P. Address.

sudo apt-get install

The status bar appears to the right of the title bar. It's still at the bottom, though. This gives you an alternative to a "real" status bar (if you use xmobar, or something similar), with a short date and time output.

,

It's sort of annoying to use zero-based indices with windows, when the first window (window 0) is accessed with <C-a> <M-0> on the right side of the number row, and the second window (window 1) is accessed with <C-a> <M-1> on the left side of the number pad. This change makes the first window start at integer 1, instead.

I'd advise you to spend some time customizing your tmux configuration file for your needs, for both a greater understanding of the program, and a superior user experience.

How I work with it

I don't pretend to know the "right way" to work with tmux, but I hope that my workflow can inspire you to create one that works for you.

In general, I like to separate my projects into different windows; to create a new tmux window, you use the command key c. Inside of a window, I rename it according to its purpose (project-a-backend, project-a-content, etc) with tmux rename-window. Each window will have
a bunch of panes that correspond to that window name (the window named project-a-backend might have a docker container, a web server, etc).

Besides that, I just separate work stuff from personal projects with tmux sessions. To create a new session, you use tmux new -s [name here], and you access said session with tmux attach -t [name here].

..That's pretty much it. Once you know the relevant command keys, as well as the context in which they are used, it all seems pretty simple. But, it's a huge step away from having a disorganized mess of tabs/windows/workspaces just so you can have multiple terminals running at a time. Still, you might combine both approaches into something more suitable for your needs. I don't think there is much "wrong" you can find with new approaches, but I could, myself, be wrong about that.

Like I always say, I'd advise you just to experiment, and come to an objective decision of your own - flex your critical thinking muscles!

Further reading

An excellent tmux guide from ThoughtBot

Daniel Miessler's tmux guide, with a focus on remote sessions

Yet another tmux cheatsheet

Download our Incubator Resources

 

WANT MORE?

We’re known for sharing everything!

HANDBOOK

Save more time, get more done!

FREE HANDBOOK

Innovate from the inside

Written by
Cody Welsh 05 Sep 2017

Musician; software engineer; science doer.

YOU MIGHT ALSO LIKE

comments powered by Disqus