Terminal Multiplexing with tmux

📅 January 2, 2018
Do you find yourself opening a terminal for every new program or command-line based operation you want to perform?

How about opening a terminal to run a GUI program, such as VeraCrypt?

When multiple terminals are open simultaneously, they might make your desktop appear geeky (which is fun), but they can also clutter the desktop quickly. Is there a way to reduce clutter?

Terminal multiplexing is the process of switching among several open terminals (Bash sessions) in a single terminal to avoid the clutter.

One handy program is called tmux. tmux is full-featured program that runs…wait for it…in a terminal, and it allows you to open and manage multiple terminals within. The result? A single terminal that reduces desktop clutter.

Installation

tmux is available for free from the repository. In Linux Mint 18.3, install it from Synaptic or the command line. Yes, the command line installation will require a terminal.

sudo apt install tmux

 

Running

At the command line, enter tmux and press Enter. You will be greeted with what looks like a normal Bash shell with a green status bar at the bottom.

tmux opening screen. The green status bar at the bottom is fixed to tmux and provides information regarding terminal multiplexing.

As it is, tmux will operate as a standard terminal. Whatever you can do with Bash you can do with the Bash session running within tmux.

“Okay. Whoopie. Why would I want to use this?”

System administrators and everyday users can find several helpful advantages to using terminal multiplexing, such as managing multiple SSH connections from a single terminal window or opening command-line hogging GUI applications.

For this article, let’s look at a simple use that might benefit the everyday desktop Linux user: a cleaner desktop.

Suppose we have six terminals open. Each serves a different purpose.

Six terminals open plus a VeraCrypt GUI opened from a terminal.

In the image above from top left to bottom right:

  1. Remote SSH displaying ls man page
  2. Remote SSH of a Raspberry Pi running Raspian (neofetch)
  3. bmon of active network interface
  4. watch netstat to show active connections
  5. slurm of another network interface
  6. Opening VeraCrypt GUI from command line

Notice the desktop clutter with the open windows? Using tmux, we can reduce this to a single terminal window.

Before. Linux Mint 18.3 desktop with separate terminals.

After. Same Linux Mint 18.3 desktop with the same terminal windows, but multiplexed using tmux. Less desktop clutter.

 

tmux. A single terminal window contains the same terminals as the six separate terminal windows shown above. Shown here is the terminal running bmon.

Each open window is represented by a number starting at 0 along with the command running within that terminal. This is shown in the green status bar at the bottom. An asterisk denotes the current terminal (2:bmon* in this case).

Notice that tmux uses a full-window approach by default.

“How do I switch among windows?”

By default, press the hotkey combination, Ctrl+b, followed by the terminal number. In this example, there are six terminals numbered 0 to 5. To switch to the terminal running slurm, press Ctrl-b followed by the number 4 on the keyboard.

Switched to terminal running slurm.

The other terminals are still active just as if they had their own terminal windows. Only one terminal window is being shown at a time with tmux.

This is especially useful for hiding terminals that only serve to open other programs, but they remain unusable until the program quits. Take VeraCrypt, for example. Sometimes, it is more convenient to open VeraCrypt GUI from a command line, which might already be open. However, unless VeraCrypt is opened as a background process with the ampersand character (veracrypt &), the terminal waits for the VeraCrypt GUI to close before returning control to the user.

VeraCrypt GUI opened from a terminal as a foreground process. The command prompt in the terminal window must wait for VeraCrypt to close before returning control to the user.In this case, the VeraCrypt terminal is excess clutter. We can use tmux to “hide” the terminal. By opening a new terminal within tmux, we can open VeraCrypt from within its own terminal and then switch to any of the other existing terminals. The waiting terminal window sitting by itself on the desktop is avoided. (It still exists in tmux.)

If you have more than ten terminals, then the interactive menu is more convenient. Press Ctrl+b followed by w.

Interactive tmux window selector. Use the arrow keys and Enter to highlight the terminal you want to switch to. (Ctrl+b followed by w)

“How do I create a new window?”

When tmux opens, one terminal window is available by default. To add more, press Ctrl+b followed by pressing c on the keyboard. A new terminal opens. You can open as many as you need (within reason). Switch among them as already shown.

All open terminal windows will be listed in the green status bar at the bottom of the tmux window.

To close a window, press Ctrl+b followed by & (x will also work if the pane occupies the entire window).

“Can I use panes within a window?”

Yes. Press Ctrl+b followed by % (Shift+5). The result is something like this:

tmux split pane.

Panes are quite flexible, and there are a number of commands for manipulating them. In fact, tmux offers a wide selection of commands for fine-tuned control of terminal window management. Definitely read the man page (man tmux) for details. Take note that commands are case-sensitive.

Look forward to a slew of tmux commands!

Conclusion

tmux is a handy program in the Linux user’s toolbox. Sure, for one or two commands, tmux is overkill, but multiple terminal windows definitely benefit.

There are many features available, and the best way to experience them is to try tmux for yourself.

Have fun!

Advertisements

, , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: