# tmux 빠른 참조

*세션, 윈도우, 패널, 복사 모드, 설정*

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

## 세션 관리

### 생성 및 연결

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

### 세션 명령어

| Command | Description |
|---------|-------------|
| `tmux ls` | 모든 세션 목록 |
| `tmux kill-session -t work` | 지정 세션 종료 |
| `tmux kill-server` | tmux 서버와 모든 세션 종료 |
| `tmux rename-session -t old new` | 세션 이름 변경 |
| `Prefix d` | 현재 세션에서 분리 |
| `Prefix $` | 현재 세션 이름 변경 |
| `Prefix s` | 세션 목록 (인터랙티브) |

*기본 프리픽스는 Ctrl-b*

## 윈도우

### 윈도우 키 바인딩

| Command | Description |
|---------|-------------|
| `Prefix c` | 새 윈도우 생성 |
| `Prefix ,` | 현재 윈도우 이름 변경 |
| `Prefix &` | 현재 윈도우 닫기 (확인) |
| `Prefix n` | 다음 윈도우 |
| `Prefix p` | 이전 윈도우 |
| `Prefix 0-9` | 번호로 윈도우 전환 |
| `Prefix w` | 윈도우 목록 (인터랙티브 선택) |
| `Prefix l` | 마지막 활성 윈도우로 전환 |

### 윈도우 명령어

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

## 패널

### 분할 및 닫기

| Command | Description |
|---------|-------------|
| `Prefix %` | 수평 분할 (왼쪽 \| 오른쪽) |
| `Prefix "` | 수직 분할 (위 / 아래) |
| `Prefix x` | 현재 패널 닫기 (확인) |
| `Prefix !` | 패널을 독립 윈도우로 분리 |
| `Prefix z` | 패널 줌 전환 (전체 화면) |

### 패널 크기 조정

| Command | Description |
|---------|-------------|
| `Prefix Ctrl-Arrow` | 1 셀씩 크기 조정 |
| `Prefix Alt-Arrow` | 5 셀씩 크기 조정 |
| `Prefix Space` | 레이아웃 순환 |

*레이아웃: even-horizontal, even-vertical, main-horizontal, main-vertical, tiled*

## 내비게이션

### 패널 간 이동

| Command | Description |
|---------|-------------|
| `Prefix Arrow` | 방향의 패널로 이동 |
| `Prefix o` | 다음 패널로 순환 |
| `Prefix q` | 패널 번호 표시 후 번호 입력으로 이동 |
| `Prefix {` | 이전 패널과 교체 |
| `Prefix }` | 다음 패널과 교체 |

### 세션 간 이동

| Command | Description |
|---------|-------------|
| `Prefix (` | 이전 세션으로 전환 |
| `Prefix )` | 다음 세션으로 전환 |
| `Prefix s` | 세션 선택기 (트리 뷰) |
| `Prefix w` | 모든 세션의 윈도우 선택기 |

## 복사 모드

### 진입 및 내비게이션

| Command | Description |
|---------|-------------|
| `Prefix [` | 복사 모드 진입 |
| `q` | 복사 모드 종료 |
| `Arrow / hjkl` | 커서 이동 (vi 모드) |
| `Ctrl-u / Ctrl-d` | 페이지 위 / 아래 |
| `g / G` | 맨 위 / 맨 아래로 이동 |
| `/` | 앞으로 검색 |
| `?` | 뒤로 검색 |
| `n / N` | 다음 / 이전 검색 결과 |

### 선택 및 복사

| Command | Description |
|---------|-------------|
| `Space` | 선택 시작 (vi 모드) |
| `Enter` | 선택 복사 후 종료 |
| `Prefix ]` | 버퍼 붙여넣기 |
| `v` | 직사각형 선택 전환 (vi 모드) |

*vi 모드 활성화: set -g mode-keys vi*

## 설정

### 일반 설정 (~/.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
```

### 설정 다시 로드

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

## 상태 표시줄

### 상태 표시줄 옵션

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

### 형식 변수

| Command | Description |
|---------|-------------|
| `#S` | 세션 이름 |
| `#I` | 윈도우 인덱스 |
| `#W` | 윈도우 이름 |
| `#P` | 패널 인덱스 |
| `#H` | 호스트명 |
| `#T` | 패널 제목 |

## 키 바인딩

### 커스텀 바인딩

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

### 바인딩 구문

| Command | Description |
|---------|-------------|
| `bind X command` | Prefix + X를 명령에 바인딩 |
| `bind -n M-Left prev` | 프리픽스 없이 바인딩 (Alt-Left) |
| `bind -r H resize-pane -L 5` | 반복 가능한 바인딩 |
| `unbind X` | 바인딩 제거 |
| `Prefix ?` | 현재 키 바인딩 목록 |

## 스크립팅

### 스크립트로 레이아웃 생성

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

### 유용한 명령어

| Command | Description |
|---------|-------------|
| `tmux send-keys -t sess 'cmd' Enter` | 세션에 키스트로크 전송 |
| `tmux capture-pane -p` | 패널 내용을 stdout으로 출력 |
| `tmux display-message '#S'` | 메시지 / 변수 표시 |
| `tmux set-environment -g VAR val` | 환경 변수 설정 |
| `tmux list-keys` | 모든 키 바인딩 목록 |

## 자주 쓰는 패턴

### 추천 ~/.tmux.conf

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

### 빠른 레시피

| Command | Description |
|---------|-------------|
| `Survive SSH disconnect` | `tmux new -s work` → 연결 해제 → `tmux attach -t work` |
| `Share session` | 두 사용자 모두 `tmux attach -t same` (읽기 전용: `-r`) |
| `Save scrollback` | `tmux capture-pane -S -3000 -p > log.txt` |
| `Kill stuck pane` | Prefix x, 또는 `tmux kill-pane -t %N` |
| `Reorder windows` | `:swap-window -s 3 -t 1` |
