OpenCode 提供了一个交互式 terminal interface(终端界面),也称为 TUI,用于配合 LLM 在你的项目中工作。
运行 OpenCode 会在当前目录启动 TUI。
opencode
你也可以为指定的工作目录启动:
opencode /path/to/project
进入 TUI 后,你可以直接输入消息向它提问或下达指令。
Give me a quick summary of the codebase.给我一个代码库的快速总结。
File references(文件引用)
你可以在消息中使用 @ 来引用文件。它会在当前工作目录中进行 fuzzy file search(模糊文件搜索)。
示例:
"@packages/functions/src/api/index.ts"How is auth handled in @packages/functions/src/api/index.ts?
对应文件的内容会自动添加到对话上下文中。
Bash commands(Bash 命令)
如果以 ! 开头输入消息,就会执行一个 shell command(命令)。
```bash frame=”none” !ls -la
该命令的输出会作为 tool result(工具结果)自动加入到对话中。---## Commands(命令)在使用 OpenCode TUI 时,你可以输入 `/` 加命令名称来快速执行操作。例如:```bash frame="none"/help
大多数命令还支持 keybind(快捷键),默认使用 ctrl+x 作为 leader key(引导键)。
你可以理解为:先按 ctrl+x,再按对应的快捷键。
更多信息请参考 keybinds。
以下是所有可用的 slash commands(斜杠命令):
connect
为 OpenCode 添加一个 provider。可以从可用 provider 列表中选择,并配置对应的 API keys。
```bash frame=”none” /connect
---### compact压缩当前 session(会话)。*别名(Alias)*:`/summarize````bash frame="none"/compact
Keybind: ctrl+x c
details
切换是否显示 tool execution details(工具执行细节)。
```bash frame=”none” /details
**Keybind:** `ctrl+x d`---### editor打开外部 editor(编辑器)来编写消息。使用的是你在 `EDITOR` environment variable(环境变量)中配置的编辑器。详情参考:[Learn more](#editor-setup)```bash frame="none"/editor
Keybind: ctrl+x e
exit
退出 OpenCode。
别名(Aliases):/quit、/q
```bash frame=”none” /exit
**Keybind:** `ctrl+x q`---### export将当前对话导出为 Markdown 并在默认 editor 中打开。使用的是 `EDITOR` 环境变量指定的编辑器。详情参考:[Learn more](#editor-setup)```bash frame="none"/export
Keybind: ctrl+x x
help
显示帮助对话框。
```bash frame=”none” /help
**Keybind:** `ctrl+x h`---### init创建或更新 `AGENTS.md` 文件。详情参考:[Learn more](/docs/rules)```bash frame="none"/init
Keybind: ctrl+x i
models
列出当前可用的 models(模型)。
```bash frame=”none” /models
**Keybind:** `ctrl+x m`---### new开启一个新的 session。*别名(Alias)*:`/clear````bash frame="none"/new
Keybind: ctrl+x n
redo
重做之前被 /undo 撤销的操作。仅在执行过 /undo 后可用。
内部实现是使用 Git 来管理文件变更,因此你的项目 必须是一个 Git repository(Git 仓库)。
```bash frame=”none” /redo
**Keybind:** `ctrl+x r`---### sessions列出并在不同 session 之间切换。*别名(Aliases)*:`/resume`、`/continue````bash frame="none"/sessions
Keybind: ctrl+x l
share
分享当前 session。 详情参考:Learn more
```bash frame=”none” /share
**Keybind:** `ctrl+x s`---### themes列出可用的 themes(主题)。```bash frame="none"/theme
Keybind: ctrl+x t
undo
撤销最近一次消息操作。会删除最近的用户消息、后续所有响应以及对应的文件变更。
内部同样是使用 Git 来管理文件变更,因此你的项目 必须是一个 Git repository。
```bash frame=”none” /undo
**Keybind:** `ctrl+x u`---### unshare取消分享当前 session。详情参考:[Learn more](/docs/share#un-sharing)```bash frame="none"/unshare
Editor setup(编辑器配置)
/editor 和 /export 命令都会使用 EDITOR environment variable 中配置的编辑器。
Linux / macOS
# nano 或 vim 示例export EDITOR=nanoexport EDITOR=vim# GUI 编辑器,例如 VS Code、Cursor、VSCodium、Windsurf、Zed 等# 需要包含 --wait 参数export EDITOR="code --wait"
要永久生效,请将配置加入你的 shell profile,例如:
~/.bashrc、~/.zshrc 等。
Windows(CMD)
set EDITOR=notepad# GUI 编辑器,例如 VS Code、Cursor、VSCodium、Windsurf、Zed 等# 需要包含 --wait 参数set EDITOR=code --wait
要永久生效,请通过: System Properties → Environment Variables 进行设置。
Windows(PowerShell)
$env:EDITOR = "notepad"# GUI 编辑器,例如 VS Code、Cursor、VSCodium、Windsurf、Zed 等# 需要包含 --wait 参数$env:EDITOR = "code --wait"
要永久生效,请将其加入你的 PowerShell profile。
常见 editor 选项包括:
code—— Visual Studio Codecursor—— Cursorwindsurf—— Windsurfnvim—— Neovim editorvim—— Vim editornano—— Nano editornotepad—— Windows Notepadsubl—— Sublime Text
部分编辑器需要额外的 command-line arguments(命令行参数)才能以 blocking mode(阻塞模式)运行。
--wait 会让编辑器进程在关闭之前一直阻塞。
Configure(配置)
你可以通过 OpenCode 的 config file(配置文件)来自定义 TUI 的行为。
json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"tui": {
"scroll_speed": 3,
"scroll_acceleration": {
"enabled": true
}
}
}
Options(配置项)
scroll_acceleration—— 启用 macOS 风格的 scroll acceleration(滚动加速),实现更平滑、自然的滚动体验。启用后,快速滚动会自动加速,慢速滚动则保持精确控制。 该配置优先级高于scroll_speed,启用后会覆盖它。scroll_speed—— 控制在使用滚动命令时 TUI 的滚动速度(最小值:1),默认值为3。 注意:如果启用了scroll_acceleration.enabled = true,该配置会被忽略。
Customization(个性化定制)
你可以通过 command palette(命令面板,ctrl+x h 或 /help)来自定义 TUI 的各项显示和行为设置。这些设置会在重启后持续生效。
