创建 .opencode/commands/review.md,即可把一段提示词变成 /review 命令:
Review $ARGUMENTS for bugs and missing tests.
在 TUI 中带上目标运行它:
/review src/auth.ts
OpenCode 会将 Review src/auth.ts for bugs and missing tests. 作为用户提示词提交。

Markdown

将全局命令放在 ~/.config/opencode/commands/,项目命令放在 .opencode/commands/。
~/.config/opencode/commands/review.md
.opencode/commands/review.md
只有 .md 文件会被发现。嵌套路径会以 / 作为分隔符构成命令名:
Review $ARGUMENTS using the team's checklist.
以 /team/review src/auth.ts 的形式运行该嵌套命令。旧版单数目录 command/ 同样会被发现,但新文件请使用 commands/。
当命令需要元数据时,可添加 YAML frontmatter。去除首尾空白后的 Markdown 正文始终作为提示词模板。
---
description: Review code for correctness
agent: plan
model: anthropic/claude-sonnet-5-5#high
---

Review $ARGUMENTS. Report bugs first.

JSON

在任意 OpenCode JSON 或 JSONC 配置文件 的 commands 下定义命令。每个命令都需要一个 template。
{
  "$schema": "https://opencode.ai/config.json",
  "commands": {
    "review": {
      "description": "Review code for correctness",
      "template": "Review $ARGUMENTS. Report bugs first.",
    },
  },
}

字段

Markdown frontmatter 与 JSON 条目接受相同的字段,区别在于 Markdown 的 template 取自文件正文。
| 字段 | 必填 | 行为 | | --- | --- | --- | | template | 仅 JSON | 提示词模板。 | | description | 否 | 在命令列表和发现结果中显示的文本。 | | agent | 否 | 命令运行时选定的智能体。 | | model | 否 | 模型覆盖,格式为 provider/model 或 provider/model#variant。 | | subagent | 否 | true 在后台子会话中运行;false 强制使用当前会话。 | | subtask | 否 | subagent 的已弃用别名。 |
例如,以下 JSON 命令设置了当前所有可选字段:
{
  "commands": {
    "audit": {
      "template": "Audit $ARGUMENTS.",
      "description": "Audit a package",
      "agent": "general",
      "model": "anthropic/claude-sonnet-5-5#high",
      "subagent": true,
    },
  },
}
不要把 template 放进 Markdown frontmatter;正文会提供它。

参数

使用 $ARGUMENTS 获取按原样输入的完整参数字符串。
Create a typed React component named $ARGUMENTS.
对于 /component Account Settings,占位符会被替换为 Account Settings。

位置参数

使用 $1、$2 及更大的数字表示解析后的位置参数。单引号或双引号可将多个词组合起来,引号本身会被去除。
Check $1. Focus on $2.
对于 /check src/auth.ts "error handling",$1 变为 src/auth.ts,$2 变为 error handling。
编号最大的占位符会消费其参数及其后的所有内容。缺失的位置会变为空字符串。
Compare $1 with $2.
对于 /compare api stable branch,OpenCode 会提交 Compare api with stable branch.

回退

当模板中没有 $ARGUMENTS 或位置占位符时,OpenCode 会在空行之后追加非空参数。
Explain this code clearly.
对于 /explain src/cache.ts,提示词变为:
Explain this code clearly.

src/cache.ts

Shell

将 shell 命令包在 ! 与反引号之间,可在提交提示词之前插入其输出。
Review this diff:

!`git diff --stat && git diff`
OpenCode 会在活动项目目录中用配置的 shell 运行每个代码块,并插入其合并后的输出。 参数占位符会先被展开:
Summarize these commits:

!`git log --oneline -$1`
对于 /history 5,shell 会收到 git log --oneline -5。不要把不受信任的参数放进 shell 代码块中。
shell 代码块在 OpenCode 求值命令时运行,位于智能体的工具权限流程之外。只使用来自可信来源的命令。

附件

存储的模板不会展开 @path;它仍是普通的提示词文本。
Review @README.md.
要附加文件,请在调用命令时通过输入框添加。OpenCode 在提交已展开的提示词时会保留这些输入框附件。

执行

命令会在提交持久化的用户提示词之前展开参数与 shell 代码块。默认在当前会话中运行。
---
agent: plan
---

Plan $ARGUMENTS.
运行 /plan migration 会在提交提示词之前把当前会话切换到 plan。
模型选择遵循以下规则:
  1. 命令的 model 覆盖其他所有模型。
  2. 否则,所选命令智能体上配置的模型会覆盖调用时处于活动状态的模型。
  3. 否则,当前会话模型保持活动。
例如,以下命令同时选定智能体和显式模型:
---
agent: plan
model: openai/gpt-6.1-sol#high
---

Design $ARGUMENTS.

后台运行

设置 subagent: true 可让命令在后台子会话中运行。
---
description: Audit changes
agent: general
subagent: true
---

Audit $ARGUMENTS for bugs and missing tests.
父会话保持可用,并保留其智能体与模型。子会话结束后,OpenCode 会将其结果或失败回传给父会话。
| 值 | 行为 | | --- | --- | | true | 始终使用子会话,即使所选智能体为 mode: primary。 | | false | 始终使用当前会话,即使所选智能体为 mode: subagent。 | | 省略 | 仅当所选智能体为 mode: subagent 时才使用子会话。 |
子会话依次使用命令模型、所选智能体的模型、父会话的模型。旧版 subtask 仍被接受;若两个字段同时存在,以 subagent 为准。
subagent: false
subtask: true
本例在当前会话中运行,因为 subagent 优先。

加载

Markdown 与 JSON 命令共享同一注册表。较后的来源会替换较早的同名命令。
~/.config/opencode/commands/review.md   # Lower priority
.opencode/commands/review.md           # Replaces the global command
项目来源优先于全局来源,较近的项目来源优先于祖先目录中的来源。自定义定义同样可以替换较早的内置命令。OpenCode 会自动重新加载命令文件与配置变更。
Review only the staged changes: !`git diff --cached`
保存该文件即可在不重启 OpenCode 的情况下更新 /review。