測驗:打造你的 tmux + Claude Code 開發環境
共 5 題,點選答案後會立即顯示結果
1. 在 .tmux.conf 中,為什麼建議把視窗的起始編號從 0 改成 1?
2. 以下設定中,-c "#{pane_current_path}" 的作用是什麼?
3. 在自動化啟動腳本中,為什麼要先用 tmux has-session 檢查 session 是否存在?
4. 關於 tmux-resurrect 和 tmux-continuum 這兩個外掛,以下敘述何者正確?
5. 使用 tmuxinator 定義開發環境時,以下 YAML 設定中的 layout: main-vertical 代表什麼意思?
系列定位
這是 tmux x Claude Code 系列的最後一篇。前三篇我們學會了 tmux 的基本操作、視窗與面板管理、以及 Claude Code 工作流。這篇要教你客製化一切,打造一個屬於自己的長期開發環境。
一句話說明
透過 .tmux.conf 設定檔和自動化腳本,讓 tmux + Claude Code 用起來更順手、啟動更快。
為什麼需要自訂設定?
tmux 的預設設定其實不太好用:
| 預設行為 | 問題 | 改完之後 |
|---|---|---|
前綴鍵是 Ctrl+b |
手指要伸很遠 | 改成 Ctrl+a,近多了 |
| 沒有滑鼠支援 | 不能用滑鼠切面板 | 滑鼠點哪切哪 |
| 視窗從 0 開始編號 | 鍵盤上 0 在最右邊 | 從 1 開始,左到右 |
分割面板用 % 和 " |
完全記不住 | 改成 | 和 -,直覺 |
自訂完之後,你每天省下的操作時間是很可觀的。
.tmux.conf 在哪裡?
.tmux.conf 是 tmux 的設定檔,放在你的 home 目錄:
~/.tmux.conf
如果還沒有這個檔案,自己建一個:
touch ~/.tmux.conf
編輯完之後,有兩種方式讓設定生效:
# 方法 1:在 tmux 裡按前綴鍵 + : 然後輸入
source-file ~/.tmux.conf
# 方法 2:直接在終端跑
tmux source-file ~/.tmux.conf
Code language: PHP (php)小技巧:等一下我們會在設定檔裡加一個快捷鍵,讓你按
前綴鍵 + r就能重新載入設定。
基礎配置:讓 tmux 好用十倍
改前綴鍵
# 把前綴鍵從 Ctrl+b 改成 Ctrl+a
unbind C-b # 解除原本的 Ctrl+b
set -g prefix C-a # 設定新前綴為 Ctrl+a
bind C-a send-prefix # 按兩次 Ctrl+a 會送出真正的 Ctrl+a
Code language: PHP (php)逐行翻譯:
unbind C-b— 取消 Ctrl+b 作為前綴鍵set -g prefix C-a— 把 Ctrl+a 設為新的前綴鍵(-g表示全域設定)bind C-a send-prefix— 如果你在終端真的需要按 Ctrl+a(例如跳到行首),按兩次就好
啟用滑鼠
# 一行搞定:可以用滑鼠點選面板、拖曳調整大小、滾動內容
set -g mouse on
Code language: PHP (php)這在幹嘛: 開啟滑鼠支援後,你可以用滑鼠點擊切換面板、拖曳面板邊框調整大小、滾輪捲動歷史輸出。對新手來說超方便。
視窗和面板索引從 1 開始
set -g base-index 1 # 視窗編號從 1 開始
setw -g pane-base-index 1 # 面板編號從 1 開始
Code language: PHP (php)為什麼: 鍵盤上 1 在最左邊,0 在最右邊。視窗從 1 開始編號,按 前綴鍵 + 1 切到第一個視窗,手指動線更直覺。
更直覺的分割快捷鍵
# 用 | 水平分割(左右),用 - 垂直分割(上下)
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"' # 解除原本的垂直分割鍵
unbind % # 解除原本的水平分割鍵
Code language: PHP (php)逐行翻譯:
bind |— 按前綴鍵再按|就會水平分割-h— 水平分割(horizontal)-c "#{pane_current_path}"— 新面板沿用目前的工作目錄(超重要!不然每次都跑回 home)unbind '"'和unbind %— 把那兩個難記的原始快捷鍵取消
快速重新載入設定
# 按前綴鍵 + r 重新載入設定檔
bind r source-file ~/.tmux.conf \; display-message "Config reloaded!"
Code language: PHP (php)這在幹嘛: 改完設定之後,按 Ctrl+a 再按 r 就能立即套用,不用重開 tmux。display-message 會在狀態列顯示提示訊息。
外觀自訂:狀態列配色
tmux 的狀態列預設是一條綠色的橫條,看起來很陽春。我們來美化它。
基本配色
# 狀態列底色和文字顏色
set -g status-style 'bg=#1e1e2e fg=#cdd6f4'
# 目前選中的視窗用不同顏色標示
setw -g window-status-current-style 'bg=#89b4fa fg=#1e1e2e bold'
# 沒選中的視窗
setw -g window-status-style 'fg=#6c7086'
Code language: PHP (php)翻譯:
| 設定 | 意思 |
|---|---|
status-style 'bg=#1e1e2e' |
狀態列背景色(深色) |
fg=#cdd6f4 |
狀態列文字顏色(淺色) |
window-status-current-style |
目前所在視窗的樣式 |
bold |
粗體,讓它更明顯 |
狀態列顯示有用資訊
# 左邊:顯示 session 名稱
set -g status-left '#[fg=#89b4fa,bold] #S '
set -g status-left-length 30
# 右邊:顯示日期時間
set -g status-right '#[fg=#a6adc8] %Y-%m-%d %H:%M '
set -g status-right-length 50
# 每 5 秒更新一次狀態列
set -g status-interval 5
Code language: PHP (php)逐行翻譯:
#S— 顯示目前 session 的名稱(例如claude-dev)#[fg=#89b4fa,bold]— 設定接下來文字的顏色和粗體%Y-%m-%d %H:%M— 日期時間格式,例如2026-02-07 14:30status-interval 5— 每 5 秒更新狀態列(預設是 15 秒)
常見變化:加入更多系統資訊
# 顯示主機名稱和時間
set -g status-right '#[fg=#f38ba8]#H #[fg=#a6adc8]| %H:%M '
Code language: PHP (php)#H— 主機名稱(hostname)
知道就好:有些人會用外掛在狀態列顯示 CPU 使用率、記憶體、電池電量等。如果你好奇可以搜尋
tmux-cpu或tmux-battery這類外掛。
自動化啟動腳本:一鍵進入開發模式
每次開機都要手動建 session、開視窗、分面板、啟動 Claude Code… 太累了。我們寫一個 shell script,一鍵搞定。
最小範例
#!/bin/bash
# 檔案:~/dev-start.sh
# 用途:一鍵啟動 Claude Code 開發環境
SESSION="claude-dev"
# 如果 session 已經存在,直接連上去
tmux has-session -t $SESSION 2>/dev/null
if [ $? -eq 0 ]; then
tmux attach -t $SESSION
exit 0
fi
# 建立新 session,第一個視窗叫 "editor"
tmux new-session -d -s $SESSION -n editor
# 在 editor 視窗啟動 Claude Code
tmux send-keys -t $SESSION:editor "claude" Enter
# 建立第二個視窗叫 "terminal"
tmux new-window -t $SESSION -n terminal
# 回到第一個視窗
tmux select-window -t $SESSION:editor
# 連上 session
tmux attach -t $SESSION
Code language: PHP (php)逐行翻譯:
SESSION="claude-dev"
# 把 session 名稱存成變數,方便後面引用
tmux has-session -t $SESSION 2>/dev/null
# 檢查叫 "claude-dev" 的 session 是否已經存在
# 2>/dev/null 是把錯誤訊息丟掉,不要顯示
if [ $? -eq 0 ]; then
# $? 是上一個指令的結束代碼,0 表示成功(session 存在)
tmux new-session -d -s $SESSION -n editor
# -d 表示在背景建立(不馬上連進去)
# -s 設定 session 名稱
# -n 設定第一個視窗名稱
tmux send-keys -t $SESSION:editor "claude" Enter
# 對 editor 視窗送出 "claude" 指令並按 Enter
# 這就會啟動 Claude Code
tmux new-window -t $SESSION -n terminal
# 在 session 裡新增第二個視窗叫 "terminal"
Code language: PHP (php)使用方式:
chmod +x ~/dev-start.sh # 給執行權限(只需要做一次)
~/dev-start.sh # 之後每次跑這個就好
Code language: PHP (php)進階版:多面板佈局
#!/bin/bash
# 檔案:~/dev-start-pro.sh
# 用途:進階版 Claude Code 開發環境(含面板佈局)
SESSION="claude-dev"
tmux has-session -t $SESSION 2>/dev/null && tmux attach -t $SESSION && exit 0
# 視窗 1:主開發區
tmux new-session -d -s $SESSION -n dev -c ~/projects
# 左邊面板:Claude Code
tmux send-keys -t $SESSION:dev "claude" Enter
# 右邊面板:終端機(佔 40% 寬度)
tmux split-window -h -t $SESSION:dev -p 40 -c ~/projects
# 視窗 2:Git 操作
tmux new-window -t $SESSION -n git -c ~/projects
tmux send-keys -t $SESSION:git "git status" Enter
# 視窗 3:伺服器日誌
tmux new-window -t $SESSION -n logs -c ~/projects
# 回到第一個視窗的第一個面板
tmux select-window -t $SESSION:dev
tmux select-pane -t $SESSION:dev.0
tmux attach -t $SESSION
Code language: PHP (php)關鍵指令翻譯:
| 指令 | 意思 |
|---|---|
split-window -h -p 40 |
水平分割,新面板佔 40% 寬度 |
-c ~/projects |
新面板的工作目錄設為 ~/projects |
select-pane -t $SESSION:dev.0 |
選到 dev 視窗的第 0 個面板 |
tmuxinator:用 YAML 定義開發環境
如果你覺得 shell script 寫起來有點麻煩,tmuxinator 讓你用更好讀的 YAML 格式定義 tmux 佈局。
安裝
# macOS
brew install tmuxinator
# Ubuntu / Debian
sudo apt install tmuxinator
# 用 Ruby gem 安裝(通用)
gem install tmuxinator
Code language: PHP (php)建立專案設定
tmuxinator new claude-dev
Code language: JavaScript (javascript)這會打開一個 YAML 檔案讓你編輯(存放在 ~/.config/tmuxinator/claude-dev.yml)。
設定範例
# ~/.config/tmuxinator/claude-dev.yml
name: claude-dev
root: ~/projects
windows:
- dev:
layout: main-vertical
panes:
- claude # 左邊面板:啟動 Claude Code
- "" # 右邊面板:空的終端機
- git:
panes:
- git status # 顯示 git 狀態
- logs:
panes:
- "" # 空面板,等你要看 log 時自己用
Code language: PHP (php)逐行翻譯:
name: claude-dev # session 名稱
root: ~/projects # 所有視窗的預設工作目錄
windows: # 定義視窗列表
- dev: # 第一個視窗叫 "dev"
layout: main-vertical # 佈局方式:左邊一大塊,右邊小的
panes: # 這個視窗裡的面板
- claude # 第一個面板執行 "claude"
- "" # 第二個面板什麼都不跑
- git: # 第二個視窗叫 "git"
panes:
- git status # 面板執行 "git status"
Code language: PHP (php)啟動和管理
tmuxinator start claude-dev # 啟動這個環境
tmuxinator stop claude-dev # 關閉這個環境
tmuxinator list # 列出所有已定義的環境
tmuxinator edit claude-dev # 編輯設定
Code language: PHP (php)tmuxinator vs shell script:
| 面向 | shell script | tmuxinator |
|---|---|---|
| 門檻 | 只需要 bash | 需要額外安裝 |
| 可讀性 | 普通 | YAML 很好讀 |
| 彈性 | 高,可以寫任何邏輯 | 中,只能定義佈局和指令 |
| 推薦情境 | 需要條件判斷時 | 單純定義佈局時 |
進階技巧:tmux send-keys 與 Claude Code –resume
send-keys 自動送指令
tmux send-keys 可以從外部對 tmux 面板送指令,這在自動化腳本裡非常有用。
# 基本語法
tmux send-keys -t [目標] "[指令]" Enter
# 範例:對 claude-dev session 的 editor 視窗送指令
tmux send-keys -t claude-dev:editor "ls -la" Enter
Code language: PHP (php)翻譯: -t claude-dev:editor 指定目標是 claude-dev session 裡叫 editor 的視窗。Enter 表示按下 Enter 鍵。
與 Claude Code –resume 搭配
Claude Code 有一個 --resume 參數,可以恢復上次的對話。搭配 tmux 使用時特別強大:
#!/bin/bash
# 斷線重連後,恢復 Claude Code 上次的對話
SESSION="claude-dev"
tmux has-session -t $SESSION 2>/dev/null
if [ $? -ne 0 ]; then
# session 不存在,建立新的並用 --resume 恢復對話
tmux new-session -d -s $SESSION -n editor
tmux send-keys -t $SESSION:editor "claude --resume" Enter
tmux attach -t $SESSION
else
# session 還在,直接連上
tmux attach -t $SESSION
fi
Code language: PHP (php)這在幹嘛: 當你的電腦重啟或 SSH 斷線後,這個腳本會重建 tmux 環境,並用 claude --resume 恢復 Claude Code 上一次的對話狀態。你不會失去工作進度。
知道就好:send-keys 的注意事項
send-keys 本質上是模擬鍵盤輸入,有時候如果 tmux 面板還在忙,指令可能會「送丟」。常見做法是在 send-keys 之間加上 sleep:
tmux send-keys -t $SESSION:editor "claude" Enter
sleep 2 # 等 Claude Code 啟動完成
tmux send-keys -t $SESSION:editor "/init" Enter
Code language: PHP (php)實用外掛推薦
tmux 有一個外掛管理器叫 TPM(Tmux Plugin Manager),幾乎所有外掛都透過它安裝。
安裝 TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
Code language: PHP (php)然後在 .tmux.conf 最底部加上:
# 外掛管理器(必須放在設定檔最後面)
set -g @plugin 'tmux-plugins/tpm'
# 初始化 TPM(這一行一定要在最最後面)
run '~/.tmux/plugins/tpm/tpm'
Code language: PHP (php)安裝外掛的方法:加完設定後,在 tmux 裡按 前綴鍵 + I(大寫 I),TPM 就會自動下載。
tmux-resurrect:重啟後恢復 session
電腦重開機之後,tmux session 就沒了。tmux-resurrect 可以幫你存下來再恢復。
set -g @plugin 'tmux-plugins/tmux-resurrect'
Code language: CSS (css)| 操作 | 快捷鍵 |
|---|---|
| 儲存目前環境 | 前綴鍵 + Ctrl+s |
| 恢復環境 | 前綴鍵 + Ctrl+r |
這在幹嘛: 按 Ctrl+a 再按 Ctrl+s 就會把目前所有 session、視窗、面板的佈局和工作目錄都存起來。重開機之後,按 Ctrl+a 再按 Ctrl+r 就能一鍵恢復。
tmux-continuum:自動備份 + 自動恢復
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-restore 'on' # 啟動 tmux 時自動恢復上次環境
set -g @continuum-save-interval '15' # 每 15 分鐘自動存檔
Code language: PHP (php)這在幹嘛: tmux-continuum 是 tmux-resurrect 的自動化版本。它會每 15 分鐘自動存檔,而且下次開 tmux 時自動恢復。你完全不需要手動操作。
注意:
tmux-continuum的設定要放在外掛列表的最後面(在 TPM 的run之前),否則可能被其他外掛覆蓋。
完整的 .tmux.conf 範本
以下是一份可以直接使用的設定檔,附有完整註解:
# ============================================
# tmux 設定檔 - 適合搭配 Claude Code 使用
# 放在 ~/.tmux.conf
# ============================================
# --- 基礎設定 ---
# 改前綴鍵:Ctrl+b -> Ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# 啟用滑鼠(點選面板、拖曳調整大小、滾輪捲動)
set -g mouse on
# 視窗和面板編號從 1 開始
set -g base-index 1
setw -g pane-base-index 1
# 視窗關閉時自動重新編號
set -g renumber-windows on
# 加大歷史紀錄上限(預設 2000 行太少了)
set -g history-limit 50000
# 256 色支援
set -g default-terminal "screen-256color"
# 減少按鍵延遲(預設 500ms 太慢)
set -sg escape-time 10
# --- 快捷鍵 ---
# 按前綴鍵 + r 重新載入設定
bind r source-file ~/.tmux.conf \; display-message "Config reloaded!"
# 更直覺的分割方式
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %
# 用 Alt + 方向鍵切換面板(不需要前綴鍵)
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
# 用 Shift + 方向鍵切換視窗(不需要前綴鍵)
bind -n S-Left previous-window
bind -n S-Right next-window
# --- 外觀 ---
# 狀態列
set -g status-style 'bg=#1e1e2e fg=#cdd6f4'
set -g status-left '#[fg=#89b4fa,bold] #S #[fg=#6c7086]| '
set -g status-left-length 30
set -g status-right '#[fg=#a6adc8] %m/%d %H:%M '
set -g status-right-length 50
set -g status-interval 5
# 目前視窗
setw -g window-status-current-style 'fg=#89b4fa bold'
setw -g window-status-current-format ' #I:#W '
# 其他視窗
setw -g window-status-style 'fg=#6c7086'
setw -g window-status-format ' #I:#W '
# 面板邊框
set -g pane-border-style 'fg=#313244'
set -g pane-active-border-style 'fg=#89b4fa'
# --- 外掛(需要先安裝 TPM) ---
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
# 外掛設定
set -g @continuum-restore 'on'
set -g @continuum-save-interval '15'
# 初始化 TPM(一定要放最後一行)
run '~/.tmux/plugins/tpm/tpm'
Code language: PHP (php)如何使用這份設定
# 1. 把上面的內容貼到 ~/.tmux.conf
nano ~/.tmux.conf
# 2. 安裝 TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# 3. 重新載入設定(如果已經在 tmux 裡)
tmux source-file ~/.tmux.conf
# 4. 安裝外掛:在 tmux 裡按 Ctrl+a 再按大寫 I
Code language: PHP (php)必看懂 vs 知道就好
必看懂(本篇核心):
- set -g 是全域設定,setw -g 是視窗層級設定
- bind 綁定快捷鍵,unbind 解除綁定
- send-keys 從外部對 tmux 面板送指令
- -c "#{pane_current_path}" 讓新面板沿用目前目錄
知道就好(遇到再查):
- 狀態列的 #[] 顏色語法
- TPM 以外的外掛管理方式
- tmux 的 hook(事件觸發)機制
- tmux 的 if-shell 條件判斷語法
Code language: PHP (php)Vibe Coder 檢查點
看到 tmux 相關設定檔或腳本時確認:
- [ ]
.tmux.conf裡的前綴鍵設定是否符合你的習慣? - [ ] 啟動腳本有沒有先檢查 session 是否已存在(避免重複建立)?
- [ ]
send-keys之後有沒有適當的等待時間(sleep)? - [ ] 外掛設定是否放在 TPM
run之前? - [ ] 有沒有設定
history-limit來保留足夠的歷史紀錄?
系列總結:你的 tmux + Claude Code 學習路徑
恭喜你讀完整個系列!讓我們回顧一下四篇文章的學習路徑:
| 篇 | 主題 | 你學會了 |
|---|---|---|
| #01 | tmux 基礎 | session、視窗、面板的基本概念與操作 |
| #02 | 視窗與面板管理 | 多視窗切換、面板分割與佈局調整 |
| #03 | Claude Code 工作流 | 在 tmux 裡高效使用 Claude Code 的實戰模式 |
| #04 | 打造開發環境 | 自訂設定、自動化腳本、外掛與長期使用策略 |
接下來可以做的事
- 先套用範本:把本篇的
.tmux.conf範本貼到你的設定檔,直接開始用 - 寫啟動腳本:根據你的專案建立
dev-start.sh,每天省下 2 分鐘的環境準備時間 - 慢慢調整:用了一兩週之後,你會知道哪些快捷鍵需要改、狀態列要顯示什麼
- 安裝外掛:先裝
tmux-resurrect,重開機後再也不怕丟失環境
最重要的是:不要一次改太多。先用基礎設定,習慣了再逐步加東西。tmux 的強大之處在於它可以陪你一路成長,從簡單的終端分割器變成你的個人化開發指揮中心。
延伸閱讀:如果你想看更多 tmux 設定範例,可以在 GitHub 上搜尋 “dotfiles tmux”,很多開發者會分享自己的設定檔。
進階測驗:打造你的 tmux + Claude Code 開發環境
共 5 題,包含情境題與錯誤診斷題。