# Tham Khảo Nhanh tmux

*Sessions, windows, panes, copy mode, cấu hình*

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

## Quản lý Session

### Tạo & Kết nối

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

### Lệnh Session

| Command | Description |
|---------|-------------|
| `tmux ls` | Liệt kê tất cả sessions |
| `tmux kill-session -t work` | Kết thúc session có tên |
| `tmux kill-server` | Kết thúc tmux server và tất cả sessions |
| `tmux rename-session -t old new` | Đổi tên session |
| `Prefix d` | Tách khỏi session hiện tại |
| `Prefix $` | Đổi tên session hiện tại |
| `Prefix s` | Liệt kê sessions (tương tác) |

*Prefix mặc định là Ctrl-b*

## Windows

### Phím tắt Window

| Command | Description |
|---------|-------------|
| `Prefix c` | Tạo window mới |
| `Prefix ,` | Đổi tên window hiện tại |
| `Prefix &` | Đóng window hiện tại (xác nhận) |
| `Prefix n` | Window tiếp theo |
| `Prefix p` | Window trước |
| `Prefix 0-9` | Chuyển đến window theo số |
| `Prefix w` | Liệt kê windows (chọn tương tác) |
| `Prefix l` | Chuyển đến window hoạt động gần nhất |

### Lệnh Window

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

## Panes

### Chia & Đóng

| Command | Description |
|---------|-------------|
| `Prefix %` | Chia ngang (trái \| phải) |
| `Prefix "` | Chia dọc (trên / dưới) |
| `Prefix x` | Đóng pane hiện tại (xác nhận) |
| `Prefix !` | Tách pane thành window riêng |
| `Prefix z` | Bật/tắt zoom pane (toàn window) |

### Thay đổi kích thước Panes

| Command | Description |
|---------|-------------|
| `Prefix Ctrl-Arrow` | Thay đổi kích thước 1 ô |
| `Prefix Alt-Arrow` | Thay đổi kích thước 5 ô |
| `Prefix Space` | Chuyển qua các bố cục |

*Bố cục: even-horizontal, even-vertical, main-horizontal, main-vertical, tiled*

## Điều hướng

### Di chuyển giữa các Panes

| Command | Description |
|---------|-------------|
| `Prefix Arrow` | Di chuyển đến pane theo hướng |
| `Prefix o` | Chuyển sang pane tiếp theo |
| `Prefix q` | Hiển thị số pane, nhấn số để nhảy |
| `Prefix {` | Hoán đổi pane với pane trước |
| `Prefix }` | Hoán đổi pane với pane tiếp |

### Di chuyển giữa các Sessions

| Command | Description |
|---------|-------------|
| `Prefix (` | Chuyển đến session trước |
| `Prefix )` | Chuyển đến session tiếp theo |
| `Prefix s` | Chọn session (dạng cây) |
| `Prefix w` | Chọn window trong tất cả sessions |

## Copy Mode

### Vào & Điều hướng

| Command | Description |
|---------|-------------|
| `Prefix [` | Vào copy mode |
| `q` | Thoát copy mode |
| `Arrow / hjkl` | Di chuyển con trỏ (vi mode) |
| `Ctrl-u / Ctrl-d` | Cuộn lên / xuống một trang |
| `g / G` | Nhảy lên đầu / xuống cuối |
| `/` | Tìm tiến |
| `?` | Tìm lùi |
| `n / N` | Kết quả tìm tiếp / trước |

### Chọn & Sao chép

| Command | Description |
|---------|-------------|
| `Space` | Bắt đầu chọn (vi mode) |
| `Enter` | Sao chép selection và thoát |
| `Prefix ]` | Dán buffer |
| `v` | Bật/tắt chọn hình chữ nhật (vi mode) |

*Bật vi mode: set -g mode-keys vi*

## Cấu hình

### Cài đặt thường dùng (~/.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
```

### Tải lại cấu hình

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

## Thanh trạng thái

### Tùy chọn thanh trạng thái

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

### Biến định dạng

| Command | Description |
|---------|-------------|
| `#S` | Tên session |
| `#I` | Chỉ mục window |
| `#W` | Tên window |
| `#P` | Chỉ mục pane |
| `#H` | Hostname |
| `#T` | Tiêu đề pane |

## Phím tắt

### Binding tùy chỉnh

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

### Cú pháp Binding

| Command | Description |
|---------|-------------|
| `bind X command` | Gán Prefix + X vào lệnh |
| `bind -n M-Left prev` | Gán không cần prefix (Alt-Left) |
| `bind -r H resize-pane -L 5` | Binding có thể lặp lại |
| `unbind X` | Xóa một binding |
| `Prefix ?` | Liệt kê tất cả phím tắt hiện tại |

## Scripting

### Bố cục bằng 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
```

### Lệnh hữu ích

| Command | Description |
|---------|-------------|
| `tmux send-keys -t sess 'cmd' Enter` | Gửi phím tắt đến session |
| `tmux capture-pane -p` | In nội dung pane ra stdout |
| `tmux display-message '#S'` | Hiển thị thông điệp / biến |
| `tmux set-environment -g VAR val` | Thiết lập biến môi trường |
| `tmux list-keys` | Liệt kê tất cả phím tắt |

## Các mẫu thường dùng

### ~/.tmux.conf khuyến nghị

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

### Công thức nhanh

| Command | Description |
|---------|-------------|
| `Survive SSH disconnect` | `tmux new -s work` → ngắt kết nối → `tmux attach -t work` |
| `Share session` | Cả hai người `tmux attach -t same` (chỉ đọc: `-r`) |
| `Save scrollback` | `tmux capture-pane -S -3000 -p > log.txt` |
| `Kill stuck pane` | Prefix x, hoặc `tmux kill-pane -t %N` |
| `Reorder windows` | `:swap-window -s 3 -t 1` |
