opencode - 常用命令&自定义命令

opencode - 常用命令&自定义命令

基本用法

bash 复制代码
opencode          # 启动 TUI 界面(默认)
opencode --help   # 查看帮助
opencode --version # 查看版本

TUI 启动

bash 复制代码
opencode [project]               # 启动 TUI,可指定项目目录
opencode -c                      # 继续上次会话
opencode -s <sessionID>          # 继续指定会话
opencode --fork -s <sessionID>   # Fork 指定会话
opencode -m anthropic/claude-sonnet-4-5  # 指定模型启动
opencode --agent build           # 指定 Agent 启动

TUI 内消息技巧

  • @filename --- 模糊搜索并引用文件,文件内容自动加入对话
  • !ls -la --- 以 ! 开头直接运行 shell 命令,输出作为工具结果
  • /command --- 输入 / 执行 slash 命令

TUI Slash 命令

默认 leader 键为 ctrl+x

命令 快捷键 说明
/help ctrl+x h 显示帮助
/new /clear ctrl+x n 开始新会话
/sessions /resume ctrl+x l 列出并切换会话
/models ctrl+x m 列出可用模型
/themes ctrl+x t 列出可用主题
/compact /summarize ctrl+x c 压缩当前会话上下文
/share ctrl+x s 分享当前会话
/unshare 取消分享
/undo ctrl+x u 撤销上条消息及文件变更(需 Git)
/redo ctrl+x r 重做已撤销的消息(需 Git)
/editor ctrl+x e 用外部编辑器撰写消息
/export ctrl+x x 导出对话为 Markdown 并打开编辑器
/details ctrl+x d 切换工具执行详情显示
/thinking 切换模型思考过程显示
/init ctrl+x i 引导创建/更新 AGENTS.md
/connect 添加提供商 API Key
/exit /quit /q ctrl+x q 退出 opencode

TUI 编辑器配置

/editor/export 命令使用 EDITOR 环境变量指定的编辑器:

ini 复制代码
export EDITOR=vim
export EDITOR="code --wait"   # VS Code 需加 --wait
export EDITOR="cursor --wait"

TUI 配置文件(tui.json)

opencode.json 同目录,单独控制界面行为:

kotlin 复制代码
{
  "$schema": "https://opencode.ai/tui.json",
  "theme": "opencode",
  "keybinds": { "leader": "ctrl+x" },
  "scroll_speed": 3,
  "scroll_acceleration": { "enabled": true },
  "diff_style": "auto",   // auto | stacked
  "mouse": true
}

使用 OPENCODE_TUI_CONFIG 环境变量可指定自定义 TUI 配置路径。


run --- 非交互模式

直接传入提示词,适合脚本和自动化:

arduino 复制代码
opencode run "解释 Go 中 context 的用法"
opencode run -m anthropic/claude-sonnet-4-5 "审查这段代码"
opencode run -f main.go "分析这个文件"           # 附加文件
opencode run -c "继续上次的任务"                  # 继续上次会话
opencode run --share "生成一份报告"               # 运行并分享会话

# 附加到已运行的 serve 实例(避免 MCP 冷启动)
opencode serve
opencode run --attach http://localhost:4096 "解释 async/await"
参数 说明
-c / --continue 继续上次会话
-s / --session 指定会话 ID
--fork Fork 会话后继续
-m / --model 指定模型
--agent 指定 Agent
-f / --file 附加文件
--share 分享会话
--attach 附加到运行中的服务
--format 输出格式:default 或 json
--dangerously-skip-permissions 自动批准权限(慎用)

serve / web / attach --- 服务命令

ini 复制代码
opencode serve                              # 启动无界面 HTTP API 服务
opencode serve --port 4096 --hostname 0.0.0.0  # 对外暴露
OPENCODE_SERVER_PASSWORD=xxx opencode serve # 启用认证

opencode web                                # 启动带 Web UI 的服务
opencode web --port 4096 --hostname 0.0.0.0 --mdns

opencode attach http://localhost:4096       # 将 TUI 附加到远程服务

auth --- 认证管理

bash 复制代码
opencode auth login    # 配置 API Key(交互式)
opencode auth list     # 列出已认证的提供商
opencode auth ls       # 同上(简写)
opencode auth logout   # 退出指定提供商

凭证存储在 ~/.local/share/opencode/auth.json


models --- 模型管理

bash 复制代码
opencode models                  # 列出所有可用模型
opencode models anthropic        # 过滤指定提供商
opencode models --refresh        # 刷新模型缓存
opencode models --verbose        # 显示详细信息(含费用)

session --- 会话管理

bash 复制代码
opencode session list             # 列出所有会话
opencode session list -n 10       # 最近 10 条
opencode session list --format json  # JSON 格式输出

opencode export <sessionID>       # 导出会话为 JSON
opencode export                   # 交互式选择会话导出

opencode import session.json      # 从文件导入
opencode import https://opncd.ai/s/abc123  # 从分享链接导入

agent --- Agent 管理

bash 复制代码
opencode agent list               # 列出所有 Agent
opencode agent create             # 交互式创建 Agent

# 非交互式创建
opencode agent create \
  --description "专注于代码审查的助手" \
  --mode primary \
  --permissions bash,read,edit,grep \
  --model anthropic/claude-sonnet-4-5

mcp --- MCP 服务器管理

