這個單元解決什麼問題slash command 把一段常用 prompt 或流程綁成
/name 一鍵叫用。它和 Skill 的關鍵差別是:command 由你主動觸發,Skill 由模型自動判斷是否叫用。本單元講 command 的格式、製作方式、$ARGUMENTS 參數注入,以及和 Skill 的分工判準。學習目標
- 說出 command(主動觸發)與 Skill(模型自動叫用)的差別與分工。
- 在
.claude/commands/或~/.claude/commands/下建立自訂 command 檔。 - 用
$ARGUMENTS、$0、$1等語法接收參數,讓 command 具備動態能力。 - 用
disable-model-invocation: true限制 Claude 自動叫用特定 command。 - 判斷一個需求該做成 command 還是 Skill,並說出判準。
1. command 是什麼
slash command 是你在 Claude Code session 開頭打/name 觸發的封裝 prompt。它不依賴模型主動識別意圖,你打什麼就跑什麼(截至 2026-06,依官方 commands 章節 [1])。
三個關鍵特性:
- 主動觸發:你不打,Claude 也不會建議打。決定權完全在你。
- 可重用:寫一次,之後每次都一樣的叫用方式。
- 可版控:放在
.claude/commands/內可進 Git,團隊共享。
2. 與 Skill 的差別:主動 vs 自動
.claude/commands/<name>.md 與 Skill(SKILL.md)在 Claude Code 裡現在已合併為同一套底層機制 [2]。但兩者觸發時機不同:
| 維度 | Command | Skill |
|---|---|---|
| 觸發方式 | 使用者主動打 /name | 模型依 description 語意判斷叫用 |
| 預設可見性 | / 選單顯示,使用者輸入 | 模型可見,使用者也可打 /name |
| 寫在哪裡 | .claude/commands/<name>.md 或 ~/.claude/commands/<name>.md | .claude/skills/<name>/SKILL.md 或 ~/.claude/skills/<name>/SKILL.md |
| 同名衝突 | 若 skill 與 command 同名,skill 優先 [2] | 同左 |
| 適合場合 | 「我知道現在要做 X」 | 「讓模型判斷什麼時候做 X」 |
- 確定性高、流程固定 → command。
- 有副作用、你不希望模型自己決定何時跑(deploy、發送訊息、commit)→ command +
disable-model-invocation: true。 - 依使用者意圖判斷才合適(code review、文件搜尋、refactor 建議)→ Skill。
3. 製作:檔案位置與基本結構
放路徑(截至 2026-06):| 位置 | 適用範圍 |
|---|---|
~/.claude/commands/<name>.md | 所有專案通用 |
./.claude/commands/<name>.md | 當前專案,透過版控共享給團隊 |
<plugin>/commands/<name>.md | 在已啟用 plugin 內可用 |
/ 後面要打的字:.claude/commands/deploy.md 對應 /deploy,.claude/commands/fix-issue.md 對應 /fix-issue。
3.1 最簡形式
/commit 觸發後,這段就會被注入 Claude 當下對話,Claude 用它產生 commit message。
3.2 用 frontmatter 設定行為
| 欄位 | 用途 |
|---|---|
description | 在 / 選單顯示、Claude 判斷語意時的提示 |
argument-hint | 自動完成時的提示文字,例如 [issue-number] |
allowed-tools | 列出 command 啟用時不需要額外確認的工具 |
model | 指定使用模型,預設繼承當前 session |
disable-model-invocation: true | 禁止 Claude 自動叫用此 command |
user-invocable: false | 從 / 選單隱藏,只讓模型叫用 |
為什麼要
disable-model-invocation: true假設你寫了 /deploy command。如果不限制,模型可能在對話中看到「這段程式碼看起來該部署了」就自己叫用 /deploy,然後跑了部署;你可能還在 review。設了 disable-model-invocation: true 之後,這個 command 只在你明確打 /deploy 時才會跑,模型完全看不到它。對 deploy、send、push 這類有副作用的工作流,預設都該加。4. 參數注入:$ARGUMENTS 與索引語法
command 觸發時,你打 /name 後面這些字 會被注入到 command 內容中。
最簡單的接收方式:$ARGUMENTS
用 打
$ARGUMENTS 接收你在 /name 後面打的所有文字:/fix-issue 123 時,$ARGUMENTS 被替換為 123,Claude 收到的是「Fix GitHub issue 123 following our coding standards. …」[2]。如果 command 內容裡沒有 $ARGUMENTS,Claude Code 會把參數以 ARGUMENTS: <你的輸入> 形式附加在 command 內容尾端,Claude 還是看得到 [2]。位置索引:$0、$1、$2
需要把多個參數拆開處理時,用 打
$0、$1、$2 或 $ARGUMENTS[0] 等:/migrate-component SearchBar React Vue 時,$0 為 SearchBar,$1 為 React,$2 為 Vue。多字參數要用引號包起來:/migrate-component "Search Bar" React "Vue 3"。4.1 其他可用變數
| 變數 | 用途 |
|---|---|
${CLAUDE_SESSION_ID} | 當前 session ID,用於日誌或 session 特定檔名 |
${CLAUDE_SKILL_DIR} | command/skill 所在目錄,用於引用同目錄腳本 |
${CLAUDE_EFFORT} | 當前 effort 等級(low / medium / high / xhigh / max) |
5. 動態 context 注入:!`command`
command 內容裡可以用 !`shell command` 在送進 Claude 之前先執行 shell 指令,把輸出嵌入內容 [2]:
!`cmd`,把輸出取代進去,然後才把整段內容當作 prompt 交給 Claude。對 Claude 來說,它看到的是已經包含 git diff 結果的純文字。
多行指令用 fenced code block 形式:
6. 工具對照
| 概念 | Anthropic Claude(主範本) | OpenAI Codex | GitHub Copilot | Cursor |
|---|---|---|---|---|
| 自訂 command 位置 | .claude/commands/<name>.md、~/.claude/commands/<name>.md | config.toml 設定 [需確認原始出處:Codex custom slash command 截至 2026-06 的官方路徑] | .github/prompts/<name>.prompt.md(prompt files 機制) | .cursor/commands/<name>.md |
| 參數注入語法 | $ARGUMENTS、$0、$1、具名變數 | 不適用一般 ARGUMENTS 語法 | frontmatter 結構化欄位 | $ARGUMENTS 風格變數 |
| 全域 vs 專案作用域 | ~/.claude/commands/ vs .claude/commands/ | ~/.codex/prompts/ vs .codex/prompts/ | 全域與專案分檔 | 全域 User Rules + 專案 .cursor/rules/ |
| 觸發方式 | 使用者主動輸入 /name | 使用者主動輸入 | 使用者主動輸入 | 使用者主動輸入 |
| 防止模型自動叫用 | disable-model-invocation: true | (機制差異大) | (機制差異大) | (機制差異大) |
命名澄清Claude Code 的 command 與 skill 為同一底層機制:截至 2026-06 兩者已合併,
.claude/commands/<name>.md 與 .claude/skills/<name>/SKILL.md 都能產生 /name [2]。差別在前者放單一檔、後者放目錄(含 references/ 與 scripts/,見 04-4 Skill)。本單元講的是兩者共通的「使用者主動觸發」這一面。OpenAI Codex、GitHub Copilot、Cursor 各有「prompt file」或「prompt template」概念,但路徑與機制差異大;通用寫法是「prompt file 支援 $ARGUMENTS 風格變數」,細節以各家當前文件為準。7. 動手做
30 分鐘練習
- 寫一個
/commitcommand:用 frontmatter 帶disable-model-invocation: true與allowed-tools: Bash(git *);內容用!`git diff --cached`與!`git log --oneline -10`取得 staged diff 與最近風格,請 Claude 依 Conventional Commits 格式輸出 commit message。 - 建一個全域 command:
~/.claude/commands/weekly-review.md,用$ARGUMENTS接收你想 focus 的主題;啟動新專案 session 測試/weekly-review可跨專案叫用。 - 測試防誤觸:寫一個
/deploy-prodcommand,故意不設disable-model-invocation;在對話中提到「看起來該 deploy 了」觀察 Claude 是否會自己叫用。然後加上disable-model-invocation: true,重複同一句話,確認這次 Claude 不會自己跑。
8. 常見誤區
自我檢核
通過本單元的標準
- 你能說出 command 與 Skill 的根本差別嗎?(觸發主體不同)
- 你能在五分鐘內寫出一個能用
$ARGUMENTS接收參數的 command 嗎? - 你能判斷哪些 command 該加
disable-model-invocation: true嗎? - 你手邊有沒有一個每週重複打三次以上的 prompt?它該做成 command 還是 Skill?說出你的判準。
來源與延伸閱讀
事實主張依官方文件,快變動項標註截至 2026-05。- [1] Anthropic, “Commands,” code.claude.com, 2026. [Online]. Available: https://code.claude.com/docs/en/commands (截至 2026-06;含內建與自訂 command)
- [2] Anthropic, “Extend Claude with skills,” code.claude.com, 2026. [Online]. Available: https://code.claude.com/docs/en/skills (截至 2026-06;含 command 與 skill 合併機制、
$ARGUMENTS與 frontmatter 完整參考) - [3] Anthropic, “How Claude remembers your project,” code.claude.com, 2026. [Online]. Available: https://code.claude.com/docs/en/memory (截至 2026-06;
CLAUDE.md與 rules 模組化)
CLAUDE.md與自動記憶見 04-1 CLAUDE.md 與記憶檔。- 規則檔 path-scoped 機制見 04-2 rules。
- Skill 進階(references/、scripts/、漸進式揭露)見 04-4 Skill。
- Hook 強制執行見 04-6 Hooks。