GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — OpenCode 自定义命令 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
OpenCode 教程
OpenCode 教程 OpenCode 简介 OpenCode 安装 OpenCode API 设置 OpenCode DeepSeek OpenCode 配置文件 OpenCode 第一次使用 OpenCode VSCode OpenCode TUI OpenCode CLI 使用 OpenCode Web 使用 OpenCode GitHub OpenCode GitLab OpenCode 工具 OpenCode 规则 OpenCode 代理 OpenCode 主题 OpenCode 快捷键 OpenCode 自定义命令 OpenCode 格式化工具 OpenCode 权限配置 OpenCode LSP 服务器配置 OpenCode MCP 服务器配置 OpenCode Skills OpenCode skills 使用 OpenCode 自定义工具 OpenCode oh-my-openagent OpenCode 生态系统 OpenCode 配置文件
OpenCode 快捷键
OpenCode 格式化工具

OpenCode 自定义命令

自定义命令允许你将常用的提示词封装成一个短命令,在 TUI 中通过 /命令名称 的方式一键执行,避免重复输入相同的提示词。

例如,将"运行测试并分析失败原因"这个复杂提示词封装为 /test,每次只需输入两个字符即可触发。

自定义命令是 /init、/undo、/redo、/share、/help 等内置命令之外的扩展。

如果你定义的自定义命令与内置命令同名,自定义命令会覆盖内置命令。请避免使用 init、undo、redo、share、help 作为自定义命令名称,除非你明确需要替换内置行为。


两种定义方式

自定义命令支持两种定义方式,效果完全相同,可以按照个人习惯选择:

方式 文件 适用场景
JSON 配置 opencode.json 的 command 字段 命令较少,希望集中管理所有配置
Markdown 文件 commands/ 目录下的 .md 文件 命令内容较长,希望每个命令单独维护

方式一:JSON 配置

在 opencode.json 的 command 字段中定义,键名即为命令名称:

实例

{
  "$schema": "https://opencode.ai/config.json",
  "command": {
    "test": {                           // 命令名称,在 TUI 中通过 /test 调用
      "template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes.",
                                        // template:发送给 LLM 的提示词内容(必填)
      "description": "Run tests with coverage",
                                        // description:在 TUI 命令列表中显示的说明(可选)
      "agent": "build",                 // agent:指定执行此命令的代理(可选)
      "model": "anthropic/claude-3-5-sonnet-20241022"
                                        // model:覆盖此命令使用的模型(可选)
    }
  }
}

配置完成后,在 TUI 输入框中输入 /test 并回车即可执行该命令。


方式二:Markdown 文件

在 commands/ 目录下创建 .md 文件来定义命令,文件名即为命令名,文件内容分为两部分:

  • Frontmatter(--- 包裹的 YAML 部分):定义命令的属性
  • 正文(Frontmatter 下方的内容):成为发送给 LLM 的提示词模板

Markdown 文件支持两个存放位置:

  • 项目级:.opencode/commands/命令名.md(仅对当前项目有效)
  • 全局:~/.config/opencode/commands/命令名.md(对所有项目有效)
<!-- 文件路径:.opencode/commands/test.md -->
---
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.

该文件名为 test.md,因此命令名为 test,在 TUI 中通过 /test 调用。


提示词模板语法

命令的提示词(template 字段或 Markdown 正文)支持以下特殊语法,可以让提示词动态响应参数、实时注入命令输出或引用文件内容。

1、传入参数($ARGUMENTS)

使用 $ARGUMENTS 占位符接收命令调用时传入的参数,整体作为一个字符串插入:

<!-- 文件路径:.opencode/commands/component.md -->
---
description: Create a new React component
---

Create a new React component named $ARGUMENTS with TypeScript support.
Include proper typing and basic structure.

调用时在命令名后附加参数:

/component Button

$ARGUMENTS 会被替换为 Button,实际执行的提示词变为:

Create a new React component named Button with TypeScript support.
Include proper typing and basic structure.

2、位置参数($1、$2、$3…)

当需要传入多个独立参数时,可以使用位置参数 $1、$2、$3 分别引用每个参数,空格分隔,含空格的参数用引号包裹:

<!-- 文件路径:.opencode/commands/create-file.md -->
---
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" }

3、注入 Shell 命令输出(!`command`)

使用 !`shell命令` 语法可以在命令执行时运行一段 Shell 命令,并将其输出结果直接插入提示词中。Shell 命令在项目根目录下运行。这在需要让 LLM 分析实时数据(如测试结果、git 日志等)时非常有用:

