Tmux (terminal multiplexer)

Ctrl+B **+** ...

* O: Move through panes in order. Each press takes you to the next, until you loop through all of them.
* }: Swap the position of the current pane with the next.
* {: Swap the position of the current pane with the previous.
* X: Close the current pane

The Linux `tmux` command is a terminal multiplexer, like `screen`. It allows you to have multiple windows within a single terminal window, and to jump back and forth between them. A window can be divided into panes, each of which gives you an independent command line.

You can also detach a session and it becomes a headless entity running in the background—you can even close the terminal window that launched it. When you’re ready, you can open a new terminal window and reattach the still-running session. You can also do this over an SSH connection.

You can detach a session on one computer, go home, and log in to the remote computer. When reconnected, you can reattach the background session and use it interactively again.

## How to install

```
sudo apt-get install tmux

```

## How to use tmux?

### Sessions

#### Starting a tmux session

```
tmux

```
The terminal window will show a status bar below when indicating a `tmux` session.

The right side of the status bar shows the hostname, and the time and date. The left side shows the following session-related information:

* **[0]:** This is the session name. By default, they’re numbered, starting with zero. We cover how you can give meaningful names to sessions below.
* **0:bash\*:** The 0 indicates this is the first window in this session. The only process running in this session is `bash`. If you run a program, its name will appear here. The asterisk (\*) means this is the window you’re looking at. Each time you create a new window in a `tmux` session, its window number and the name of the program running in it are added to the status bar.

##### Starting a Named tmux Session

To start `**tmux**` with a session name, use the `new` (new session) command, and the `**-s**` (session name) option. Our session is going to be called `“custom-session”` so we type the following:

```
tmux new -s custom-session

```

#### Detaching & Attaching Sessions

##### Detaching Session

If you press **Ctrl+B**, and then **D**, you will detach the session. It will continue to run in the background, but you won’t be able to see or interact with it.

Any long-running or continual processes you launched before detaching the session will still be running in the background (unless they’ve finished) when you attach the session.

##### Attaching Session

To attach a detached session, we’ll use the self-explanatory `attach-session` command with the `-t` (target session) option. We’ll also provide the name of the session we wish to recall.

```
tmux attach-session -t [session_name]

```

#### Handling multiple sessions

start a session detach it and start another one.

Press **Ctrl+B**, and then **S** to see a list of `tmux` sessions.

Move up and down by pressing the Up and Down Arrows. The bottom section displays a preview of the content in the highlighted session.

If you press the Right Arrow, the windows for the highlighted session are displayed.

Press Enter to move to the highlighted session or window or Esc to leave the session list without changing sessions. If you select a new session, your current one detaches, and the one you selected is attached.

### Windows

Show windows in current session

```
tmux ls

```

#### Adding Windows

To create a new window in the current session, press **Ctrl+B**, and then **C**. You’ll get a blank terminal window in the current session.

#### Hop between Windows

To hop between windows, press **Ctrl+B**, and then one of the followings keys:

* **N:** Display the next window.
* **P:** Display the previous window.
* **0 to 9:** Display a window numbered 0 to 9.

You can also choose a window from a list. If you press **Ctrl+B**, and then **W**, a list of windows appears. Hop up and down with *arrow keys*. Confirm selection with **"enter"**

*"top-"* & *"dmesg\*"* are the programs being used.

#### Close Window

To close the window, press **Ctrl+B**, and then quickly hit **X**. The status bar turns amber. You’re then prompted to confirm you want to kill the window.

Press **Y** to close the window or **N** if you change your mind. You don’t have to press Enter afterward; **Y** or **N** is enough to register your choice.

If close (**0**) which is the session itself, you will close all windows and end the session

#### Multiplex Windows / Window Panes

##### Horizontal Split

If you press **`Ctrl+B`**, and then double quotation marks (**`“`**), you split the window horizontally into two panes.

This only affects the current window; the others in the session won’t be changed.

**NOTE:** These are two independent command lines, not two views in one window; they are distinct and separate shells.

##### Vertical Split

If you press **`Ctrl+B`**, and then the percentage sign (**`%`**) it splits the current pane vertically.

##### Moving from Pane to Pane

To move from one pane to another, press **Ctrl+B,** and then either the **Up**, **Down**, **Left**, or **Right** *Arrow*.

###### Highlight Panes

Press **Ctrl+B**, and then **Q** to make `**tmux**` briefly flash the number of each pane.

These numbers are used in prompts and messages from `tmux`.

##### Expand / Reduce Pane

**[hold] `ctrl + B`** move *arrow* **up / down / left / right**

To increase / reduce hold and keep holding **`ctrl`** to then press "**`B`**" and move *arrow* **up / down / left / right**.

##### Close Pane

Press **Ctrl+B**, and then **X** to close the current pane..