I’ve been a Linux admin since the late 90s, and for many many years I’ve heard the command “tmux” mentioned. I knew it had something to do with terminals, and that it might be something like screen, but I never really paid any attention. But a few days ago it was mentioned in an article and I thought, hey - I should check it out. So I did!

Firstly, what is tmux?

Tmux, short for terminal multiplexer, is a powerful tool that allows you to manage multiple terminal sessions within a single terminal window. It’s like having multiple desktops for your command line, enabling you to switch between different tasks or projects without losing your work.

Compared with screen, tmux is generally considered to have an easier to use status bar, better naming in the individual sessions, and you can easily set up different text/styles for current window, windows with activity, etc. and you can put things on the left and right of the status bar, including shell commands that can be run at a specified interval (default 15s).

Why use such a thing?

  • Efficiency: You can run multiple commands or applications simultaneously in different panes within a single terminal window.
  • Persistence: Detach from a running session and reattach later, even after closing your terminal or losing your connection. Imagine having to run a long database command, but being connected by a flaky signal.
  • Organisation: Manage multiple projects or tasks in separate windows or sessions.
  • Screen Sharing: Share specific terminal sessions with others.

Installation

Tmux is often pre-installed on most Linux distributions and macOS. To check if it’s installed, open your terminal and type:

tmux -V

If installed, that should return the version. If it’s not installed, you can usually install it using your package manager:

Debian / Ubuntu

sudo apt install tmux

Fedora / RedHat / CentOS

sudo yum install tmux

MacOS (using homebrew)

brew install tmux

Starting and Stopping

tmux is based around sessions. To start a new session in tmux, simply type tmux new-session in your terminal. Once you are in tmux, it will mostly look like any other terminal session, however there will be a green bar at the bottom. This is the default view after starting a new session:

starting

To get out, you can type exit if you’re in a single pane, and you’ll return from where you were.

An important note is that exit is not the only way to get out, and usually not the best way. For that you have detach. However before you get to that, you first have to cover the prefix…

Using Prefix

All commands in tmux require the prefix shortcut, which by default is ctrl+b. You’ll be using the prefix a lot, so best to just commit it to memory. After entering ctrl+b you can then run a tmux command, or type : to get a tmux prompt. “ctrl+b :” to get tmux command prompt:

commands

When entering the prefix, tmux itself will not change in any way. So, if you enter ctrl+b and nothing changes, that does not necessarily mean you typed it wrong.

Attach, Detach & Kill

As mentioned, a better way to get out of a session without exiting out of everything is to detach the session. To do this, you first enter the prefix command and then the detach shortcut of d:

ctrl+b d

This will detach the current session and return you to your normal shell.

However, just because you’re out doesn’t mean your session is closed. The detached session is still running and available, allowing you to pick up where you left off. To check what sessions are active you can run:

tmux ls

The tmux sessions will each have a number associated with them on the left-hand side (zero indexed as nature intended). This number can be used to attach and get back into this same session. For example, for session number 3 you would type:

tmux attach-session -t 3

or you can go to the last created session with:

tmux a

Naming Sessions

Now you could just rely the session numbers, but it would make your life much easier if you give your sessions names based on their intended use.

To start a new session with a specific name you can just do this:

tmux new -s [name of session]

With named sessions in place, now when you do tmux ls you see the session name instead. Likewise, you can then attach a session by using the name:

tmux a -t [name of session]

Note that you substituted a for attach-session to help save on keystrokes.

Managing Panes

In a GUI desktop environment, you have windows. In tmux, you have panes. Like windows in a GUI, these panes allow you to interact with multiple applications and similarly can be opened, closed, resized and moved.

Unlike a standard GUI desktop, these panes are tiled, and are primarily managed by tmux shortcuts as opposed to a mouse (although mouse functionality can be added). To create a new pane you simply split the screen horizontally or vertically.

To split a pane horizontally:

ctrl+b "

To split pane vertically:

ctrl+b %

split

You can split panes further using the same methodology. For example, in the screenshot above, the screen was first split horizontally using ctrl+b " and then split vertically within the lower pane using ctrl+b %.

To move from pane to pane, simply use the prefix followed by the arrow key:

ctrl+b [arrow key]

Resizing Panes

Let’s say you need a little extra space in one of our panes, and want to expand the pane down a few lines. For this, you will go into the tmux prompt:

ctrl+b :

From there you can type resize-pane followed by a direction flag: -U for up, -D for down -L for left and -R for right. The last part is the number of lines to move it over by.

As an example, if you are in the top pane and want to expand it down by 2 lines, you would do the following:

ctrl+b :
resize-pane -D 2

split

Going Further

By mastering tmux, you’ll significantly improve your command-line efficiency and productivity. Start experimenting with these commands and discover how tmux can transform your terminal experience.

The possibilities here are just the tip of the iceberg. If you are ready to go even further down the rabbit-hole, the below links should help fill the gaps:

And my personal fave:

I’m personally quite interested in deep diving into an alleged integration with iTerm2 on my Macbook, and understanding what plugins can do with it. I’ll certainly be trying this instead when I would have previously reached for screen.

A Cheatsheet

Start a new named session:

tmux new -s [session name]

Detach from a session:

ctrl+b d

List all sessions:

tmux ls

Attach to a named session:

tmux a -t [name of session]

Kill a named session:

tmux kill-session -t [name of session]

Split panes horizontally:

ctrl+b "

Split panes vertically:

ctrl+b %

Kill the current pane:

ctrl+b x

Move to another pane:

ctrl+b [arrow key]

Cycle through panes:

ctrl+b o

Cycle just between previous and current pane:

ctrl+b ;

Kill tmux server, along with all sessions:

tmux kill-server