bash 复制代码
opencode mcp list               # 列出已配置的 MCP 服务器
opencode mcp ls                 # 同上(简写)
opencode mcp add                # 交互式添加 MCP 服务器
opencode mcp auth [name]        # OAuth 认证
opencode mcp auth list          # 列出支持 OAuth 的服务器
opencode mcp logout [name]      # 移除 OAuth 凭证
opencode mcp debug <name>       # 调试 OAuth 连接问题

stats --- 用量统计

css 复制代码
opencode stats                   # 查看 token 用量和费用统计
opencode stats --days 7          # 最近 7 天
opencode stats --models 5        # 显示 Top 5 模型用量
opencode stats --tools 10        # 显示 Top 10 工具用量
opencode stats --project myapp   # 过滤指定项目

github --- GitHub 集成

csharp 复制代码
opencode github install          # 在仓库中安装 GitHub Agent
opencode github run              # 运行 GitHub Agent(通常在 CI 中使用)
opencode github run --event pull_request --token <token>

upgrade / uninstall

bash 复制代码
opencode upgrade                 # 升级到最新版本
opencode upgrade v0.1.48         # 升级到指定版本
opencode upgrade -m brew         # 指定安装方式(curl/npm/pnpm/bun/brew)

opencode uninstall               # 卸载 opencode
opencode uninstall --keep-config # 保留配置文件
opencode uninstall --keep-data   # 保留会话数据
opencode uninstall --dry-run     # 预览将删除的内容
opencode uninstall -f            # 跳过确认直接卸载

自定义命令

自定义命令让你预设提示词模板,在 TUI 中用 /命令名 触发。

创建方式一:Markdown 文件

.md 文件放入以下目录,文件名即命令名:

  • 全局:~/.config/opencode/commands/
  • 项目:.opencode/commands/
yaml 复制代码
---
description: 运行测试并显示覆盖率
agent: build
model: anthropic/claude-sonnet-4-5
---

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

在 TUI 中使用:

bash 复制代码
/test

创建方式二:opencode.json 配置

json 复制代码
{
  "command": {
    "test": {
      "template": "Run the full test suite with coverage report and show any failures.",
      "description": "运行测试",
      "agent": "build",
      "model": "anthropic/claude-sonnet-4-5"
    }
  }
}

命令选项

选项 必填 说明
template 发送给 LLM 的提示词
description TUI 中显示的描述
agent 指定执行的 Agent,默认使用当前 Agent
model 覆盖默认模型
subtask 强制作为子任务执行,不污染主上下文

提示词占位符

传入参数:

yaml 复制代码
---
description: 创建 React 组件
---

Create a new React component named $ARGUMENTS with TypeScript support.
bash 复制代码
/component Button       # $ARGUMENTS → Button

多个参数用位置变量:

javascript 复制代码
Create a file named $1 in the directory $2 with content: $3
lua 复制代码
/create-file config.json src "{}"

注入 shell 命令输出:

yaml 复制代码
---
description: 分析测试覆盖率
---

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

Based on these results, suggest improvements.

引用文件内容:

yaml 复制代码
---
description: 审查组件
---

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

注意: 自定义命令可覆盖内置命令(如 /help/undo 等)。


全局参数

参数 简写 说明
--help -h 显示帮助
--version -v 打印版本号
--print-logs 将日志输出到 stderr
--log-level 日志级别:DEBUG / INFO / WARN / ERROR

常用环境变量

变量 说明
OPENCODE_SERVER_PASSWORD 启用 serve/web 的 HTTP Basic Auth
OPENCODE_SERVER_USERNAME 覆盖认证用户名(默认 opencode)
OPENCODE_CONFIG 自定义配置文件路径
OPENCODE_CONFIG_CONTENT 内联 JSON 配置
OPENCODE_CONFIG_DIR 配置目录路径
OPENCODE_PERMISSION 内联 JSON 权限配置
OPENCODE_AUTO_SHARE 自动分享会话
OPENCODE_DISABLE_AUTOUPDATE 禁用自动更新检查
OPENCODE_DISABLE_AUTOCOMPACT 禁用自动上下文压缩
OPENCODE_DISABLE_MOUSE 禁用 TUI 鼠标支持
相关推荐
CoderCodingNo1 小时前
【CSP】CSP-J 2021真题 | 插入排序 luogu-P7910 (适合GESP四-六级及以上考生练习)
数据结构·算法·排序算法
艺术电影节2 小时前
祝贺电影《撤离》《悼念词》《水草长生》 荣获亚洲艺术电影节提名
算法·推荐算法·电视
MATLAB代码顾问2 小时前
改进鲸鱼优化算法(IWOA)求解柔性作业车间调度问题(FJSP)——附MATLAB代码
开发语言·算法·matlab
量子-Alex2 小时前
【大模型】EvoLM论文LLM训练各个阶段效果
人工智能·算法·机器学习
Hello world.Joey2 小时前
OSTrack
人工智能·算法·目标检测·目标跟踪
WL_Aurora2 小时前
Python 算法基础篇之堆和优先队列
python·算法
努力努力再努力wz2 小时前
【MySQL进阶系列】一文打通事务机制:从锁、Undo Log 到 MVCC 与隔离级别
c语言·数据结构·数据库·c++·mysql·算法·github
薇茗3 小时前
【初阶数据结构】 左右逢源的分支诗律 二叉树1
c语言·数据结构·算法
澈2073 小时前
C++ string全面解析:从入门到精通
数据结构·c++·算法