Multiplexing with tmux

by
, posted

Table of Contents

Intro

For a regular unix user a single terminal suffice for most of the jobs & if needed new tabs & windows can be fired up anytime.

But as the number of windows & tabs grow like for somebody working as a system adminstrator, the management of dozens of windows & tabs starts to take toll on the overall efficiency & the productivity of the system as a whole.

That is exactly where multiplexers come to rescue.

A terminal multiplexer is a software application that can be used to multiplex several separate pseudoterminal-based login sessions inside a single terminal display or to detach and reattach sessions from a terminal.

By “multiplexing” we mean the program takes input from single user & forwards that to the target pseudo-terminal or shell. With the help of a multiplexer, a user is able to manage several sessions inside one single terminal.

One of the most useful features of such programs is the fact that users can attach and detach sessions therby ensuring persistency of remote logins. Having that said, let’s explore one such application called “tmux” & see how useful a multiplexer can be.

Installation

Most platforms provide binary packages for tmux. Use the command below to install from binary.

# Debian, Ubuntu
$ sudo apt-get install tmux

# RHEL/CentOS/Fedora
$ sudo yum install tmux

# MacOS
$ brew install tmux

First tmux session

Now that you’ve completed the installation, type tmux to start the first session:

$ tmux

The green strip at the terminal bottom means that tmux session have started successfully.

Split pane horizontally

Panes in tmux compare to the tabs of a web brwoser. Now to split the panes horizontally type Ctrl + b then %. The combination Ctrl + b is known as prefix.

Switch between panes

To switch between the panes type prefix Ctrl + b then (left arrow) to switch from right to left pane.

Similarly, prefix + (right arrow), prefix + (down arrow), prefix + (up arrow) to switch from left to right pane, top to bottom pane, bottom to top pane respectively.

Split pane vertically

Now to split the panes vertically type Ctrl + b then " (double quote).

Delete a pane

To delete a pane type Ctrl + d

Manipulating Sessions

Start new session

To create or start a new session, type tmux new or tmux new -s session-name

Detach from the session

To close the current session, type prefix Ctrl + b then d or simply close your window.

List all sessions

Now to list your tmux sessions type tmux ls or inside an active session type prefix + s

Attach to last session

In order to attach to last session, type tmux a or tmux attach. If you want to attach to a session with specific name, type tmux a -t session-name

When we attach back to a certain session the state of that session is persistent & starts at exactly where we left off, like if we are logged into remote session it will be persistent.

Windows

Windows in tmux compare to the windows of a web browser. There can be multiple windows associated to a session.

Create a new window

To start a new session with the name mysession and window mywindow, type

tmux new -s mysession -n mywindow

If you are want to create a new window inside an existing session, type prefix + c

Close a window

To close current windows type Ctrl + b then &

Switch between windows

Switch to previous window: Ctrl + b then p

Switch to next window: Ctrl + b then n

List all windows

To list all windows type Ctrl + b then w

Summary

I hope you got some idea on how useful multiplexers are. Be it for CLI enthusiasts or system adminstrators terminal multiplexers ease the multitaksting, improves efficiency & saves a lot of time by featuring state persistency.

Nonetheless this post is not exhaustive & doesn’t cover everything there is to know about tmux or multiplexers in general but provides a good kick start to get going with multiplexing.

This guide is heavily inspired from this cheatsheet so you might want to bookmark it. That is all for today, see you next time :P

Reply via mail

Your Signature