# Tmux

## Motivation

At cases, we need to run a long session via SSH for heavy tasks, but the connection is not always stable and might be disconnected. If so, we are going to lose the access to these remote sessions. `tmux` can help you reconnect to a disconnected remote terminal (if you run these tasks under `tmux` from the very beginning).&#x20;

## Installation

```shell
sudo apt install tmux
sudo apt install tmuxp  # a python session manager for tmux
```

## Usage

### tmux

Outside of a session

```shell
tmux     # enable tmux in remote terminal before running time-consuming tasks
tmux a   # attach to a previous disconnected terminal launched in tmux
tmux ls  # list all terminals launched by tmux
tmux kill-session -t target-session  # kill a running session
tmux kill-session -a  # kill all sessions
tmux kill-server      # kill all sessions
```

Inside of a session

```shell
Ctrl+B, and type
  1, 2, or other numbers  # switch to window
  arrow keys  # navigate panes
  n  # go to next window
  w  # go to window mode
  t  # add tag
  x  # kill tagged window
  d  # detach current session
  :kill-session  # kill current session
  [  # enter scroll mode, press q to quit
```

To set up mouse scrolling, add the following line to `~/.tmux.conf` :

```shell
set -g mouse on   # For tmux version 2.1 and up
```

Note that this setting breaks highlighting and copying text. Use shift+click to maintain this functionality.

### tmuxp

```shell
tmuxp load config.yaml
```

* Example: <https://github.com/hanzheteng/LOCUS/blob/main/tmuxp_config/run_locus.yaml>

## Reference

* `tmux` source code: <https://github.com/tmux/tmux>
* `tmuxp` source code: <https://github.com/tmux-python/tmuxp>
* Tutorials: <https://phoenixnap.com/kb/tmux-tutorial-install-commands>
* Tmux Cheat Sheet: <https://tmuxcheatsheet.com/>&#x20;
* [How do I scroll in tmux?](https://superuser.com/a/510310/1279296)
