OpenCode Formatter
After each write or edit of a file, OpenCode willautomaticallyuse the formatter for the corresponding language to format it, ensuring the generated code always conforms to your project's code style. The whole process runs silently in the background, requiring no manual operation.
How It Works
When OpenCode writes or edits a file, it automatically performs formatting in these steps:
- Detect the extension of the file being written or edited (e.g.
.ts、.py、.goetc.) - Match the extension against all enabled formatters
- Run the matched formatting command on the file
- Automatically write the formatting result back to the file
For example, if your project package.json contains prettier depends on it, OpenCode will automatically detect it and, after each edit of .js、.ts、.css files such as, call prettier for formatting, with no extra configuration needed.
Built-in formatters
OpenCode has built-in formatter support covering mainstream languages and frameworks, divided into two categories:
- Enabled as soon as the command is available: just install the corresponding command on your system, and OpenCode will automatically detect and use it
- Requires a project config file or dependency: requires specific config files or dependency packages in the project
| Formatter | Supported file extensions | Enablement requirement |
|---|---|---|
air |
.R |
air Command available |
biome |
.js .jsx .ts .tsx .html .css .md .json .yaml and more |
Present in project biome.json or biome.jsonc Configuration file |
cargofmt |
.rs |
cargo fmt Command available |
clang-format |
.c .cpp .h .hpp .ino and more |
Present in project .clang-format Configuration file |
cljfmt |
.clj .cljs .cljc .edn |
cljfmt Command available |
dart |
.dart |
dart Command available |
dfmt |
.d |
dfmt Command available |
gleam |
.gleam |
gleam Command available |
gofmt |
.go |
gofmt Command available |
htmlbeautifier |
.erb .html.erb |
htmlbeautifier Command available |
ktlint |
.kt .kts |
ktlint Command available |
mix |
.ex .exs .eex .heex .leex .neex .sface |
mix Command available |
nixfmt |
.nix |
nixfmt Command available |
ocamlformat |
.ml .mli |
ocamlformat Command available, and present in project .ocamlformat Configuration file |
ormolu |
.hs |
ormolu Command available |
oxfmt(experimental) |
.js .jsx .ts .tsx |
package.json contains oxfmt dependency, and the corresponding experimental environment variable flag is set |
pint |
.php |
composer.json contains laravel/pint Depends on |
prettier |
.js .jsx .ts .tsx .html .css .md .json .yaml and more |
package.json contains prettier Depends on |
rubocop |
.rb .rake .gemspec .ru |
rubocop Command available |
ruff |
.py .pyi |
ruff Command available and corresponding config present |
rustfmt |
.rs |
rustfmt Command available |
shfmt |
.sh .bash |
shfmt Command available |
standardrb |
.rb .rake .gemspec .ru |
standardrb Command available |
terraform |
.tf .tfvars |
terraform Command available |
uv |
.py .pyi |
uv Command available |
zig |
.zig .zon |
zig Command available |
When multiple formatters support the same extension (e.g.
.rssupported by bothcargofmtandrustfmtand.rbsupported by bothrubocopandstandardrbsupported), OpenCode will automatically choose based on the tools installed in the project. To specify explicitly, you can disable unwanted formatters via the configuration below.
Configuration file structure
Custom configuration for formatters is written under the opencode.json 's formatter field, with the key being the formatter name (consistent with the names in the built-in table) and the value being the tool's config object:
Example
{
"$schema"
:
"https://opencode.ai/config.json"
,
"formatter"
:
{
// Formatter name: config object
"prettier"
:
{
// Fill in prettier's config options here
}
}
}
Each formatter supports the following config options:
| Configuration Item | Type | Description |
|---|---|---|
disabled |
boolean |
Set to true Can disable this formatter; other config options will be ignored |
command |
string[] |
An array of commands to perform formatting. Commands can use the $FILE placeholder, which is replaced at runtime with the full path of the file to be formatted |
environment |
object |
Environment variables injected when running the formatter, in key-value form |
extensions |
string[] |
A list of file extensions handled by this formatter (including ., such as ".ts") |
Disable formatters
1. Disable all formatters
If you don't want OpenCode to auto-format any files, you can set formatter directly to false:
Example
{
"$schema": "https://opencode.ai/config.json",
"formatter": false // Globally disable all formatters; no formatting is done after file writes
}
2. Disable a specific formatter
If you only need to disable one formatter, set it in that tool's config object disabled Set to true, other formatters are unaffected:
Example
{
"$schema": "https://opencode.ai/config.json",
"formatter": {
"prettier": {
"disabled": true // Only disable prettier; other formatters (such as ruff, gofmt) still work normally
}
}
}
Custom formatters
Pass command、environment and extensions config options can override the default behavior of built-in formatters, or add formatters not yet built into OpenCode.
1. Override the command of a built-in formatter
For example, specify particular arguments for prettier and restrict it to only certain extensions:
Example
{
"$schema"
:
"https://opencode.ai/config.json"
,
"formatter"
:
{
"prettier"
:
{
"command"
:
[
"npx"
,
"prettier"
,
"--write"
,
"$FILE"
]
,
// $FILE in the command array is a placeholder, replaced at runtime with the full path of the file to be formatted
// For example, actually executed: npx prettier --write /your/project/src/index.ts
"environment"
:
{
"NODE_ENV"
:
"development"
// Environment variables injected when running prettier
}
,
"extensions"
:
[
".js"
,
".ts"
,
".jsx"
,
".tsx"
]
// Override default extensions: only run prettier on these four file types, not .html, .css, etc.
}
}
}
2. Add a custom formatter
If the formatter you use is not in the built-in list, you can add it by specifying any name. Just provide the launch command and the file extensions to handle:
Example
{
"$schema"
:
"https://opencode.ai/config.json"
,
"formatter"
:
{
"custom-markdown-formatter"
:
{
// Custom name (any name, as long as it doesn't conflict with built-in tools)
"command"
:
[
"deno"
,
"fmt"
,
"$FILE"
]
,
// Use deno fmt to format files; $FILE is replaced with the actual file path
"extensions"
:
[
".md"
]
// Only run this formatter on .md files
}
}
}
3. Configure multiple formatters at once
Example
{
"$schema"
:
"https://opencode.ai/config.json"
,
"formatter"
:
{
"prettier"
:
{
"command"
:
[
"npx"
,
"prettier"
,
"--write"
,
"$FILE"
]
,
"environment"
:
{
"NODE_ENV"
:
"development"
}
,
"extensions"
:
[
".js"
,
".ts"
,
".jsx"
,
".tsx"
]
}
,
"ruff"
:
{
"disabled"
:
true
// The project doesn't use ruff, so disable it
}
,
"custom-markdown-formatter"
:
{
"command"
:
[
"deno"
,
"fmt"
,
"$FILE"
]
,
"extensions"
:
[
".md"
]
}
}
}