|
| 1 | +--- |
| 2 | +title: Formatters |
| 3 | +description: Code formatting with opencode |
| 4 | +--- |
| 5 | + |
| 6 | +opencode automatically formats code files after they are written or edited using language-specific formatters. This ensures consistent code style across your project without manual intervention. |
| 7 | + |
| 8 | +## Built-in Formatters |
| 9 | + |
| 10 | +opencode comes with several built-in formatters for popular languages and frameworks: |
| 11 | + |
| 12 | +| Formatter | Languages/Extensions | Requirements | |
| 13 | +| -------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------- | |
| 14 | +| gofmt | .go | `gofmt` command available | |
| 15 | +| mix | .ex, .exs, .eex, .heex, .leex, .neex, .sface | `mix` command available | |
| 16 | +| prettier | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and [more](https://prettier.io/docs/en/index.html) | `prettier` dependency in package.json | |
| 17 | +| biome | .js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and [more](https://biomejs.dev/) | `biome.json` config file | |
| 18 | +| zig | .zig, .zon | `zig` command available | |
| 19 | +| clang-format | .c, .cpp, .h, .hpp, .ino, and [more](https://clang.llvm.org/docs/ClangFormat.html) | `.clang-format` config file | |
| 20 | +| ktlint | .kt, .kts | `ktlint` command available | |
| 21 | +| ruff | .py, .pyi | `ruff` command available with config | |
| 22 | +| rubocop | .rb, .rake, .gemspec, .ru | `rubocop` command available | |
| 23 | +| standardrb | .rb, .rake, .gemspec, .ru | `standardrb` command available | |
| 24 | +| htmlbeautifier | .erb, .html.erb | `htmlbeautifier` command available | |
| 25 | + |
| 26 | +Formatters are automatically enabled when their requirements are met in your project environment. |
| 27 | + |
| 28 | +## Configuration |
| 29 | + |
| 30 | +You can customize formatters through the `formatter` section in your `opencode.json` configuration file. |
| 31 | + |
| 32 | +### Disabling Formatters |
| 33 | + |
| 34 | +To disable a specific formatter, set its `disabled` property to `true`: |
| 35 | + |
| 36 | +```json title="opencode.json" |
| 37 | +{ |
| 38 | + "$schema": "https://opencode.ai/config.json", |
| 39 | + "formatter": { |
| 40 | + "prettier": { |
| 41 | + "disabled": true |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### Custom Formatters |
| 48 | + |
| 49 | +You can override the default formatters or add new ones by specifying the command, environment variables, and file extensions: |
| 50 | + |
| 51 | +```json title="opencode.json" |
| 52 | +{ |
| 53 | + "$schema": "https://opencode.ai/config.json", |
| 54 | + "formatter": { |
| 55 | + "custom-prettier": { |
| 56 | + "command": ["npx", "prettier", "--write", "$FILE"], |
| 57 | + "environment": { |
| 58 | + "NODE_ENV": "development" |
| 59 | + }, |
| 60 | + "extensions": [".js", ".ts", ".jsx", ".tsx"] |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +The `$FILE` placeholder in the command will be replaced with the path to the file being formatted. |
| 67 | + |
| 68 | +### Configuration Options |
| 69 | + |
| 70 | +Each formatter configuration supports these properties: |
| 71 | + |
| 72 | +| Property | Type | Description | |
| 73 | +| ------------- | -------- | ------------------------------------------------------- | |
| 74 | +| `disabled` | boolean | Set to `true` to disable the formatter | |
| 75 | +| `command` | string[] | The command to run for formatting | |
| 76 | +| `environment` | object | Environment variables to set when running the formatter | |
| 77 | +| `extensions` | string[] | File extensions this formatter should handle | |
| 78 | + |
| 79 | +## How It Works |
| 80 | + |
| 81 | +When opencode writes or edits a file, it: |
| 82 | + |
| 83 | +1. Checks the file extension against all enabled formatters |
| 84 | +2. Runs the appropriate formatter command on the file |
| 85 | +3. Applies the formatting changes automatically |
| 86 | + |
| 87 | +This process happens seamlessly in the background, ensuring your code maintains consistent formatting without requiring manual steps. |
0 commit comments