For system, network and cloud administrators

How to install and use tmux

How to install and use tmux

tmux is a very useful tool when working with the terminal. It basically allows you to start multiple sessions and each one of these sessions can have multiple panes inside it. tmux is a terminal tool focused on window and session management. tmux is available for: Debian/Ubuntu, Fedora, RHEL/CentOS, Arch Linux, OpenSUSE, macOS.

Install tmux

Install tmux on Debian/Ubuntu derrivatives:
sudo apt-get install tmux
Install tmux on Ubuntu 20.04.1 desktop
Install tmux on CentOS
sudo yum install tmux
Install tmux on CentOS 8.2 server
Install tmux on Fedora
dnf install tmux
Install tmux on OpenSUSE
zypper install tmux
Install tmux on Arch Linux
pacman -S tmux
Install tmux on macOS
brew install tmux

Run tmux

After tmux has been installed, you can create your own first tmux session by typing in your terminal:

tmux
First tmux session on Ubuntu 20.04.1 desktop

You see … when you start a tmux session, tmux will start one window inside a single panel. And the [0] 0:bash * from the bottom-left corner will let you know how many current opened windows there are.

Creating and manging panes in tmux

Once you ran tmux, you’ll need to know a few keyboard shortcuts and tmux uses a pretty simple shortcut keys concept. For example:

  • to create a new pane and have it displayed horizontally next to the default one (the one initially created when you first ran tmux), you’ll need to press both Ctrl+b keys, release and then press the % key – this will basically split your panes horizontally, from left to right;
Creating and manging panes in tmux – Ubuntu 20.04.1 desktop
  • to create a new pane and have it displayed vertically, you’ll need to press both Ctrl+b keys, release and then press the key – this will split your tmux panes vertically;
Creating and manging panes in tmux – Ubuntu 20.04.1 desktop
  • to navigate/shift between panes you’ll need to press both Ctrl+b keys, release and then press the left/right/up/down arrow key – it ill move you from one pane to another, left/right, up/down;
  • to navigate/shift to the next pane, you’ll need to press both Ctrl+b keys, release and then press the o key – it’ll allow you to just move quickly through panes without needing to know their position;
Creating and manging panes in tmux – Ubuntu 20.04.1 desktop

Creating and managing windows in tmux

  • to create a new window in tmux, you’ll need to press both Ctrl+b keys, release and then press the c key – notice how the [0] 0:bash * (from the left-bottom corner) will increment from 0 to 1, as we have opened a 2nd window in tmux; here, inside this 2nd window, we can create other panes too;
Creating and managing windows in tmux – Ubuntu 20.04.1 desktop
Creating and managing windows in tmux – Ubuntu 20.04.1 desktop
  • to switch between windows, you have 2 options:
  1. switch to the previous tmux window by pressing both Ctrl+b keys, release and then press the p key; switch to the next tmux window by pressing both Ctrl+b keys, release and then press the n key;
  2. switch between tmux windows by pressing both Ctrl+b keys, release and then press theIDnumberOfTheSessionWindow (the ID is shown on the left-bottom corner of your terminal and has an asterisk).
Creating and manging panes in tmux – Ubuntu 20.04.1 desktop

Creating and managing sessions in tmux

With tmux, sessions can be attached and detached while still running your early processes in the background and:

  • to detach your current tmux session, you’ll need to press both Ctrl+b keys, release and then press the d key;
  • if you want tmux to give you a choice of which sessions you want to detach, you’ll need to press both Ctrl+b keys, release and then press the D key.

Remember, detaching tmux sessions will not mean that you have closed those sessions. You’ve just detached them for better session management but whatever activities you were doing in those sessions, they’re still there. And:

  • in order to reconnect to those detached sessions, you’ll need to first view a list of all running tmux sessions by typing in your terminal:
tmux ls
Creating and managing sessions in tmux – Ubuntu 20.04.1 desktop
  • to connect to a specific tmux session, you’ll need to choose the proper ID related to your needed session by typing in your terminal:
tmux attach -t 0

With the above command, I’ve reconnected to the session ID 0 which has 2 windows launched:

Creating and managing sessions in tmux – Ubuntu 20.04.1 desktop
  • to create a new tmux session with your own customized name, you’ll need to start it by typing in your terminal:
tmux new -s newDesiredSessionName
  • to rename your current session, you’ll need to type in your terminal:
tmux rename-session -t theIDofYourSessionWindow newDesiredSessionName

Leave a Reply

Your email address will not be published. Required fields are marked *