<!-- 文件路径:.opencode/commands/analyze-coverage.md -->
---
description: Analyze test coverage
---

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

Based on these results, suggest improvements to increase coverage.
<!-- 文件路径:.opencode/commands/review-changes.md -->
---
description: Review recent git changes
---

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

Review these changes and suggest any improvements or potential issues.

执行命令时,!`git log --oneline -10` 会被替换为实际的 git 日志输出,然后连同提示词一起发送给 LLM。

4、引用文件内容(@文件路径)

使用 @文件路径 语法可以将指定文件的内容自动包含在提示词中,无需手动复制粘贴:

<!-- 文件路径:.opencode/commands/review-component.md -->
---
description: Review a component for performance issues
---

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

执行命令时,@src/components/Button.tsx 会被替换为该文件的完整内容,LLM 可以直接分析文件代码。


配置选项详解

以下是自定义命令支持的所有配置选项,Markdown 文件中写在 frontmatter 里,JSON 配置中写在命令对象下:

选项 是否必填 类型 说明
template 必填(JSON 方式) 字符串 发送给 LLM 的提示词内容。Markdown 方式中正文即为 template,无需单独声明
description 可选 字符串 命令的简要说明,输入 / 时在 TUI 命令列表中显示,帮助快速识别命令用途
agent 可选 字符串 指定执行此命令的代理名称。未指定则使用当前选中的代理。若指定的是子代理,命令默认触发子代理调用
subtask 可选 布尔值 设为 true 可强制命令以子代理方式运行,避免污染主会话上下文。设为 false 可禁止子代理调用行为
model 可选 字符串 覆盖此命令使用的模型,格式为 provider/model-id,如 anthropic/claude-3-5-sonnet-20241022

agent 与 subtask 的关系

agent 和 subtask 共同控制命令的执行方式:

  • 若 agent 指定的是一个子代理(mode 为 subagent),命令默认触发子代理调用,在独立上下文中执行,不影响主会话
  • 若不希望此行为,将 subtask 设为 false 可强制在主会话中执行
  • 若将 subtask 设为 true,无论代理的 mode 设置如何,命令始终以子代理方式运行,适合需要完全隔离上下文的场景

实例

{
  "$schema": "https://opencode.ai/config.json",
  "command": {
    "analyze": {
      "template": "Analyze the codebase for potential security vulnerabilities.",
      "description": "Security analysis(独立运行,不污染主会话)",
      "subtask": true,            // 强制以子代理方式运行,分析结果不占用主会话上下文
      "model": "anthropic/claude-3-5-sonnet-20241022"
    },
    "review": {
      "template": "Review the current code changes.",
      "description": "Code review(在当前代理中运行)",
      "agent": "plan",            // 使用 plan 代理执行
      "subtask": false            // 禁止子代理调用,在主会话中运行
    }
  }
}

综合使用示例

示例一:多参数代码生成命令

<!-- 文件路径:.opencode/commands/gen-api.md -->
---
description: Generate a REST API endpoint
agent: build
---

Generate a $1 REST API endpoint for the resource "$2".
Place the file in the $3 directory.
Include input validation, error handling, and JSDoc comments.

<!-- 调用示例:/gen-api POST user src/api -->
<!-- $1 = POST,$2 = user,$3 = src/api -->

示例二:结合 Shell 输出和文件引用的代码审查命令

<!-- 文件路径:.opencode/commands/pr-review.md -->
---
description: Review changes in current branch against main
agent: plan
subtask: true
---

Please review the following changes for code quality, potential bugs, and security issues.

## Recent commits
!`git log main..HEAD --oneline`

## Changed files
!`git diff main --name-only`

## Diff content
!`git diff main`

## Project coding guidelines
@.opencode/AGENTS.md

Provide structured feedback with severity levels (critical / warning / suggestion).

示例三:定时数据快照命令(JSON 方式)

{
  "$schema": "https://opencode.ai/config.json",
  "command": {
    "healthcheck": {
      "description": "Check project health status",
      "template": "Analyze the project health based on the following data:\n\nDependency vulnerabilities:\n!`npm audit --json`\n\nOutdated packages:\n!`npm outdated`\n\nTest results:\n!`npm test -- --passWithNoTests 2>&1 | tail -20`\n\nSummarize the issues by severity and suggest a prioritized action plan.",
      "model": "anthropic/claude-3-5-sonnet-20241022",
      "subtask": true
    }
  }
}
AI 思考中...
OpenCode 快捷键
OpenCode 格式化工具

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.