I Finally had My TMUX Moment

In spring, there are a few weeks in Germany with public holidays sprinkled over weekdays. In many cases, taking an additional day off results in a very long weekend that can, of course, be used to learn new stuff. This time around I particularly had a look tmux which, after the expected steep learning curve, makes working in the shell so much easier.

A tmux example

tmux – Throw Your Mouse and Shell Windows Away – Almost

On my Linux boxes I’m using the shell a lot either because they are servers and don’t have a GUI in the first place or because it’s often much more convenient then clicking around in a GUI anyway. One thing that happens quite quickly then is that at some point, a lot of shell windows are suddenly open in different sizes and at different locations of the screen, partly overlapping or hidden because I’ve minimized them at some point in time. In other words the mess on the screen grows quite quickly and if I didn’t have shell prompts in different colors for different machines it’s easy to loose an overview of what is running where.

Multiplexing Shells

An excellent way to fix this is tmux, which is short for terminal multiplexer. As the name implies, the main purpose of tmux is to multiplex several shell sessions in a single shell window by splitting the window into different text based sub-windows (panes) with a thin border around each. The screen shot above shows how this looks in practice with three shells managed by tmux in a single shell window. At the right, ‘htop‘ is running in a shell so I can see what is going on in the machine while at the bottom, a ‘tail -f‘ gives me real time information from the system log. The upper left shell is for typing in commands.

The tmux Server in the Background

The way this works is that when tmux is started (by typing ‘tmux’ in a shell window), a background daemon process is started that creates a new shell instance that is connected to the background process and not to the shell window. When creating additional windows, referred to as ‘panes’ in tmux, additional shell processes are tied to the tmux background process. Apart from the steep learning curve for keyboard commands, the idea of having another background process running that might mess with my standard shell interaction was a bit scary which is why I held-off until now to learn about tmux. But I need not to have worried because tmux leaves one completely alone. I can open new shell windows on the desktop and while not starting tmux in them, they are just shell windows like before. No difference, they’ve got nothing to do with tmux. This is important to realize!

Preserve Remote Shells When Connectivity Drops

Apart from opening panes (windows) with sub-shells, the other great thing about tmux is that it can also be used remotely via ssh. When ssh’ing into a server and starting tmux in the ssh session, the tmux background process is started on the server. A single ssh session is then enough to open as many shell panes (windows) in a single ssh session. This become especially useful when interacting with a server while moving. Should connectivity drop or your have to close your notebook because you are on a train and have arrived at your destination, the ssh connectivity will break but the tmux background process on the remote sever just continues running. Once you are ready to continue working on the remote server, the only thing that is needed is to start another SSH session and to type in ‘tmux’ again. Everything is still running, commands that take a long time and were still running when one had to close the notebook are still running and might have finished in the meantime. And also, all panes are still arranged like they were before connectivity was interrupted.

Blue Lines and A Bit of a Mouse

The learning curve for tmux is a bit steep because everything is controlled with keyboard shortcuts. CTRL-B starts a keyboard control sequence that is then followed, by example by a ” or % character for opening new panes, a ‘c’ for opening a new virtual tab, by pressing the arrow keys to resize the panes or to change the active pane, etc. etc. There are a number of good ‘cheat sheets’ our there such as this one and this one that will help at the beginning to find the basic keyboard shortcuts one needs, and to come back later to for more esoteric stuff that one forgets more easily.

I have to admit that while doing everything with keyboard shortcuts is great, especially in environments without a lot of space for a mouse, I do quite enjoy using the mouse to resize panes, to select the active pane for typing in commands and to use the scroll wheel to go through shell window output. By default, any kind of mouse interaction is disabled in tmux. But one can activate mouse support by creating a .tmux.conf file in the home directory and then putting the following command inside:

setw -g mouse on

After creating or updating the file in a tmux pane, e.g. using ‘nano‘, the configuration is then activated without leaving tmux by reloading the settings with ‘CTRL-B’, then pressing the ‘:’ key and then typing in:

source-file ~/.tmux.conf

As I didn’t like the command bar being green, I added the following two commands in the config file to give me a blue command bar and blue pane borders:

set -g status-bg blue
set -g pane-active-border-style fg=blue

Yes, the learning curve is a bit steep but then, there are the cheat sheets and Internet search engines for quick results…