# Référence rapide tmux

*Sessions, fenêtres, volets, mode copie, configuration*

> Source: tmux Documentation (github.com/tmux/tmux) · MIT

## Gestion des sessions

### Créer et rattacher

```
tmux                        # new session
tmux new -s work            # new named session
tmux attach -t work         # attach to session
tmux attach -dt work        # detach others, attach
```

### Commandes de session

| Command | Description |
|---------|-------------|
| `tmux ls` | Lister toutes les sessions |
| `tmux kill-session -t work` | Supprimer une session nommée |
| `tmux kill-server` | Arrêter le serveur tmux et toutes les sessions |
| `tmux rename-session -t old new` | Renommer une session |
| `Prefix d` | Se détacher de la session courante |
| `Prefix $` | Renommer la session courante |
| `Prefix s` | Lister les sessions (interactif) |

*Le préfixe par défaut est Ctrl-b*

## Fenêtres

### Raccourcis clavier des fenêtres

| Command | Description |
|---------|-------------|
| `Prefix c` | Créer une nouvelle fenêtre |
| `Prefix ,` | Renommer la fenêtre courante |
| `Prefix &` | Fermer la fenêtre courante (confirmation) |
| `Prefix n` | Fenêtre suivante |
| `Prefix p` | Fenêtre précédente |
| `Prefix 0-9` | Passer à la fenêtre par numéro |
| `Prefix w` | Lister les fenêtres (sélecteur interactif) |
| `Prefix l` | Basculer vers la dernière fenêtre active |

### Commandes de fenêtres

```
tmux new-window -t work     # new window in session
tmux swap-window -t 0       # move window to index 0
tmux move-window -t 3       # renumber to index 3
```

## Volets

### Diviser et fermer

| Command | Description |
|---------|-------------|
| `Prefix %` | Diviser horizontalement (gauche \| droite) |
| `Prefix "` | Diviser verticalement (haut / bas) |
| `Prefix x` | Fermer le volet courant (confirmation) |
| `Prefix !` | Détacher le volet dans sa propre fenêtre |
| `Prefix z` | Basculer le zoom du volet (fenêtre entière) |

### Redimensionner les volets

| Command | Description |
|---------|-------------|
| `Prefix Ctrl-Arrow` | Redimensionner d'une cellule |
| `Prefix Alt-Arrow` | Redimensionner de cinq cellules |
| `Prefix Space` | Faire défiler les dispositions |

*Dispositions : even-horizontal, even-vertical, main-horizontal, main-vertical, tiled*

## Navigation

### Se déplacer entre les volets

| Command | Description |
|---------|-------------|
| `Prefix Arrow` | Aller au volet dans la direction |
| `Prefix o` | Passer au volet suivant |
| `Prefix q` | Afficher les numéros de volets, puis appuyer sur le numéro |
| `Prefix {` | Échanger le volet avec le précédent |
| `Prefix }` | Échanger le volet avec le suivant |

### Se déplacer entre les sessions

| Command | Description |
|---------|-------------|
| `Prefix (` | Passer à la session précédente |
| `Prefix )` | Passer à la session suivante |
| `Prefix s` | Sélecteur de session (vue arborescente) |
| `Prefix w` | Sélecteur de fenêtres toutes sessions confondues |

## Mode copie

### Entrer et naviguer

| Command | Description |
|---------|-------------|
| `Prefix [` | Entrer en mode copie |
| `q` | Quitter le mode copie |
| `Arrow / hjkl` | Déplacer le curseur (mode vi) |
| `Ctrl-u / Ctrl-d` | Page précédente / suivante |
| `g / G` | Aller au début / à la fin |
| `/` | Rechercher en avant |
| `?` | Rechercher en arrière |
| `n / N` | Correspondance suivante / précédente |

### Sélectionner et copier

| Command | Description |
|---------|-------------|
| `Space` | Démarrer la sélection (mode vi) |
| `Enter` | Copier la sélection et quitter |
| `Prefix ]` | Coller le tampon |
| `v` | Basculer la sélection rectangulaire (mode vi) |

*Activer le mode vi : set -g mode-keys vi*

## Configuration

### Paramètres courants (~/.tmux.conf)

```
set -g mouse on                 # enable mouse
set -g base-index 1             # windows start at 1
set -g renumber-windows on      # renumber on close
set -g default-terminal "tmux-256color"
set -g history-limit 50000
```

### Recharger la configuration

```
tmux source-file ~/.tmux.conf   # from shell
# or Prefix : then type:
source-file ~/.tmux.conf
```

## Barre de statut

### Options de la barre de statut

```
set -g status-position top
set -g status-style "bg=black,fg=white"
set -g status-left "[#S] "
set -g status-right "%H:%M %d-%b"
set -g status-interval 5
```

### Variables de format

| Command | Description |
|---------|-------------|
| `#S` | Nom de la session |
| `#I` | Index de la fenêtre |
| `#W` | Nom de la fenêtre |
| `#P` | Index du volet |
| `#H` | Nom d'hôte |
| `#T` | Titre du volet |

## Raccourcis clavier

### Liaisons personnalisées

```
# change prefix to Ctrl-a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# vim-style pane navigation
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
```

### Syntaxe de liaison

| Command | Description |
|---------|-------------|
| `bind X command` | Lier Prefix + X à la commande |
| `bind -n M-Left prev` | Lier sans préfixe (Alt-Gauche) |
| `bind -r H resize-pane -L 5` | Liaison répétable |
| `unbind X` | Supprimer une liaison |
| `Prefix ?` | Lister toutes les liaisons clavier actuelles |

## Scripts

### Dispositions scriptées

```
tmux new-session -d -s dev
tmux send-keys -t dev "vim" Enter
tmux split-window -h -t dev
tmux send-keys -t dev "npm run dev" Enter
tmux attach -t dev
```

### Commandes utiles

| Command | Description |
|---------|-------------|
| `tmux send-keys -t sess 'cmd' Enter` | Envoyer des frappes à une session |
| `tmux capture-pane -p` | Afficher le contenu du volet sur stdout |
| `tmux display-message '#S'` | Afficher un message / variable |
| `tmux set-environment -g VAR val` | Définir une variable d'environnement |
| `tmux list-keys` | Lister toutes les liaisons clavier |

## Modèles courants

### ~/.tmux.conf recommandé

```
set -g mouse on
set -g base-index 1
set -g mode-keys vi
set -g status-position top
set -g history-limit 50000
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
```

### Recettes rapides

| Command | Description |
|---------|-------------|
| `Survive SSH disconnect` | `tmux new -s work` → déconnecter → `tmux attach -t work` |
| `Share session` | Les deux utilisateurs `tmux attach -t same` (lecture seule : `-r`) |
| `Save scrollback` | `tmux capture-pane -S -3000 -p > log.txt` |
| `Kill stuck pane` | Prefix x, ou `tmux kill-pane -t %N` |
| `Reorder windows` | `:swap-window -s 3 -t 1` |
