commands.mdx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ---
  2. title: "Commands"
  3. description: ""
  4. ---
  5. Custom commands turn a named prompt template into a slash command. Type the
  6. command in the TUI, followed by any arguments:
  7. ```text
  8. /review src/auth
  9. ```
  10. ## Configure with Markdown
  11. OpenCode discovers `.md` command files in `commands/` directories:
  12. ```text
  13. ~/.config/opencode/commands/ # Global
  14. .opencode/commands/ # Project
  15. ```
  16. Files may be nested; for example, `.opencode/commands/team/review.md` defines
  17. `/team/review`. Files with other extensions, including `.mdx`, are not
  18. discovered.
  19. ```md title=".opencode/commands/review.md"
  20. ---
  21. description: Review code for correctness and missing tests
  22. agent: plan
  23. model: anthropic/claude-sonnet-4-5#high
  24. ---
  25. Review $ARGUMENTS. Report bugs first, then missing tests.
  26. ```
  27. The file body, with surrounding whitespace removed, is the command template.
  28. JSON and Markdown commands share one registry. Project definitions take
  29. precedence over global definitions, and a later definition can override a
  30. built-in or earlier command with the same name. Changes are reloaded
  31. automatically.
  32. Run it with:
  33. ```text
  34. /review src/auth
  35. ```
  36. ## Configure with JSON
  37. Add commands under the `commands` key in any OpenCode JSON or JSONC
  38. [configuration file](/config). Each entry's key is the command name and
  39. `template` is required.
  40. ```jsonc title="opencode.jsonc"
  41. {
  42. "$schema": "https://opencode.ai/config.json",
  43. "commands": {
  44. "review": {
  45. "description": "Review code for correctness and missing tests",
  46. "template": "Review $ARGUMENTS. Report bugs first, then missing tests.",
  47. "agent": "plan",
  48. "model": "anthropic/claude-sonnet-4-5#high"
  49. }
  50. }
  51. }
  52. ```
  53. ## Fields
  54. | Field | Required | Behavior |
  55. | --- | --- | --- |
  56. | `template` | JSON only | Prompt template. In a Markdown command, the file body supplies it. |
  57. | `description` | No | Text shown with the command in autocomplete. |
  58. | `agent` | No | Agent selected before the prompt runs. |
  59. | `model` | No | Model override in `provider/model` or `provider/model#variant` format. |
  60. | `subtask` | No | Accepted as a boolean, but currently has no execution effect in V2. |
  61. The four optional fields can be used in JSON or YAML frontmatter. Do not put
  62. `template` in frontmatter because the Markdown body always supplies it.
  63. ## Arguments
  64. Use `$ARGUMENTS` for the complete argument string:
  65. ```md title=".opencode/commands/component.md"
  66. ---
  67. description: Create a component
  68. ---
  69. Create a typed React component named $ARGUMENTS.
  70. ```
  71. ```text
  72. /component Button
  73. ```
  74. Use `$1`, `$2`, and higher numbers for parsed positional arguments. Single and
  75. double quotes group text containing spaces and are removed during parsing.
  76. ```md title=".opencode/commands/check.md"
  77. ---
  78. description: Check one area with a specific focus
  79. ---
  80. Check $1. Focus on $2.
  81. ```
  82. ```text
  83. /check src/auth "error handling and missing tests"
  84. ```
  85. The highest-numbered positional placeholder present in the template consumes
  86. that argument and all remaining arguments. For example, if a template contains
  87. only `$1`, then `$1` receives the full parsed argument list. Missing positions
  88. become empty strings.
  89. If a template contains neither positional placeholders nor `$ARGUMENTS`,
  90. OpenCode appends non-empty arguments to the template after a blank line.
  91. ## Shell interpolation
  92. Wrap a shell command in `!` followed by backticks to insert its output before
  93. the prompt is submitted:
  94. ```md title=".opencode/commands/review-diff.md"
  95. ---
  96. description: Review the current diff
  97. ---
  98. Review this diff:
  99. !`git diff --stat && git diff`
  100. ```
  101. OpenCode runs each interpolation with the configured shell in the active
  102. project location and inserts its combined output into the template. Argument
  103. interpolation happens first, so avoid placing untrusted arguments inside shell
  104. interpolations.
  105. <Warning>
  106. Shell interpolations run when the command is evaluated, outside the agent's
  107. tool permission flow. Only use commands from sources you trust.
  108. </Warning>
  109. No other template interpolation is performed. In particular, an `@path`
  110. written into a stored template remains ordinary prompt text; V2 does not
  111. automatically attach that file.
  112. ## Agent, model, and execution
  113. Running a command evaluates its arguments and shell blocks, submits the result
  114. as a durable user prompt in the current session, and schedules normal model
  115. execution.
  116. If `agent` is set, it overrides the agent selected when the command was
  117. invoked and becomes the session's active agent. If `model` is set, it overrides
  118. the model. Otherwise, a model configured on the command's agent takes
  119. precedence over the model selected at invocation.
  120. Although `subtask` is accepted in JSON and frontmatter, V2 currently ignores
  121. it: commands run in the current session and do not create a child session.
  122. Selecting an agent whose mode is `subagent` also does not turn the command into
  123. a subtask.