OpenCode Custom Commands
Custom commands allow you to encapsulate frequently used prompts into a short command, executed with one click in TUI via /命令名称 avoiding repeated entry of the same prompt.
For example, encapsulate the complex prompt "run tests and analyze failure reasons" into /test, so each time you only need to type two characters to trigger it.
Custom commands are /init、/undo、/redo、/share、/help extensions beyond built-in commands such as these.
If a custom command you define has the same name as a built-in command,the custom command will override the built-in command. Please avoid using
init、undo、redo、share、helpas a custom command name unless you explicitly want to replace the built-in behavior.
Two definition methods
Custom commands support two definition methods with identical effects; you can choose according to personal preference:
| Method | file | Applicable scenarios |
|---|---|---|
| JSON configuration | opencode.json 's command Field |
Few commands, want to manage all configurations centrally |
| Markdown file | commands/ under the directory .md file |
Long command content, want to maintain each command separately |
Method 1: JSON configuration
in opencode.json 's command defined in the field, where the key name is the command name:
Example
{
"$schema"
:
"https://opencode.ai/config.json"
,
"command"
:
{
"test"
:
{
// Command name, invoked in TUI via /test
"template"
:
"Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes."
,
// template: prompt content sent to the LLM (required)
"description"
:
"Run tests with coverage"
,
// description: description shown in the TUI command list (optional)
"agent"
:
"build"
,
// agent: specify the agent that executes this command (optional)
"model"
:
"anthropic/claude-3-5-sonnet-20241022"
// model: override the model used by this command (optional)
}
}
}
After configuration, enter in the TUI input box /test and press Enter to execute the command.
Method 2: Markdown files
in commands/ directory, creating .md file to define the command,the file name is the command name, and the file content is divided into two parts:
- Frontmatter(
---wrapped YAML section): defines the command's properties - Body(content below the Frontmatter): becomes the prompt template sent to the LLM
Markdown files support two storage locations:
- Project-level:
.opencode/commands/命令名.md(valid only for the current project) - Global:
~/.config/opencode/commands/命令名.md(valid for all projects)
---
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.
The file name is test.md, so the command name is test, invoked in TUI via /test invoked.
Prompt template syntax
The command's prompt (template field or Markdown body) supports the following special syntax, allowing the prompt to dynamically respond to arguments, inject command output in real time, or reference file contents.
1. Passing arguments ($ARGUMENTS)
Use $ARGUMENTS The placeholder receives arguments passed at command invocation, inserted as a whole string:
---
description: Create a new React component
---
Create a new React component named $ARGUMENTS with TypeScript support.
Include proper typing and basic structure.
Append arguments after the command name when invoking:
/component Button
$ARGUMENTS will be replaced with Button, and the actually executed prompt becomes:
Create a new React component named Button with TypeScript support.
Include proper typing and basic structure.
2. Positional arguments ($1, $2, $3…)
When multiple independent arguments need to be passed, you can use positional arguments $1、$2、$3 to reference each argument separately, separated by spaces, with arguments containing spaces wrapped in quotes:
---
description: Create a new file with content
---
Create a file named $1 in the directory $2
with the following content: $3
Invocation example (arguments with spaces wrapped in quotes):
/create-file config.json src "{ \"key\": \"value\" }"
Replacement results of each placeholder:
| Placeholder | with |
|---|---|
$1 |
config.json |
$2 |
src |
$3 |
{ "key": "value" } |
3. Inject Shell command output (!command)
Use !Shell command`` syntax can run a Shell command at command execution time and insert its output directly into the prompt. The Shell command runs inProject root directoryThis is very useful when you need the LLM to analyze real-time data (such as test results, git logs, etc.):
---
description: Analyze test coverage
---
Here are the current test results:
!`npm test`
Based on these results, suggest improvements to increase coverage.
---
description: Review recent git changes
---
Recent git commits:
!`git log --oneline -10`
Review these changes and suggest any improvements or potential issues.
When executing the command,!git log --oneline -10`` will be replaced with the actual git log output, then sent to the LLM together with the prompt.
4. Reference file contents (@file path)
Use @文件路径 syntax can automatically include the contents of a specified file in the prompt, without manual copy-paste:
---
description: Review a component for performance issues
---
Review the component in @src/components/Button.tsx.
Check for performance issues and suggest improvements.
When executing the command,@src/components/Button.tsx will be replaced with the complete contents of the file, and the LLM can directly analyze the file's code.
Configuration options in detail
The following are all configuration options supported by custom commands; written in the frontmatter in Markdown files, and under the command object in JSON configuration:
| Option | Required | Type | Description |
|---|---|---|---|
template |
Required (JSON method) | String | Prompt content sent to the LLM. In the Markdown method, the body is the template, no separate declaration needed |
description |
Optional | String | A brief description of the command, shown in / the TUI command list, helping to quickly identify the command's purpose |
agent |
Optional | String | Specify the name of the agent that executes this command. If not specified, the currently selected agent is used. If a subagent is specified, the command triggers a subagent invocation by default |
subtask |
Optional | Boolean | Set to true can force the command to run as a subagent, avoiding pollution of the main session context. Set to false can disable subagent invocation behavior |
model |
Optional | String | Override the model used by this command, in the format provider/model-id, such as anthropic/claude-3-5-sonnet-20241022 |
The relationship between agent and subtask
agent and subtask jointly control how the command is executed:
- If
agentspecifies a subagent (mode is subagent), the commandtriggers a subagent invocation by default, executed in an independent context without affecting the main session - If you don't want this behavior, set
subtaskSet tofalsecan force execution in the main session - If you set
subtaskSet totrue, regardless of the agent'smodesetting, the command always runs as a subagent, suitable for scenarios requiring complete context isolation
Example
{
"$schema"
:
"https://opencode.ai/config.json"
,
"command"
:
{
"analyze"
:
{
"template"
:
"Analyze the codebase for potential security vulnerabilities."
,
"description"
:
"Security analysis (runs independently, does not pollute the main session)"
,
"subtask"
:
true
,
// Force running as a subagent; analysis results do not occupy the main session context
"model"
:
"anthropic/claude-3-5-sonnet-20241022"
}
,
"review"
:
{
"template"
:
"Review the current code changes."
,
"description"
:
"Code review (runs in the current agent)"
,
"agent"
:
"plan"
,
// Execute using the plan agent
"subtask"
:
false
// Disable subagent invocation, run in the main session
}
}
}
Combined usage examples
Example 1: multi-argument code generation command
---
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.
Example 2: code review command combining Shell output and file references
---
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).
Example 3: scheduled data snapshot command (JSON method)
{
"$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
}
}
}