OpenCode 命令
透過 Markdown 檔案或自訂提示詞建立可複用的 OpenCode 命令。支援參數傳遞、shell 輸出擷取,以及巢狀子任務編排。
本頁內文若連結到尚未提供繁體版本的頁面(例如 /ecosystem/plugins/、/news/),會開啟英文原頁;導覽與頁尾標有 EN 的項目即為這類頁面。
自訂命令讓你指定在 TUI 中執行該命令時要送出的提示詞。
/my-command
自訂命令是內建命令(如 /init、/undo、/redo、/help)的補充。瞭解更多。
建立指令檔案
在 commands/ 目錄中建立 markdown 檔案來定義自訂命令。
建立 .opencode/commands/test.md:
---
description: Run tests with coverage
agent: build
model: <provider>/<model>
---
Run the full test suite with coverage report and show any failures.
Focus on the failing tests and suggest fixes.
frontmatter 定義命令屬性,內容則成為模板。
輸入 / 後接命令名稱即可使用該命令。
"/test"
設定
你可以透過 OpenCode 設定,或在 commands/ 目錄中建立 markdown 檔案來新增自訂命令。
JSON
使用 command 選項設定命令:
{
"$schema": "https://opencode.ai/config.json",
"command": {
// 這會成為命令名稱
"test": {
// 這是將傳送給 LLM 的提示詞
"template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes.",
// 這在 TUI 中顯示為描述
"description": "Run tests with coverage",
"agent": "build",
"model": "<provider>/<model>"
}
}
} 現在你可以在 TUI 中執行這個命令:
/test
Markdown
你也可以用 markdown 檔案定義命令,把它們放在:
- 全域:
~/.config/opencode/commands/ - 專案級:
.opencode/commands/
---
description: Run tests with coverage
agent: build
model: <provider>/<model>
---
Run the full test suite with coverage report and show any failures.
Focus on the failing tests and suggest fixes.
markdown 檔名會成為命令名稱。例如,test.md 讓你可以執行:
/test
提示詞設定
自訂命令的提示詞支援多種特殊佔位符與語法。
參數
使用 $ARGUMENTS 佔位符把參數傳遞給命令。
---
description: Create a new component
---
Create a new React component named $ARGUMENTS with TypeScript support.
Include proper typing and basic structure.
使用參數執行命令:
/component Button
$ARGUMENTS 會被替換成 Button。
你也可以用位置參數存取單一參數:
$1- 第一個參數$2- 第二個參數$3- 第三個參數- 以此類推…
例如:
---
description: Create a new file with content
---
Create a file named $1 in the directory $2
with the following content: $3
執行命令:
/create-file config.json src "{ \"key\": \"value\" }"
這會替換成:
$1為config.json$2為src$3為{ "key": "value" }
Shell 輸出
使用 !command 將 bash 命令 的輸出注入你的提示詞中。
例如,建立一個分析測試覆蓋率的自訂命令:
---
description: Analyze test coverage
---
Here are the current test results:
!`npm test`
Based on these results, suggest improvements to increase coverage.
或檢視最近的變更:
---
description: Review recent changes
---
Recent git commits:
!`git log --oneline -10`
Review these changes and suggest any improvements.
命令在你的專案根目錄中執行,輸出會成為提示詞的一部分。
檔案引用
使用 @ 後接檔案名稱來引用檔案。
---
description: Review component
---
Review the component in @src/components/Button.tsx.
Check for performance issues and suggest improvements.
檔案內容會自動包含在提示詞中。
設定選項
讓我們詳細瞭解設定選項。
Template
template 選項定義執行命令時傳送給 LLM 的提示詞。
{
"command": {
"test": {
"template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes."
}
}
} 這是必填的設定選項。
Description
使用 description 選項提供命令功能的簡要描述。
{
"command": {
"test": {
"template": "Run the full test suite with coverage report.",
"description": "Run tests with coverage"
}
}
} 這在 TUI 中輸入命令時顯示為描述。
Agent
使用 agent 設定可選地指定哪個 智慧代理 應該執行此命令。如果是 子智慧代理,命令將預設觸發子智慧代理呼叫。要停用此行為,請設定 subtask 為 false。
{
"command": {
"review": {
"template": "審查當前變更並列出發現的問題。",
"agent": "plan"
}
}
} 這是可選的設定選項。如果未指定,預設為目前的智慧代理。
Subtask
使用 subtask 布林值強制命令觸發 子智慧代理 呼叫。如果你希望命令不汙染主上下文,這很有用,並且會強制智慧代理充當子智慧代理,即使智慧代理 設定中 mode 設為 primary。
{
"command": {
"analyze": {
"template": "分析選取的程式碼並解釋其運作方式。",
"subtask": true
}
}
} 這是可選的設定選項。
Model
使用 model 設定覆寫此命令的預設模型。
{
"command": {
"analyze": {
"template": "分析選取的程式碼並解釋其運作方式。",
"model": "<provider>/<model>"
}
}
} 這是可選的設定選項。
內建命令
opencode 包含多個內建命令,如 /init、/undo、/redo、/help;瞭解更多。