Root command
Description
OpenCode is a powerful terminal-based AI assistant that helps with software development tasks. It provides an interactive chat interface with AI capabilities, code analysis, and LSP integration to assist developers in writing, debugging, and understanding code directly from the terminal.Examples
Interactive mode
When launched without the-p flag, OpenCode starts in interactive mode with a full terminal user interface.
Features
- Interactive chat: Chat with AI models in real-time
- Session management: Create, switch, and manage multiple conversation sessions
- Tool integration: AI can execute commands, search files, and modify code
- File tracking: Visualize file changes during sessions
- External editor: Open your preferred editor for composing messages
- LSP integration: Language server protocol support for code intelligence
- Permission system: Control what actions the AI can perform
Keyboard shortcuts
See the keyboard shortcuts documentation for a complete list of available shortcuts in interactive mode.Non-interactive mode
When you provide a prompt with the-p flag, OpenCode runs in non-interactive mode.
Behavior
- Processes the prompt immediately
- Prints the AI response to standard output
- Exits after completion
- Auto-approves all permissions for the session
- No TUI is launched
Use cases
- Scripting: Integrate OpenCode into shell scripts and automation
- Quick queries: Get fast answers without launching the full TUI
- CI/CD pipelines: Use AI assistance in automated workflows
- Testing: Automate testing of prompts and responses
Output formats
Non-interactive mode supports multiple output formats:| Format | Description |
|---|---|
text | Plain text output (default) |
json | Response wrapped in a JSON object |
-f or --output-format flag to specify the desired format.
Spinner display
By default, a spinner animation displays while the AI processes your query. Use the-q or --quiet flag to disable the spinner, which is particularly useful when:
- Running OpenCode from scripts
- Piping output to other commands
- Using in automated workflows
- Logging output to files
Built-in commands
OpenCode includes several built-in commands accessible viaCtrl+K in interactive mode:
Initialize project
Initialize project
Creates or updates the OpenCode.md memory file with project-specific information. This command helps the AI understand your project structure and context.
Compact session
Compact session
Manually triggers summarization of the current session, creating a new session with the summary. Useful when you want to reduce context size while preserving important information.
Custom commands
OpenCode supports user-defined custom commands stored as Markdown files. These allow you to create reusable prompts for common tasks.Command locations
User commands (prefixed withuser:):
$XDG_CONFIG_HOME/opencode/commands/(typically~/.config/opencode/commands/)$HOME/.opencode/commands/
project:):
<PROJECT_DIR>/.opencode/commands/
Creating custom commands
- Create a
.mdfile in one of the command directories - Add your prompt content to the file
- Use named arguments with
$NAMEsyntax for placeholders - Access via
Ctrl+Kin interactive mode
Named arguments
Custom commands support named arguments using the format$NAME where NAME consists of uppercase letters, numbers, and underscores, and must start with a letter.
Organizing commands
Organize commands in subdirectories for better structure:user:git:commituser:git:reviewuser:testing:unit
Exit codes
| Code | Description |
|---|---|
| 0 | Success |
| 1 | Error occurred |