# Referência Rápida do tmux

*Sessões, janelas, painéis, modo de cópia, configuração*

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

## Gerenciamento de Sessão

### Criar e Anexar

```
tmux                        # nova sessão
tmux new -s work            # nova sessão com nome
tmux attach -t work         # anexar à sessão
tmux attach -dt work        # desanexar outros, anexar
```

### Comandos de Sessão

| Command | Description |
|---------|-------------|
| `tmux ls` | Listar todas as sessões |
| `tmux kill-session -t work` | Encerrar uma sessão pelo nome |
| `tmux kill-server` | Encerrar o servidor tmux e todas as sessões |
| `tmux rename-session -t old new` | Renomear uma sessão |
| `Prefix d` | Desanexar da sessão atual |
| `Prefix $` | Renomear sessão atual |
| `Prefix s` | Listar sessões (interativo) |

*O prefixo padrão é Ctrl-b*

## Janelas

### Atalhos de Janela

| Command | Description |
|---------|-------------|
| `Prefix c` | Criar nova janela |
| `Prefix ,` | Renomear janela atual |
| `Prefix &` | Fechar janela atual (confirmar) |
| `Prefix n` | Próxima janela |
| `Prefix p` | Janela anterior |
| `Prefix 0-9` | Mudar para janela por número |
| `Prefix w` | Listar janelas (seletor interativo) |
| `Prefix l` | Alternar para última janela ativa |

### Comandos de Janela

```
tmux new-window -t work     # nova janela na sessão
tmux swap-window -t 0       # mover janela para índice 0
tmux move-window -t 3       # renumerar para índice 3
```

## Painéis

### Dividir e Fechar

| Command | Description |
|---------|-------------|
| `Prefix %` | Dividir horizontalmente (esquerda \| direita) |
| `Prefix "` | Dividir verticalmente (cima / baixo) |
| `Prefix x` | Fechar painel atual (confirmar) |
| `Prefix !` | Separar painel em sua própria janela |
| `Prefix z` | Alternar zoom do painel (janela completa) |

### Redimensionar Painéis

| Command | Description |
|---------|-------------|
| `Prefix Ctrl-Arrow` | Redimensionar 1 célula |
| `Prefix Alt-Arrow` | Redimensionar 5 células |
| `Prefix Space` | Ciclar pelos layouts |

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

## Navegação

### Mover Entre Painéis

| Command | Description |
|---------|-------------|
| `Prefix Arrow` | Mover para o painel na direção |
| `Prefix o` | Ciclar para o próximo painel |
| `Prefix q` | Mostrar números dos painéis, pressionar número para ir |
| `Prefix {` | Trocar painel com o anterior |
| `Prefix }` | Trocar painel com o próximo |

### Mover Entre Sessões

| Command | Description |
|---------|-------------|
| `Prefix (` | Mudar para sessão anterior |
| `Prefix )` | Mudar para próxima sessão |
| `Prefix s` | Seletor de sessão (visualização em árvore) |
| `Prefix w` | Seletor de janela em todas as sessões |

## Modo de Cópia

### Entrar e Navegar

| Command | Description |
|---------|-------------|
| `Prefix [` | Entrar no modo de cópia |
| `q` | Sair do modo de cópia |
| `Arrow / hjkl` | Mover cursor (modo vi) |
| `Ctrl-u / Ctrl-d` | Página acima / abaixo |
| `g / G` | Ir para o início / fim |
| `/` | Buscar para frente |
| `?` | Buscar para trás |
| `n / N` | Próxima / ocorrência anterior na busca |

### Selecionar e Copiar

| Command | Description |
|---------|-------------|
| `Space` | Iniciar seleção (modo vi) |
| `Enter` | Copiar seleção e sair |
| `Prefix ]` | Colar buffer |
| `v` | Alternar seleção retangular (modo vi) |

*Ativar modo vi: set -g mode-keys vi*

## Configuração

### Configurações Comuns (~/.tmux.conf)

```
set -g mouse on                 # ativar mouse
set -g base-index 1             # janelas começam em 1
set -g renumber-windows on      # renumerar ao fechar
set -g default-terminal "tmux-256color"
set -g history-limit 50000
```

### Recarregar Configuração

```
tmux source-file ~/.tmux.conf   # do shell
# ou Prefix : e digitar:
source-file ~/.tmux.conf
```

## Barra de Status

### Opções da Barra de Status

```
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
```

### Variáveis de Formato

| Command | Description |
|---------|-------------|
| `#S` | Nome da sessão |
| `#I` | Índice da janela |
| `#W` | Nome da janela |
| `#P` | Índice do painel |
| `#H` | Nome do host |
| `#T` | Título do painel |

## Atalhos de Teclado

### Atalhos Personalizados

```
# mudar prefixo para Ctrl-a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# navegação de painéis estilo vim
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
```

### Sintaxe de Atalho

| Command | Description |
|---------|-------------|
| `bind X command` | Vincular Prefix + X ao comando |
| `bind -n M-Left prev` | Vincular sem prefixo (Alt-Left) |
| `bind -r H resize-pane -L 5` | Atalho repetível |
| `unbind X` | Remover um atalho |
| `Prefix ?` | Listar todos os atalhos atuais |

## Scripting

### Layouts via Script

```
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
```

### Comandos Úteis

| Command | Description |
|---------|-------------|
| `tmux send-keys -t sess 'cmd' Enter` | Enviar teclas para uma sessão |
| `tmux capture-pane -p` | Imprimir conteúdo do painel na saída padrão |
| `tmux display-message '#S'` | Exibir uma mensagem / variável |
| `tmux set-environment -g VAR val` | Definir uma variável de ambiente |
| `tmux list-keys` | Listar todos os atalhos de teclado |

## Padrões Comuns

### ~/.tmux.conf Recomendado

```
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}"
```

### Receitas Rápidas

| Command | Description |
|---------|-------------|
| `Sobreviver à desconexão SSH` | `tmux new -s work` → desconectar → `tmux attach -t work` |
| `Compartilhar sessão` | Ambos usam `tmux attach -t same` (somente leitura: `-r`) |
| `Salvar scrollback` | `tmux capture-pane -S -3000 -p > log.txt` |
| `Fechar painel travado` | Prefix x, ou `tmux kill-pane -t %N` |
| `Reordenar janelas` | `:swap-window -s 3 -t 1` |
