# TMUX/Screen Cheatsheet

TMUX and screen are extremely useful tools for maintaining sessions through reverse tunnels and other fragile connections. They allow for reconnection of sessions that become detached (disconnected) where the process is still running but there was some sort of network interruption.

They both have a very handy function where each window can be split either vertically or horizontally in to separate panes. I am not sure what the actual limit to the number of panes may be, but I regularly split my windows into four panes comfortably.

Screen is fairly ubiquitous in most Linux distributions, however TMUX is a program that will likely have to be installed on the machine you are connecting to. Screen is fairly old and does not get feature updates anymore (as far as I am aware), while TMUX is newer and has many plugins which can be used to extend its functionality. Learning both will ensure you are ready no matter which may be present on the system you log into.

{% hint style="info" %}
**`^`** is shorthand for the **`CTRL`** key, so **`^b`** == **`CTRL + b`**.

\-----

**`^a`** is the default meta-key for **`screen`**, while **`^b`** is the default meta-key for **`TMUX`**.
{% endhint %}

## Session Management

| Action                                                        | TMUX                                                                                     | Screen                                                                    |
| ------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| Start a new session                                           | <p><code>tmux</code></p><p><code>tmux new</code></p><p><code>tmux new-session</code></p> | `screen`                                                                  |
| Create a new named session                                    | `tmux new -s <name>`                                                                     |                                                                           |
| Re-attach a (local) detached session                          | <p><code>tmux attach</code></p><p><code>tmux attach-session</code></p>                   | `screen -r`                                                               |
| Re-attach an attached session (detaching it from elsewhere)   | <p><code>tmux attach -d</code></p><p><code>tmux attach-session -d</code></p>             | `screen -dr`                                                              |
| Re-attach an attached session (keeping it attached elsewhere) | <p><code>tmux attach</code></p><p><code>tmux attach-session</code></p>                   | `screen -x`                                                               |
| Detach from currently attached session                        | <p><code>^b d</code></p><p><code>^b :detach</code></p>                                   | <p><code>^a d</code></p><p><code>^a ^d</code> <code>^a :detach</code></p> |
| Detach and log out                                            |                                                                                          | <p><code>^a f</code></p><p><code>^a ^f</code></p>                         |
| List sessions                                                 | <p><code>^b s</code></p><p><code>tmux ls</code> <code>tmux list-sessions</code></p>      | `screen -ls`                                                              |

## Pane Management

| Action                                 | TMUX    | Screen   |
| -------------------------------------- | ------- | -------- |
| Split pane horizontally                | `^b "`  | `^a S`   |
| Split pane vertically                  | `^b %`  | `^a \|`  |
| Switch to another pane                 | `^b o`  | `^a tab` |
| Kill the current pane                  | `^b x`  | `^a X`   |
| Close all panes except the current one | `^b !`  | `^a Q`   |
| Swap location of panes                 | `^b ^o` | N/A      |
| Show time                              | `^b t`  | -----    |
| Show numeric identifier for all panes  | `^b q`  | -----    |

## Window Management

| Action                             | TMUX                                                                                    | Screen                                            |
| ---------------------------------- | --------------------------------------------------------------------------------------- | ------------------------------------------------- |
| Rename window                      | <p><code>^b , \<new\_name></code></p><p><code>^b :rename-window \<new\_name></code></p> | `^a A <new_name>`                                 |
| Create new window                  | `^b c`                                                                                  | <p><code>^a c</code></p><p><code>^a ^c</code></p> |
| List windows                       | `^b w`                                                                                  | `^a w`                                            |
| List windows (with selection menu) | -----                                                                                   | `^a "`                                            |
| Go to window $num                  | `^b $num`                                                                               | `^a $num`                                         |
| Go to previously active window     | `^b l`                                                                                  | `^a ^a`                                           |
| Go to next window                  | `^b n`                                                                                  | `^a n`                                            |
| Go to previous window              | `^b p`                                                                                  | `^a p`                                            |

## MISC

| Action                       | TMUX                                                                                                                                                        | Screen                                                                 |
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| See key bindings             | `^b ?`                                                                                                                                                      | `^a ?`                                                                 |
| Enter "scroll mode"          | `^b [`                                                                                                                                                      | `^a [`                                                                 |
| Enter "Vim mode"             | <p><code>^b ]</code></p><p>Search with <code>?</code>.</p><p>Hit <code>\[spacebar]</code> to start copying text</p><p>Use <code>^b + ]</code> to paste.</p> |                                                                        |
| Scroll up in "scroll mode"   | <p><code>\[page up]</code></p><p><code>\[up arrow]</code></p>                                                                                               | <p><code>^b</code> for page up</p><p><code>k</code> for one line</p>   |
| Scroll down in "scroll mode" | <p><code>\[page down]</code></p><p><code>\[down arrow]</code></p>                                                                                           | <p><code>^f</code> for page down</p><p><code>j</code> for one line</p> |
| Exit "scroll mode"           | `q`                                                                                                                                                         | `ESC`                                                                  |
| Exit current shell           | `^d`                                                                                                                                                        | `^d`                                                                   |

### Ippsec Tmux Notes

Config from [ippsec](https://www.youtube.com/watch?v=Lqehvpe_djs).

```
#set prefix
set -g prefix C-a
bind C-a send-prefix
unbind C-b

set -g history-limit 100000
set -g allow-rename off

bind-key j command-prompt -p "Join pane from:" "join-pane -s '%%'"
bind-key s command-prompt -p "Send pane to:" "joian-pane -t '%%'"

set-window-option -g mode-keys vi

run-shell /opt/tmux-logging/logging.tmux
```

First press the prefix `ctrl + b`(default, Ippsec changes it to Ctrl+a) then release the buttons and press the combination you want.

Join panes: `prefix + s #`

Zoom in/out to panes: `prefix + z`

Kill session by tag:`tmux kill-session -t X`

Kill pane: `prefix + &`

#### tmux plugins:

* tmux logging plugin (get this!!) can save log of tmux windows
* [better mouse mode](https://github.com/NHDaly/tmux-better-mouse-mode)

If you like this content and would like to see more, please consider [buying me a coffee](https://www.buymeacoffee.com/zweilosec)!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zweilosec.gitbook.io/hackers-rest/linux-1/linux-hardening/tmux-screen-cheatsheet.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
