Skip to main content
OpenCode automatically formats files after they are written or edited using language-specific formatters. This ensures that the code that is generated follows the code styles of your project.

Built-in Formatters

OpenCode comes with several built-in formatters for popular languages and frameworks. Below is a list of the formatters, supported file extensions, and commands or config options it needs.
FormatterExtensionsRequirements
air.Rair command available
biome.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and morebiome.json(c) config file
cargofmt.rscargo fmt command available
clang-format.c, .cpp, .h, .hpp, .ino, and more.clang-format config file
cljfmt.clj, .cljs, .cljc, .edncljfmt command available
dart.dartdart command available
dfmt.ddfmt command available
gleam.gleamgleam command available
gofmt.gogofmt command available
htmlbeautifier.erb, .html.erbhtmlbeautifier command available
ktlint.kt, .ktsktlint command available
mix.ex, .exs, .eex, .heex, .leex, .neex, .sfacemix command available
nixfmt.nixnixfmt command available
ocamlformat.ml, .mliocamlformat command available and .ocamlformat config file
ormolu.hsormolu command available
oxfmt (Experimental).js, .jsx, .ts, .tsxoxfmt dependency in package.json and an experimental env variable flag
pint.phplaravel/pint dependency in composer.json
prettier.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and moreprettier dependency in package.json
rubocop.rb, .rake, .gemspec, .rurubocop command available
ruff.py, .pyiruff command available with config
rustfmt.rsrustfmt command available
shfmt.sh, .bashshfmt command available
standardrb.rb, .rake, .gemspec, .rustandardrb command available
terraform.tf, .tfvarsterraform command available
uv.py, .pyiuv command available
zig.zig, .zonzig command available
So if your project has prettier in your package.json, OpenCode will automatically use it.

How It Works

When OpenCode writes or edits a file, it:
  1. Checks the file extension against all enabled formatters.
  2. Runs the appropriate formatter command on the file.
  3. Applies the formatting changes automatically.
This process happens in the background, ensuring your code styles are maintained without any manual steps.

Configure Formatters

You can customize formatters through the formatter section in your OpenCode config.
opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {}
}
Each formatter configuration supports the following:
formatter.{name}.disabled
boolean
Set this to true to disable the formatter.
formatter.{name}.command
string[]
The command to run for formatting. Use $FILE as a placeholder for the file path.
formatter.{name}.environment
object
Environment variables to set when running the formatter.
formatter.{name}.extensions
string[]
File extensions this formatter should handle.

Disabling Formatters

To disable all formatters globally, set formatter to false:
opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": false
}
To disable a specific formatter, set disabled to true:
opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {
    "prettier": {
      "disabled": true
    }
  }
}

Custom Formatters

You can override the built-in formatters or add new ones by specifying the command, environment variables, and file extensions:
opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {
    "prettier": {
      "command": ["npx", "prettier", "--write", "$FILE"],
      "environment": {
        "NODE_ENV": "development"
      },
      "extensions": [".js", ".ts", ".jsx", ".tsx"]
    },
    "custom-markdown-formatter": {
      "command": ["deno", "fmt", "$FILE"],
      "extensions": [".md"]
    }
  }
}
The $FILE placeholder in the command will be replaced with the path to the file being formatted.

Examples

Override Prettier Configuration

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {
    "prettier": {
      "command": ["npx", "prettier", "--write", "--single-quote", "$FILE"],
      "environment": {
        "NODE_ENV": "production"
      }
    }
  }
}

Add Custom Python Formatter

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {
    "black": {
      "command": ["black", "$FILE"],
      "extensions": [".py"]
    }
  }
}

Configure Multiple Formatters

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {
    "prettier": {
      "command": ["npx", "prettier", "--write", "$FILE"],
      "extensions": [".js", ".ts", ".jsx", ".tsx"]
    },
    "gofmt": {
      "command": ["gofmt", "-w", "$FILE"],
      "extensions": [".go"]
    },
    "rustfmt": {
      "command": ["rustfmt", "$FILE"],
      "extensions": [".rs"]
    }
  }
}