Why Learn Tmux

2019-06-09

When VSCode came out with split terminal function I was surprised, why not just use tmux? It’s one of those tools which is commonly found, but a lot of people think it’s sourcery, every time people see me using it I get hacker points ™️ by default.

The config A.K.A. .tmux.conf

One of the obstacles in getting started is the ancient default config. The mouse doesn’t work and key bindings are a bit off. To fix that you’ll need to edit the ~/.tmux.conf

Mouse

The first thing which causes panic in most of the people is the missing mouse scroll and resize. To fix that add:

# Allow mouse usage
set -g mouse on

Better shortcut

Another thing I personally prefer is getting the better shortcut for calling the tmux commands. I remap it to Ctrl-B with:

# Remap <Ctrl-A> as modifier.
unbind C-b
set -g prefix C-a
bind C-a send-prefix

Zshell

If you are not using zshell you should. And to make it work with the tmux you will need to set it as default like:

# Use `zshell` or whatever is used
set -g default-shell $SHELL

This would also work for other shells as long as it is set to be your default shell.

Vim

Vim users might see a weird lag then pressing Esc fix for that:

# Kill the weird lag
set -sg escape-time 0

And colours might look a bit off, fix is there as well:

# Vim colour fix
set -g default-terminal "screen-256color"

Using it

Once the config is done it’s time to use it.

To start tmux launch if with:

tmux

Further down I will use Prefix for the keys we bound as hotkey. E.G. `Prefix

Panes

Splitting

Tmux is a terminal multiplexer, which is a fancy word for the split screen…

command result
Prefix + " Horizontal split.
Prefix + % Verical split.

I remember it as % sign looks like some fruit chopped in half from the top. And the other one is horizontal 😅. In practice mostly using vertical split.

There are a few ways to jump between panes. The one I use the most often is Prefix + o which just cycles through the panes in the current window.

If you want to be more precise you can use Prefix + <arrow key> with the direction you want to focus on. But this forces you to move the fingers from home row.

Another option is to use pane numbers. To do this press Prefix + q, then press the number of the pane while they are displayed. Kind of feels like a wack a mole, but it works.

To close the pane you can use the same shortcut you would use for closing the terminal session Ctrl + d.

Windows

Creating

The area you can split the pane is called window in tmux. And just like you can have multiple panes, you can create multiple windows with Prefix + c. Quite simple there C for create.

At the bottom left of the window, you will be able to see the numbers next to the pane names. To use them simply press Prefix + <number>. While this is a more precise way to navigate the windows I prefer to cycle through the panes with Prefix+ n and Prefix + p.

Sessions

Like panes live in windows, windows live in sessions. Just by running tmux in the terminal we spin up a session. Now to put the session in the background you can use Prefix + d.

Once back in your usual shell there you can see that session is still up with:

tmux ls

To get back to the session:

tmux attach

This will attach to the last active session.

If you have multiple sessions running you can attach by its number or name:

tmux attach -t <number>

To make sessions more useful you can add a name when starting a session

tmux new -s <name>

If you are working on multiple projects you might want to have a session per project. One of the handiest shortcuts I’ve found out just recently was `Prefix

But Why use tmux?

  1. You won’t accidentally kill processes. So many times I saw people running a job in the terminal and pressing cmd+q or something similar, just to realize that they just killed a compiler or some migration they were running on their machine. Using tmux means that you will quit the terminal app but you won’t kill processes which you are running in that session. So If you have to run a batch job or a server then you don’t risk killing it by closing a terminal

  2. It makes terminal close to ide, in the sense of ability to run debuggers, loggers, runners and etc. The ability to have multiple panes means you can run whatever you want. It’s like lego IDE. I personally will have one pane with web server logs another one for git, and something like node interpreter running for quick exploration.

  3. It’s universal, at least on Unix type systems. I’ve used Linux, macOS and even windows Linux subsystem, with the same config and same muscle memory sharing my config files as well. Learn once use almost ever which is always great.

  4. You don’t need that much to get started. You could start with the single pane for long running jobs or servers and then work from there to learn shortcuts and so, but after you use it’s irreplaceable.

Wrap up

I’m not a pro at tmux and there probably are even more shortcuts and tricks to it. But this is a list of configs, shortcuts and flows which worked for me to achieve my Plateau of Productivity

Bonus - Tmuxinator

If you are working on the same projects a lot you can create custom IDE experience by using tmuxinator. An excellent tool to define the sessions, windows and so on in declarative yaml file.