beginner30 minLesson 4 of 5

Understanding the CLI Interface

Master the OpenCode command-line interface. Learn all available commands, flags, options, and how to navigate the interactive session effectively.

Understanding the CLI Interface

Command Structure

OpenCode follows a standard CLI structure:

bash
opencode [command] [options] [arguments]

Main Commands

CommandDescriptionExample
opencodeStart interactive sessionopencode
opencode chatStart chat sessionopencode chat
opencode runRun a single promptopencode run "fix the bug"
opencode configManage configurationopencode config list
opencode providersList LLM providersopencode providers
opencode skillsList available skillsopencode skills
opencode versionShow versionopencode --version

Interactive Session Commands

Once in an interactive session, use these commands:

Navigation

CommandDescription
/helpShow all available commands
/quit or /exitExit OpenCode
/clearClear current conversation
/historyView conversation history
/save [name]Save session to file
/load [name]Load saved session

Session Management

CommandDescription
/model [name]Switch LLM model
/agent [name]Switch agent
/skill [name]Load a specific skill
/debugToggle debug mode
/verboseToggle verbose output

Command-Line Options

Global Options

OptionDescription
-h, --helpShow help
-v, --versionShow version
-c, --config [path]Specify config file
-p, --provider [name]Set LLM provider
-m, --model [name]Set model
--debugEnable debug mode
--verboseEnable verbose output

Run Command Options

OptionDescription
-f, --file [path]Read input from file
-o, --output [path]Write output to file
--no-colorDisable colored output
--timeout [ms]Set timeout in milliseconds

Example Usage Patterns

Single Prompt Execution

bash
opencode run "Explain what this function does"

Read from File

bash
opencode run -f question.txt

Write to File

bash
opencode run -o answer.md "Write a README for this project"

Debug Mode

bash
opencode --debug

Debug output shows:

  • API requests and responses
  • Tool invocations
  • Permission checks
  • Agent routing decisions

Tab Completion

OpenCode supports tab completion for:

  • Commands (/he/help)
  • File paths (/read src/ma/read src/main.py)
  • Model names (/model gpt/model gpt-4o)

Keyboard Shortcuts

ShortcutAction
EnterSend message
Ctrl+CCancel current operation
Ctrl+DExit session
Ctrl+LClear screen
Up ArrowPrevious command
Down ArrowNext command
TabAuto-complete

Environment Variables

VariableDescription
OPENCODE_CONFIGPath to config file
OPENCODE_PROVIDERDefault LLM provider
OPENCODE_MODELDefault model
OPENCODE_DEBUGEnable debug mode
OPENAI_API_KEYOpenAI API key
ANTHROPIC_API_KEYAnthropic API key
GOOGLE_API_KEYGoogle API key

Practice Questions

Practice Question

What command runs OpenCode with a single prompt without entering interactive mode?

Practice Question

Which option enables debug mode from the command line?

Practice Question

How do you switch models during an interactive session?

Practice Question

Which keyboard shortcut cancels the current operation?

Practice Question

What does the -f flag do in the run command?


Success

Key Takeaways

  • OpenCode supports both interactive and single-prompt execution modes
  • Use /help in interactive mode to see all available commands
  • The --debug flag shows detailed information about AI operations
  • Tab completion works for commands, file paths, and model names
  • Environment variables can configure defaults for provider and model
  • Keyboard shortcuts provide quick access to common actions
  • The -f flag allows reading prompts from files for batch processing
Progress80%