commands.mdx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. ---
  2. title: Commands
  3. description: Create custom commands for repetitive tasks.
  4. ---
  5. Custom commands let you specify a prompt you want to run when that command is executed in the TUI.
  6. ```bash frame="none"
  7. /my-command
  8. ```
  9. Custom commands are in addition to the built-in commands like `/init`, `/undo`, `/redo`, `/share`, `/help`. [Learn more](/docs/tui#commands).
  10. ---
  11. ## Create command files
  12. Create markdown files in the `command/` directory to define custom commands.
  13. Create `.opencode/command/test.md`:
  14. ```md title=".opencode/command/test.md"
  15. ---
  16. description: Run tests with coverage
  17. agent: build
  18. model: anthropic/claude-3-5-sonnet-20241022
  19. ---
  20. Run the full test suite with coverage report and show any failures.
  21. Focus on the failing tests and suggest fixes.
  22. ```
  23. The frontmatter defines command properties. The content becomes the template.
  24. Use the command by typing `/` followed by the command name.
  25. ```bash frame="none"
  26. "/test"
  27. ```
  28. ---
  29. ## Configure
  30. You can add custom commands through the OpenCode config or by creating markdown files in the `command/` directory.
  31. ---
  32. ### JSON
  33. Use the `command` option in your OpenCode [config](/docs/config):
  34. ```json title="opencode.jsonc" {4-12}
  35. {
  36. "$schema": "https://opencode.ai/config.json",
  37. "command": {
  38. // This becomes the name of the command
  39. "test": {
  40. // This is the prompt that will be sent to the LLM
  41. "template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes.",
  42. // This is show as the description in the TUI
  43. "description": "Run tests with coverage",
  44. "agent": "build",
  45. "model": "anthropic/claude-3-5-sonnet-20241022"
  46. }
  47. }
  48. }
  49. ```
  50. Now you can run this command in the TUI:
  51. ```bash frame="none"
  52. /test
  53. ```
  54. ---
  55. ### Markdown
  56. You can also define commands using markdown files. Place them in:
  57. - Global: `~/.config/opencode/command/`
  58. - Per-project: `.opencode/command/`
  59. ```markdown title="~/.config/opencode/command/test.md"
  60. ---
  61. description: Run tests with coverage
  62. agent: build
  63. model: anthropic/claude-3-5-sonnet-20241022
  64. ---
  65. Run the full test suite with coverage report and show any failures.
  66. Focus on the failing tests and suggest fixes.
  67. ```
  68. The markdown file name becomes the command name. For example, `test.md` lets
  69. you run:
  70. ```bash frame="none"
  71. /test
  72. ```
  73. ---
  74. ## Prompt config
  75. The prompts for the custom commands support several special placeholders and syntax.
  76. ---
  77. ### Arguments
  78. Pass arguments to commands using the `$ARGUMENTS` placeholder.
  79. ```md title=".opencode/command/component.md"
  80. ---
  81. description: Create a new component
  82. ---
  83. Create a new React component named $ARGUMENTS with TypeScript support.
  84. Include proper typing and basic structure.
  85. ```
  86. Run the command with arguments:
  87. ```bash frame="none"
  88. /component Button
  89. ```
  90. And `$ARGUMENTS` will be replaced with `Button`.
  91. You can also access individual arguments using positional parameters:
  92. - `$1` - First argument
  93. - `$2` - Second argument
  94. - `$3` - Third argument
  95. - And so on...
  96. For example:
  97. ```md title=".opencode/command/create-file.md"
  98. ---
  99. description: Create a new file with content
  100. ---
  101. Create a file named $1 in the directory $2
  102. with the following content: $3
  103. ```
  104. Run the command:
  105. ```bash frame="none"
  106. /create-file config.json src "{ \"key\": \"value\" }"
  107. ```
  108. This replaces:
  109. - `$1` with `config.json`
  110. - `$2` with `src`
  111. - `$3` with `{ "key": "value" }`
  112. ---
  113. ### Shell output
  114. Use _!`command`_ to inject [bash command](/docs/tui#bash-commands) output into your prompt.
  115. For example, to create a custom command that analyzes test coverage:
  116. ```md title=".opencode/command/analyze-coverage.md"
  117. ---
  118. description: Analyze test coverage
  119. ---
  120. Here are the current test results:
  121. !`npm test`
  122. Based on these results, suggest improvements to increase coverage.
  123. ```
  124. Or to review recent changes:
  125. ```md title=".opencode/command/review-changes.md"
  126. ---
  127. description: Review recent changes
  128. ---
  129. Recent git commits:
  130. !`git log --oneline -10`
  131. Review these changes and suggest any improvements.
  132. ```
  133. Commands run in your project's root directory and their output becomes part of the prompt.
  134. ---
  135. ### File references
  136. Include files in your command using `@` followed by the filename.
  137. ```md title=".opencode/command/review-component.md"
  138. ---
  139. description: Review component
  140. ---
  141. Review the component in @src/components/Button.tsx.
  142. Check for performance issues and suggest improvements.
  143. ```
  144. The file content gets included in the prompt automatically.
  145. ---
  146. ## Options
  147. Let's look at the configuration options in detail.
  148. ---
  149. ### Template
  150. The `template` option defines the prompt that will be sent to the LLM when the command is executed.
  151. ```json title="opencode.json"
  152. {
  153. "command": {
  154. "test": {
  155. "template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes."
  156. }
  157. }
  158. }
  159. ```
  160. This is a **required** config option.
  161. ---
  162. ### Description
  163. Use the `description` option to provide a brief description of what the command does.
  164. ```json title="opencode.json"
  165. {
  166. "command": {
  167. "test": {
  168. "description": "Run tests with coverage"
  169. }
  170. }
  171. }
  172. ```
  173. This is shown as the description in the TUI when you type in the command.
  174. ---
  175. ### Agent
  176. Use the `agent` config to optionally specify which [agent](/docs/agents) should execute this command.
  177. If this is a [subagent](/docs/agents/#subagents) the command will trigger a subagent invocation by default.
  178. To disable this behavior, set `subtask` to `false`.
  179. ```json title="opencode.json"
  180. {
  181. "command": {
  182. "review": {
  183. "agent": "plan"
  184. }
  185. }
  186. }
  187. ```
  188. This is an **optional** config option. If not specified, defaults to your current agent.
  189. ---
  190. ### Subtask
  191. Use the `subtask` boolean to force the command to trigger a [subagent](/docs/agents/#subagents) invocation.
  192. This useful if you want the command to not pollute your primary context and will **force** the agent to act as a subagent,
  193. even if `mode` is set to `primary` on the [agent](/docs/agents) configuration.
  194. ```json title="opencode.json"
  195. {
  196. "command": {
  197. "analyze": {
  198. "subtask": true
  199. }
  200. }
  201. }
  202. ```
  203. This is an **optional** config option.
  204. ---
  205. ### Model
  206. Use the `model` config to override the default model for this command.
  207. ```json title="opencode.json"
  208. {
  209. "command": {
  210. "analyze": {
  211. "model": "anthropic/claude-3-5-sonnet-20241022"
  212. }
  213. }
  214. }
  215. ```
  216. This is an **optional** config option.
  217. ---
  218. ## Built-in
  219. opencode includes several built-in commands like `/init`, `/undo`, `/redo`, `/share`, `/help`; [learn more](/docs/tui#commands).
  220. :::note
  221. Custom commands can override built-in commands.
  222. :::
  223. If you define a custom command with the same name, it will override the built-in command.