Skip to content

OpenCode 自定义命令让你把常用 prompt 封装成 /命令名 形式,在 TUI 中快速调用。支持 JSON(在 opencode.json 中)和 Markdown(.opencode/commands/*.md)两种定义方式。命令模板支持 $ARGUMENTS 传参、!shell 命令`` 注入脚本输出、@文件 引用文件内容,适合封装测试、代码审查、组件创建等重复任务。

OpenCode 自定义命令

自定义命令让你把常用 prompt 封装成 /命令名 的形式,在 TUI 中一键运行:

/test
/review
/component Button

这些是你自己定义的命令,OpenCode 有一组内置命令(如 /init/undo/help),自定义命令独立于内置命令之外。如果同名则覆盖内置命令。


创建命令

有两种方式:JSON 配置和 Markdown 文件。

方式一:JSON 配置

opencode.jsoncommand 字段中定义:

jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "command": {
    "test": {
      "template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes.",
      "description": "Run tests with coverage",
      "agent": "build",
      "model": "anthropic/claude-3-5-sonnet-20241022"
    },
    "component": {
      "template": "Create a new React component named $ARGUMENTS with TypeScript support.\nInclude proper typing and basic structure.",
      "description": "Create a new component"
    }
  }
}

方式二:Markdown 文件(推荐)

在以下目录中创建 Markdown 文件:

  • 全局:~/.config/opencode/commands/
  • 项目级:.opencode/commands/

文件名即命令名(不含 .md 后缀)。

markdown
---
description: Run tests with coverage
agent: build
model: anthropic/claude-3-5-sonnet-20241022
---

Run the full test suite with coverage report and show any failures.
Focus on the failing tests and suggest fixes.

在 TUI 中运行:

/test

命令模板语法

传入参数($ARGUMENTS)

$ARGUMENTS 接收命令后面跟的所有参数:

markdown
---
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…:

markdown
---
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\" }"

注入 Shell 输出(!command

!command 语法在模板中注入 shell 命令的输出:

markdown
---
description: Analyze test coverage
---

Here are the current test results:
!`npm test`

Based on these results, suggest improvements to increase coverage.
markdown
---
description: Review recent changes
---

Recent git commits:
!`git log --oneline -10`

Review these changes and suggest any improvements.

Shell 命令在项目根目录执行,输出结果作为 prompt 的一部分。

引用文件内容(@文件)

@ 加路径在模板中引用文件内容:

markdown
---
description: Review component
---

Review the component in @src/components/Button.tsx.
Check for performance issues and suggest improvements.

配置选项

template(必填)

发送给 AI 的 prompt 模板。

description

命令的简短描述,显示在 TUI 的命令选择列表中。

agent

指定哪个 agent 执行此命令:

json
{
  "command": {
    "review": {
      "agent": "plan"
    }
  }
}

如果指定的是 subagent,命令默认会作为 subagent 调用触发。

subtask

强制命令以 subagent 方式运行,不污染主 context(即使 agent 的 mode 设为 primary):

json
{
  "command": {
    "analyze": {
      "subtask": true
    }
  }
}

model

覆盖此命令使用的默认模型:

json
{
  "command": {
    "analyze": {
      "model": "anthropic/claude-haiku-4-5"
    }
  }
}

实用命令示例

快速代码审查

markdown
---
description: Review current changes for issues
agent: plan
subtask: true
---

Review the recent changes in this project:
!`git diff HEAD~1`

Check for:
- Potential bugs or edge cases
- Performance issues
- Security concerns
- Code clarity and maintainability

Summarize issues found.

生成测试

markdown
---
description: Generate tests for a file
---

Generate comprehensive unit tests for @$ARGUMENTS.
Cover edge cases, error handling, and main functionality.
Use the same testing framework as existing tests in the project.

运行:/gen-test src/utils/format.ts


常见问题

Q: 自定义命令和 skills 有什么区别?

A: 命令是带有固定 prompt 模板的快捷操作,每次运行都会直接执行 prompt。Skills 是 AI 可以按需加载的指令文件,更像是给 AI 的参考手册,而不是直接触发的动作。

Q: 如何查看已定义的自定义命令列表?

A: 在 TUI 中输入 /,弹出的自动补全列表会显示所有可用命令(内置命令和自定义命令)。

Q: 项目级和全局命令同名时哪个生效?

A: 项目级(.opencode/commands/)的命令会覆盖全局(~/.config/opencode/commands/)的同名命令。