Start using tmux

Amirhosein Zlf

tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen.

tmux may be detached from a screen and continue running in the background, then later reattached.

by tmux wiki
I have created a website which helps you to use tmux. Most of the content of this article is coming from that website.
Check it out !


Installation

You could take a look at tmux’s github repository and find a suitable way for installation, however, if you are using a common OS, it should be in its package manager.

Installing tmux on Ubuntu and Debian

$ sudo apt install tmux

Installing tmux on CentOS and Fedora

$ sudo yum install tmux

Installing tmux on macOS

$ brew install tmux

Your First Session

it’s super easy, open your terminal and run this

$ tmux

The status bar at the bottom is the most important part of it.

simple tmux
simple tmux

Working With tmux

The main trick for using tmux, is the prefix key for its all commands.

What is the prefix key?

the prefix is C-b by default.

Haa? what does that mean?

  • the C- means press and hold the Ctrl key
  • the b means press the b key

now that it makes sense, you need to hold the Ctrl key and then press the b button. this is just the prefix, after that you could press different shortcuts. here is a simple shortcut for creating a new window in current session.

C-b c

do you get that? press and hold the Ctrl key and then press b, after that release the Ctrl key and immediately press the c key. Now you should see two windows in your status bar like below.

tmux with 2 widnows
tmux with 2 widnows

Panes

Each window of tmux, which is each tab you could see in your status bar, could have multiple panes. You could split the current window in 2 different panes.

How it’s use full?

You could have a window which is following different log files. Check this out.

You could extend the panes and split each pane and create something like this.

Now let’s take a look at most useful panes shortcuts

Windows

Creating panes is not enough, we need different windows for different things. It’s up to you how to organize and separate your windows.

Sessions

How many windows you could create? it’s unlimited but it’s confusing. For instance I’m a system administrator who maintain 45 servers of company. We have 6 different applications and 10 database. It’s possible to have a 45 window in a session with different names, but honestly that would not work.

What’s the solution?

You could have create different sessions and each of them of have the related windows for that session. For example You could create a session for each application. Each session have different windows for that application like logs, databases, etc.

When you run tmux command it creates a new session. Run command below to see all the sessions you have in tmux.

$ tmux ls

But this is not the only advantage of sessions in tmux, the most important feature is that you could detach from a session and come back to it later. So for a system administrator for example which needs to have some windows always open, this is the best. She could create different sessions, and each time she login to that server, she needs to attach to her old tmux session and everything is still there.

Awesome?

Let’s take a look at sessions shortcuts.

Start a new session

$ tmux$ tmux new$ tmux new-session

if you are in a tmux session already, and needs to create a session here is the shortcut

C-b :new

We will talk about tmux command mode later.

Show all sessions

$ tmux ls$ tmux list-sessions

if you are in a tmux session already

C-b s

You could navigate to your session and press enter for going into that session or choose that session index which is in ( ) before the session name.

Detach from session

C-b d

Rename session

C-b $

Attach to last session

$ tmux a$ tmux at$ tmux attach$ tmux attach-session

Attach to a session with the name mysession

$ tmux a -t mysession$ tmux at -t mysession$ tmux attach -t mysession$ tmux attach-session -t mysession

Move to previous session

C-b (

Move to Next session

C-b )

Anything else for sessions?

Yup, it’s not finished !

There is another cool feature which lets different persons attach to a same session and share it.

How could it be useful?

For instance, If you need help from a person who is not around, she could login to same server and attach to your session. Now instead of chatting and doing copy paste, she could write commands and you could see it.

Scroll

Scrolling in tmux is tricky. There is a copy mode in tmux which you should get into first, and then you could scroll over your pane.
First you need the shortcut below for entering into copy mode.

C-b [

Then you could use arrow or Page-Up/Page-down keys to scroll over the pane.

Configuration

tmux has a configuration file which let’s you change so many things. It lets you change the shortcuts, colors, etc.
The default address for tmux configuration file is `~/.tmux.conf`
I will write another post for the tmux configuration file and how handy it is.

Amirhosein Zlf

Written by

A back-end developer who loves Real Madrid, Mercedes AMG Petronas F1 Team and Coldplay.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade