Guide to TMUX - Terminal That Remembers Everything
Tags: tmux, linux, opensource, bash
Published: 2023-01-05 21:49:34


## What’s tmux?

**TMUX** is a so-called terminal multiplexer, which allows you to run multiple terminals within one window.

I.e. we can open several terminals in one window and work in them at once, see what happens within one window or several terminals. 

In tmux you can create sessions and when we disconnect from them, and after some time reconnect again - _the session continues to live_ and everything will be exactly as we left this session.

![tmux new session](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kdhhiohu158pb5nja37.jpg)

So there will be the same open windows, all the history, all the messages and so on. This is very cool because, for example, when we connect to the server via ssh and we need to run some long command. Then we run it and if the connection is broken or just disconnected, we resume the running tmux session and we see exactly what was running there. 

The utility can be used on linux, unix, mac os servers.

## Installing tmux

The installation of `tmux` is quite simple in most operating systems, just run a single command: `sudo apt-get install tmux` (Ubuntu/Debian) or brew install tmux (Mac OS), `sudo yum install tmux` (CentOS)

## Starting Your First Session

A new [session](https://man.openbsd.org/tmux#new-session) is created with the `new-session` command or simply `new`. To set a name for the session, use the `-s` flag. 

After starting the first session, the <u>tmux server starts</u> and the first client becomes the new session.

The new session will have one window and one pane with index 0.

![tmux status bar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ub5nc4ahxks1xof93xq4.jpg)

The basic tmux commands start with the combination `C-b`, which means `Ctrl` key and `b`, followed by the command name. 

For example `<C-b 0>`, having 2 active windows, switch to the first one (0) by pressing `Ctrl+B+0`.

## Create new windows

Tmux has a wide variety of commands and acronyms. The command `new-window` `neww` is used to create a new window. The command <C-b c> does the same and creates a new window.

![tmux - new window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1co4ml4ne5n2uqh4dq26.jpg)

## Splitting Panes

So, we have created our first session. By default, tmux creates one window and one pane inside. Each pane can be divided horizontally and vertically into **n** panes.

- To split the pane into two vertically, you have to do `<C-b %>` (CTRL+B+%);
- To split the pane into two horizontally, you must execute `<C-b ">` (CTRL+B+");
- `tmux split-window` - is an alternative to the commands above, takes arguments to split horizontally `-h` and vertically `-v`.

![tmux - Splitting Panes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x03lkc9db2837uq09r2.jpg)

Thus, we get two windows that are absolutely full and all this works absolutely independently and lives its own life.

## Navigating Panes

To move the cursor between panes use the command `<C-b + arrow key>`.

To delete a pane use the command `<C-b x>`. After confirmation the selected pane will be deleted. 

## Closing panes
There are several ways to close a panes. By typing exit or by executing the command `<C-b d>`.

> Note: The session will still live.

## Session handling

When you are done with a session, you can either get rid of it or save the session in the background for later use.

To reconnect to the session and continue, you need to determine which session you want to connect to first. List the current session using `tmux ls`.

![tmux list of session](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97mpfob5c3zzfgu302ss.jpg)

To resume the session: `tmux attach -t <session_name>`. And we see that everything here is left exactly as we left it. This is very cool, very handy and really helps out in many cases.
  
## Configuration files

After starting the server, tmux executes the instructions from the `.tmux.conf` file in the user's home directory. If the file does not exist, create one.

> New commands will only work for new sessions. For old sessions, old instructions will be applied.

Let's tweak tmux a bit, so that we can change the size of each panel with the mouse, both for vertical and horizontal panels. Open the `.tmux.conf` file and insert the command.

```json
# Enable Mouse Support
set -g mouse on
```
Here's a good source for more in-depth tuning [.tmux.conf](https://github.com/gpakosz/.tmux)


##Conclusion

Great tool tmux, I advise everyone to put it and have a look at it for a while. The tool is really very handy and very useful and helps in many cases.

Particularly when you need to run multiple servers, it's convenient to run it in a single window to keep track of collateral, what's happening and what's down and so on. It is convenient to be able to run some long procedures on the server and then return to these procedures to see all the saved history, all logs and messages that fell into the terminal.