Posted on

If you are a computer science student or someone who enjoys working on a VPS, chances are you are using Terminal 90% of the time.

Unless you are working on UI using terminal, mastering some terminal commands bring about some benefits.

Maybe it’s just me. But ever since being comfortable with terminal commands, I realised that I had slowly shift away from UI.

Or maybe I get the feeling of being a l33t hacker getting simple operations done using the terminal.

While working on VPS, there was always a problem of wanting to run a script on it without maintaining a SSH connection.

In addition, terminals that are in full screen usually utilises only the half the screen and it is a waste of space.

In this tutorial, you will learn how to keep script running without maintain a connection as well as maximising your terminal to its fullest potential.

This tmux tutorial includes

  • Creating a session
  • Detaching a session
  • Creating a new window
  • Switching windows
  • Navigating between windows
  • Splitting panes
  • Navigating between panes

To install tmux type the following:

sudo apt-get install tmux

How Tmux Works

First, let’s first understand how tmux works.

When you execute a tmux command, a tmux server is launched.

Every activity that is launched in the tmux window happen within the server.

Commands or scripts ran within this server continues even after the SSH daemon has closed.

There are three terms while using tmux.

  1. Session
  2. Window
  3. Panes

A session is a single collection of pseudo terminals under the management of tmux.

Each session has one or more windows linked to it.

A window occupies the entire screen and may be split into rectangular panes, in which each pane is a separate pseudo terminal.

Quoted from: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux&sec=1

Tmux Usage

This is optional but you can first ssh into a VPS to experience the full potential of this tool.

I recommend RamNode(aff link) and it is the VPS provider I use when working on small projects.


Session

To create a new Session, issue the following command:

tmux new -s session1

To detach a session, type the following:

(Ctrl + b) + d

Note: Type “Ctrl+b” follow by the ”d” key separately

Now I will create another session call session2:

tmux new -s session2

And detach the session (Ctrl + b) + d to return to the main terminal.

You can list all the session by issuing the command:

tmux ls

To enter a session that was previously created, type:

tmux attach -t

In this tutorial, we will be attaching to session1. Therefore, the command will be:

tmux attach -t session1

Window

Now we will move on to the concept of Window.

Type (Ctrl + b) + c to create a new window.

To switch between the windows, enter the following:

(Ctrl + b) + n (Next window)

(Ctrl + b) + p (Previous window)

Note: The asterisk* is an indicator to the current window

Now I will switch to the first window using (Ctrl + b) + p and rename it using (Ctrl + b) + ,

If you have a lot of windows, you could type (Ctrl + b) + w to list all windows and select using the Enter key.

To delete the current window, type:

(Ctrl + b) + &


Pane

Now let’s move on the concept of Panes.

Panes are windows that are divided into multiple parts.

In a window, you can split the current into half horizontally or vertically by issuing:

(Ctrl + b) + “ (horizontally)

(Ctrl + b) + % (vertically)

I will now split the window horizontally using (Ctrl + b) + “ (horizontally).

Now I want to split the top half vertically.

However, the focus is on the 2nd pane.

Change the pane by typing (Ctrl + b) + o (Rotate clockwise) to change the selected pane.

Next split the first pane further vertically by issuing (Ctrl + b) + %


Concluding

There are plenty of use cases but one of the most important aspect is that this tool gives me the ability to continue running a script after closing my SSH connection.

A sysadmin could monitor different things at the same time without switching terminal all the time.

And since this can be done over ssh, it means the sysadmin could access these live data anytime.


Tmux Cheat Sheet

Session
Create new session tmux new -s sessionname
Detach current session (Ctrl + b) + d
List all sessions tmux ls
Window
Create new window (Ctrl + b) + c
Switch windows (Ctrl + b) + n (Next window)
(Ctrl + b) + p (Previous window)
List all windows (Ctrl + b) + w
Delete current window (Ctrl + b) + &
Pane
Split panes (Ctrl + b) + “ (horizontally)
(Ctrl + b) + % (vertically)
Switch panes (Ctrl + b) + o