Many data scientists get stuck with the manifold of tools available to them. In this article, I will present the Tmux tool. What it does best is turn a single terminal into a multi-pane tool, where you can take care of more than one task at a time.


Table of Contents

  • Introduction
  • Installation
  • Terminology
  • Start Session
  • Split Panes
  • Navigate through the Panes
  • Close/Open Tmux Session
  • List and Kill Sessions
  • Most important commands inside a Tmux Session
  • Useful Resources
  • Conclusion
  • References‌

Introduction

Tmux allows you to create multiple tabs and panes within a single terminal window. It can also keep programs running after you disconnect (e.g. when you ssh into an AWS instance).Tmux lets you run programs on your server, close your terminal,come back later and your program will still be running in the exact same way.


Installation

The best way to install Tmux on Mac is via HomeBrew by running the following command in the terminal:

 brew install tmux

or for Ubuntu and derivatives:

 sudo apt-get install tmux

After downloaded, check if it has been properly installed by running the following command which will print out its version:

 tmux -V

Terminology

First, you need to know a few terms before running Tmux.

  • Magic Command: This is the key combination that must be entered before entering any shortcut key. The default magic command for tmux is ctrl + b.
  • Session - Saved collection of windows and panes and programs you left running inside
  • Attaching - Enter a running session
  • Detaching - Exit a running session (without killing it)
  • Window - Similar to a tab in your browser. Only one window is visible at a time.
  • Pane - Belongs to a window. You can have many of these visible at once.

Start Session

Start a session by simply running the following command on a terminal:

 tmux

it looks like nothing happened, except from a little green bar at the bottom.

OR

Start a new with session name:

tmux new -s myname

Split Panes

Once inside the tmux session, you can split the window vertically:

 ctrl + b and "

or horizontally (right/left)

 ctrl + b and %

My favorite splitted type is:

  • First split vertically
  • Then split horizontally

As displayed in the image above I use the down right window to run my Jupyter notebook and the other to run commands.


You can navigate through the panes by running:

 ctrl + b and the arrow key
  • ctrl+b+↓: move to the down window
  • ctrl+b+→: move to the right window
  • ...etc

Close/Open Tmux Session

You can close Tmux (detach) by simply running:

 ctrl + b and d

Tmux will continue running in the background. This is really useful as any notebooks, the kernels I had running, they are all still sitting there. You can return back to an existing session by typing the following command on the terminal:

tmux a -t myname          #by name
tmux a 4                  #by number (in this case 4)
tmux a  #  (or at, or attach)

If you want to exit Tmux and force it to stop running this session instead of using ctrl+b and d use instead:

 ctrl + b and &

List and Kill Sessions

You can get a list of all sessions by running:

 tmux ls

Kill a specific session:

tmux kill-session -t myname

Kill all sessions:

tmux kill-server

Usually those commands are running outside of a Tmux session (on your normal terminal without the little green bar at the bottom).


Most important commands inside a Tmux Session

Start each command with CTRL + b, release, then press one of the following inside a Tmux session:

 %                          vertical split
 "                          horizontal split
 d                          detach from session  (it keeps running in the background)
 x                          kill pane
 Up/Down/Left/Right         move between panes
 : + resize-pane -D         Resizes the current pane down
 : + resize-pane -U         Resizes the current pane upward
 : + resize-pane -L         Resizes the current pane left
 : + resize-pane -R         Resizes the current pane right
 : + resize-pane -D 20      Resizes the current pane down by 20 cells

Useful Resources

Some useful resources can be found below:


Conclusion

This brings us to the end of this article. Hope you got a basic understanding of how you can use Tmux.

If you liked this article, please consider subscribing to my blog. That way I get to know that my work is valuable to you and also notify you for future articles.‌
‌Thanks for reading and I am looking forward to hear your questions :)‌
Stay tuned and Happy Machine Learning.


‌References