How to use tmux

Surely you have heard of tmux. Today we will talk about how to use tmux, that is: what it is, what it is for and how to use it.

What is TMUX?

El comando tmux es un multiplexor de terminales, como también lo es el comando screen. En este Articulo vamos a comparar los dos ¿Es tmux realmente mejor, o es solo es un tema de preferencias?

But what is a terminal multiplexer?

Well, a terminal multiplexer is a system that allows us to have pseudo-terminals “inside a terminal”. That is, when we execute the command screen or tmux for example, we can exit the screen session but later we can re-enter and recover the session (and the program has continued its execution if it was running). This is useful to avoid being cut off from executing a command if we get disconnected for example. With tmux, we can also split the screen into several terminals, enlarge one, go back to splitting screens, etc.

Installation of tmux

The installation of tmux is really simple. in ubuntu for example:

sudo apt-get install tmux

On redhat-based systems:

yum install tmux

And on Arch Linux based systems:

sudo pacman -Sy tmux

Start a tmux session

To start a new session just type in the terminal tmux and press enter.

When we start a new tmux session, we will see in the status bar a status bar that shows the host name, date and time. On the left side is more information related to the session:

  • [0]: Indicates the name of the session, by default it is represented by numbers starting with zero.
  • 0: bash *: The 0 indicates the window number, in this case it is the first window of the session. You are running bash in this window, if you run another application the name of this one will replace bash. The asterisk represents that this is the window that has the focus. Each time you open a new session, window or program this data is modified.

One of the differences that the tmux command has with screen is precisely this status bar, with screen you do not have it.

Launch a session with a custom name

To modify the session name we must execute the tmux command as follows:

tmux nuevo -s NOMBRE

That is, if we want to name the session my-session1 it would be:

tmux nuevo -s my-session1

In the status bar it will show us: [my-session1] 0:bash*

Getting started with tmux

The tmux application has a lot of options, to access them you have to press the CTRL+b keys and after pressing these two keys together press only the key indicated in the command.

List active tmux sessions

To list active sessions with tmux you do exactly the same as with screen, using the tmux ls subcommand as we can see below:

ger@host:~$ tmux ls
1: 1 windows (created Thu Oct 27 07:50:46 2022)
ger@host:~$

The first number that appears is the name of the session, in the case of a session named “mysess” it would look like this:

ger@host:~$ tmux ls
mysess: 1 windows (created Thu Oct 27 07:50:46 2022)
ger@host:~$

Recuperar sesiones activas

After listing the active sessions we can retrieve them with tmux attach -t SESSION_NAME where SESSION_NAME is the session name as shown below:

ger@host:~$ tmux ls
mysess: 1 windows (created Thu Oct 27 07:20:46 2022)
ger@host:~$ tmux attach -t mysess

Exit the current session leaving it in the background.

If we are inside a session and we want to leave it in the background, we can execute CTRL+b and then d to leave the session running in the background:

This can be useful for leaving long running tasks in the background and recovering the session later, as we would do with screen.

Having scroll in tmux

If you have started to “play” with tmux you may think that it does not have scroll, if you need to scroll, you must press CTRL+b and then RE-PAG with this you can scroll without problem or scroll with the directional arrows up and down.

List of commands for tmux

A quick summary of what can be done would be as follows:

Session commands

ActionCommandDescription
Create new sessiontmux new -s SESSION_NAMECreate a new tmux session
Log in to tmux active sessiontmux attach -t SESSION_NAMERecover an active session
List active tmux sessionstmux lsList of tmux sessions
Leave session in backgroundCTRL+b then dLeaves a tmux session running in the background

Window commands

ActionCommand (to be executed after CTRL+b)Description
Create new windowcCreates a new window in the active session
Switch to next windownSwitches to the following window within the active session
Switch to previous windowpSwitches to the previous window of the active session.
Close windowxClose active window of current session (asks for confirmation)

Panel commands

ActionCommand (to be executed after CTRL+b)Description
Split the current window horizontallySplit the active window horizontally into 2 panes (2 terminals)
Split the current window vertically%Divide active window vertically into 2 panes (2 terminals)
Change active panelDirectional arrowChange active panel using the directional arrows to choose the direction of the panel to switch to
Briefly display panel numbersqEach panel has a number assigned to it, this command displays them for a short time.
Scroll through the panels in order until you have gone through them all.oScroll through the panels in order until you have gone through them all.
Swaps the position of the current panel with the next one.}Swap the position of the panels with the next one even if both have something running.
 Swaps the position of the current panel with the previous one.{It interchanges the position of the panels with the previous one even if both have something running.
Closes the current panel.xCloses the current panel (asks for confirmation)
Maximizes or resizes the current panel to normal sizezMaximizes or resizes the current pane, i.e., if the window is divided into panes, enlarges the current pane or redisplays all panes if you had previously enlarged a pane.

Leave a Reply