Fedora Magazine: Use tmux for a more powerful terminal

Some Fedora users spend most or all their time at a command line terminal. The terminal gives you access to your whole system, as well as thousands of powerful utilities. However, it only shows you one command line session at a time by default. Even with a large terminal window, the entire window only shows one session. This wastes space, especially on large monitors and high resolution laptop screens. But what if you could break up that terminal into multiple sessions? This is precisely where tmux is handy — some say indispensable.

Install and start tmux

The tmux utility gets its name from being a terminal muxer, or multiplexer. In other words, it can break your single terminal session into multiple sessions. It manages both windows and panes:

A window is a single view — that is, an assortment of things shown in your terminal.

A pane is one part of that view, often a terminal session.

To get started, install the tmux utility on your system. You’ll need to have sudo setup for your user account (check out this article for instructions if needed).

sudo dnf -y install tmux

Run the utility to get started:

tmux

The status bar

At first, it might seem like nothing happens, other than a status bar that appears at the bottom of the terminal:

The bottom bar shows you:

[0] – You’re in the first session that was created by the tmux server. Numbering starts with 0. The server tracks all sessions whether they’re still alive or not.

0:username@host:~ – Information about the first window of that session. Numbering starts with 0. The terminal in the active pane of the window is owned by username at hostname host. The current directory is ~ (the home directory).

* – Shows that you’re currently in this window.

“hostname” – the hostname of the tmux server you’re using.

Also, the date and time on that particular host is shown.

The information bar will change as you add more windows and panes to the session.

Basics of tmux

Stretch your terminal window to make it much larger. Now let’s experiment with a few simple commands to create additional panes. All commands by default start with Ctrl+b.

Hit Ctrl+b, “ to split the current single pane horizontally. Now you have two command line panes in the window, one on top and one on bottom. Notice that the new bottom pane is your active pane.

Hit Ctrl+b, % to split the current pane vertically. Now you have three command line panes in the window. The new bottom right pane is your active pane.

Notice the highlighted border around your current pane. To navigate around panes, do any of the following:

Hit Ctrl+b and then an arrow key.

Hit Ctrl+b, q. Numbers appear on the panes briefly. During this time, you can hit the number for the pane you want.

Now, try using the panes to run different commands. For instance, try this:

Use ls to show directory contents in the top pane.

Start vi in the bottom left pane to edit a text file.

Run top in the bottom right pane to monitor processes on your system.

The display will look something like this:

So far, this example has only used one window with multiple panes. You can also run multiple windows in your session.

To create a new window, hit Ctrl+b, c. Notice that the status bar now shows two windows running. (Keen readers will see this in the screenshot above.)

To move to the previous window, hit Ctrl+b, p.

If you want to move to the next window, hit Ctrl+b, n.

To immediately move to a specific window (0-9), hit Ctrl+b followed by the window number.

If you’re wondering how to close a pane, simply quit that specific command line shell using exit, logout, or Ctrl+d. Once you close all panes in a window, that window disappears as well.

Detaching and attaching

One of the most powerful features of tmux is the ability to detach and reattach to a session. You can leave your windows and panes running when you detach. Moreover, you can even logout of the system entirely. Then later you can login to the same system, reattach to the tmux session, and see all your windows and panes where you left them. The commands you were running stay running while you’re detached.

To detach from a session, hit Ctrl+b, d. The session disappears and you’ll be back at the standard single shell. To reattach to the session, use this command:

tmux attach-session

This function is also a lifesaver when your network connection to a host is shaky. If your connection fails, all the processes in the session will stay running. Once your connection is back up, you can resume your work as if nothing happened.

And if that weren’t enough, on top of multiple windows and panes per session, you can also run multiple sessions. You can list these and then attach to the correct one by number or name:

tmux list-sessions

Further reading

This article only scratches the surface of tmux’s capabilities. You can manipulate your sessions in many other ways:

Swap one pane with another

Move a pane to another window (in the same or a different session!)

Set keybindings that perform your favorite commands automatically

Configure a ~/.tmux.conf file with your favorite settings by default so each new session looks the way you like