An easy and practical introduction to tmux

Published:  21/05/2021 15:30

Introduction

Tmux is a one of the two main terminal multiplexers commonly used in the Linux (and BSD) world (the other being screen, which we'll mention a few times in the article).

What's a terminal multiplexer you ask? It's an old concept of a program built on top of a system terminal that would easily allow you to open multiple pseudo-terminals in a single terminal session.

They also usually provide ways to:

  • Rearrange terminal sessions on a single screen;
  • Detach a terminal session, keeping it running, and allowing you to re-attach it later on even after logging out completely;
  • Copy-paste and scroll back or search terminal session with or without a graphical interface (which means it can be very handy in SSH sessions to servers).

Most of the graphical terminal clients (yes, even on Windows) will allow you to open multiple terminal tabs and sometimes even lay them out on the same screen, for instance I heavily use Tilix which does just that:

Showing multiple panels opened on Tilix

Tmux is different in that it always works, even with no graphical interface installed (e.g. when connected to a server using SSH) is very lightweight, and has the "detach session" feature.

Showing multiple panels opened on Tmux

Logging out from an SSH session (or any terminal session for that matter) kills the terminal process as well as all of its children. Basically, everything is gone. With tmux, you can keep the session alive in the background with whatever commands were running in there before.

Of course there are various ways to manually detach processes (e.g. using the fork system call) so that they're no longer a child of a console process but this isn't trivial at all and tmux helps you do it from the topmost level with a single key stroke.

Another advantage of tmux is that you can bring it everywhere: on MacOS, Windows (using WSL), Linux, BSDs, server environments and more. Learn once, earn increased productivity on most of the possible work environments you could come across.

Hurdles to starting using tmux

As we mentioned before, tmux doesn't need a graphical user interface but it doesn't need mouse support either, it's built in a fashion similar to the vi and vim editors in that you can do everything using the keyboard.

It doesn't quite get as far into it as vim does but many tmux users will customize it to have better key binds, usually close to vim and, if possible, on the home row of the keyboard.

The issue is that the early investment in learning tmux (or vim for that matter) is very high and not straight-forward in any way.

For instance, both vim and tmux support multi-pane views and have their own copy-paste buffers which aren't shared (they aren't shared with the system either if you're using a graphical interface).

Fixing these issues requires plugins and fiddling with tmux's config and all of that is quite a stiff wall to climb for someone that's not used to these kinds of tools, even though you can't argue how light they are and how practical it could be to bring your own development environment to modest servers with no issues.

I also perfectly understand how doing everything with the keyboard and optimizing keys to be very easily accessed makes you more productive in the end but it also tends to gimp any adoption of the tool whereas, if you look at something like Visual Studio Code, it's very easy to adopt but can be hard to master would you want to do everything as fast as possible or even with vim bindings (there are extensions for that).

I think there's value in fist getting to know a program before knowing if you want to go all in with it but I'm also aware some people have a personality time that is either go all in or don't go, which is fine, but this article is probably not for them.

Installing tmux

A package exists as tmux in most package managers, it's pretty quick to install and should have almot no dependencies.

Example for Debian-based distributions:

apt update && apt install tmux

Before we start — tmux's main concepts

Just introducing a few concepts and keywords that'll apply all throughout this article.

Sessions

Starting tmux can be just a simple as invoking tmux in command line.

This creates a new session, which is the detachable set of multiple terminal panels and tabs that you're going to use. In reality, it's a tmux process that exists on its own and is not a child of the current terminal session.

An implication is that if you suddenly lose your internet connection while you were inside of a tmux session on a remote server, the session won't be lost, you can immediately go back to it after reconnecting to the remote server by re-attaching it.

We'll see later on how to create multiple sessions and attach or detatch them.

The "prefix" keys

Most of the keyboard shortcuts in tmux require pressing the "prefix keys" first.

You can see it as a way to access tmux shortcuts so that it (hopefully) doesn't interfere with the shortcuts of any console application you'd want to run inside of tmux.

The other terminal multiplexer, screen, uses Ctrl+a as its prefix, whereas tmux uses Ctrl+b by default.

It's popular to change it to be Ctrl+a, but I personally keep Ctrl+b. I'll explain how to change it later on when we get to write a starting config file for tmux.

From now on remember most of the shortcuts in tmux require the prefix to be entered first.

Windows

In the context of tmux, windows are the equivalent of tabs in other terminal emulators: terminal sessions that take up the whole screen and can be accessed using a keyboard shortcut (or the mouse).

You can create new windows at any time and tmux will keep them saved inside of the session, so that you get all of your previous windows back when you re-attach a previous session.

Edit mode

Because tmux works without any mouse support in a purely textual barebones terminal, it had to provide a way to browse previous output, select text and possibly copy/paste said text without relying on any other program (e.g. a graphical interface).

These operations require entering "Edit Mode", which is somewhat similar to "Visual mode" in vim as it allows you to scroll back and select text.

Suggested config file to start with

The tmux config file is supposed to be in your home directory as ~/.tmux.conf.

Let's create it and add the following content (requires tmux version > 2.1):

set -g mouse on
setw -g mode-keys vi
bind-key -n M-c copy-mode
bind-key C-b last-window
bind-key C-x setw synchronize-panes
set -g history-limit 10000

Where, to summarize:

  • Mouse mode is enabled — Allows clicking a pane to switch to it and scrolling with the mouse wheel or gestures in Edit Mode, would also enable mouse selection;
  • In Edit Mode, the key binds match the most common ones from vi;
  • Can enter Copy Mode with Alt+c;
  • Binds the prefix key (Ctrl+b) as "switch to last window" so that Ctrl+b+b switches between the two last used windows (screen has that bind out of the box) - Try it out as switching around between two windows is a very common use case;
  • Prefix,Ctrl+x will send input to all the panels on the current window (explained in the panels section);
  • Changes scroll-back history limit to 10000 (defaults to 2000); On a fast work machine you can set it higher with a quick friendly reminder that the less and more utilities are meant to page large outputs if that's what you're after.

I tried to keep the config as short as possible but you'll find that some key binds are awkward to do on AZERTY keyboards (or any keyboard for some of them) and you may want to eventually rebind them.

Rebinding the prefix

Here are two lines to add to the config file to change the prefix to Ctrl+a:

unbind C-a
set -g prefix C-a

The C modifier means Ctrl, whereas M is Alt and S is Shift.

How to use tmux

Sessions

When not inside of a session already, you can create one by just hitting:

tmux

You could name it before creation like so:

tmux new -s <SESSION_NAME>

Or, rename it from inside the session by hitting Prefix,$.

The main reason to name a session is to make it easier to find out what it was about when re-attaching that session later on.

About that, to detach a session you just use Prefix,d.

The easiest way to list all the running tmux sessions is to run:

tmux ls

To attach a previous session, you have a few options:

Attaching the latest session

tmux a

Attaching an unnamed session

tmux a #<NUMERIC_ID>

Attaching a named session

tmux a -t <SESSION_NAME>

Forcefully kill a session

tmux kill-session -t <SESSION_NAME>

Windows

As we said before, windows serve the same purpose as what would be called "tabs" on most terminal emulators. The list of opened windows for the current session will appear at the bottom of the screen:

Showing where the windows are displayed in tmux

Here are a bunch of what I consider the most useful shortcuts when it comes to windows:

  • Prefix,c — New window + switch to it;
  • Prefix,n — Show next window;
  • Prefix,p — Show pevious window;
  • Prefix,[number key] (requires holding shift on AZERTY keyboards) — Switch to the corresponding window by number (see screenshot above - starts at 0);
  • Prefix,w — Open an interactive window selection screen with previews;
  • Prefix,, — Rename the current window;
  • Prefix,& — Kill the current window (asks for confirmation) - It's better to just use "exit", kill is when the task hanged;
  • If you have the config suggested above, hitting b twice while holding Ctrl will switch between the two latest used windows.

Panels

Panels should probably only be used when you're comfortable with windows. They allow splitting a window in multiple terminal instances (still counts as one window).

  • Prefix,% (requires pressing Shift on AZERTY layouts) — Open a new terminal panel on the right;
  • Prefix," — Open a new terminal in the bottom;
  • Prefix,! — Convert current panel into a window;
  • Prefix,Ctrl+x (if you have my config file) — Send input to all the panels in the window - Can be used to enter commands simultaneously on multiple servers, for instance - Use the key bind again to disable it;
  • Prefix,x — Kill current panel.

To switch input to panels you can use Prefix + the arrow keys, or click a panel with the mouse if you have mouse mode configured.

You can also use Prefix+; to go to the most recent pane — Can be used to switch back and forth between the two last used panels.

Rearranging panels

Quick note: there is no way to easily rearrange horizontal panels unless rotating or rearranging the entire layout, see below.

  • Prefix,{ (requires pressing AltGr on AZERTY keyboards) — Move current pane to the left;
  • Prefix,} (requires pressing AltGr on AZERTY keyboards) — Move current pane to the right;
  • Prefix,Ctrl+o — Rotate everything counter-clockwise;
  • Prefix,Alt+o — Rotate everything clockwise;
  • Prefix,Space — Cycle through different preset layouts.

Resizing panels

Resizing panels is easily done using the keyboard.

You just press Prefix, then the arrow keys while still holding Ctrl (technically it's Ctrl+[Arrow Keys] because your prefix might not have Ctrl in it).

The Copy Mode

Windows and panels are all well and good but if you try to work on tmux after having only used graphical terminals you'll find the biggest hurdle is how to browse back the output and copy paste text.

We enabled "mouse mode" earlier to make onboarding easier, an advanced tmux user would do everything with the keyboard.

The bindings for Copy Mode will also mostly be the same used in the vim text editor but you don't have to know anything about vim to get started here.

Entering or leaving Copy Mode

If you have the config provided above, Alt+c (without the prefix) enters copy mode.

Scrolling with the mouse wheel or a touchpad gesture should also automatically enter copy mode.

You'll know when copy mode is active because a line counter will appear on the top right.

To exit copy mode, just press q.

The copy mode shortcuts do not require the prefix.

If you have the config provided in an earlier section, the vim bindings are enabled in copy mode, although the behavior is not exactly that of vim for the searches.

  • g — Go to top;
  • G — Go to bottom;
  • h,j,k,l — Scroll through the terminal instead of using arrow keys (though arrow keys should also work);
  • / — Open a prompt to search forward;
  • ? — Open a prompt to search backward;
  • n — Next search occurence;
  • N — Previous search occurence.

I'm not describing the copy/pasting as I consider this advanced usage, but with mouse mode inside of a graphical interface you can copy/paste by selecting using the mouse and your terminal emulator copy/paste shortcuts. There's just one caveat in that you'll probably have to hold Shift during the selection.

Sometimes mouse mode is actually bothering me, it can be turned off by bringing up the tmux prompt using Prefix,: and entering:

set -g mouse off

Advanced

There's a lot you can do with tmux that wouldn't fit in this article which is already arguably too long.

Would you want more, don't hesitate to let us know in the comments below.

Comments

Loading...