Skip to content

Code Formatters

📝 Course Notes

Key knowledge points from this lesson:

5.18 Code Formatters Study Notes

OpenCode automatically uses language-specific formatters after files are written or edited. This ensures that generated code follows your project's code style.

Built-in Formatters

OpenCode includes built-in formatters for many popular languages and frameworks:

FormatterExtensionsRequirements
gofmt.gogofmt command available
mix.ex, .exs, .eex, .heex, .leex, .neex, .sfacemix command available
prettier.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, etc.prettier dependency in package.json
biome.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, etc.biome.json(c) config file exists
zig.zig, .zonzig command available
clang-format.c, .cpp, .h, .hpp, .ino, etc..clang-format config file exists
ktlint.kt, .ktsktlint command available
ruff.py, .pyiruff command available and configured
rustfmt.rsrustfmt command available
uv.py, .pyiuv command available
rubocop.rb, .rake, .gemspec, .rurubocop command available
standardrb.rb, .rake, .gemspec, .rustandardrb command available
htmlbeautifier.erb, .html.erbhtmlbeautifier command available
air.Rair command available
dart.dartdart command available
ocamlformat.ml, .mliocamlformat command available and .ocamlformat config exists
terraform.tf, .tfvarsterraform command available
gleam.gleamgleam command available
nixfmt.nixnixfmt command available
shfmt.sh, .bashshfmt command available
oxfmt (experimental).js, .jsx, .ts, .tsxoxfmt dependency in package.json and experimental env var enabled

If your project has prettier in its package.json, OpenCode will automatically use it.

How It Works

When OpenCode writes or edits a file:

  1. Checks all enabled formatters based on file extension
  2. Runs the appropriate formatting command
  3. Automatically applies formatting changes

This process happens in the background, ensuring code style is maintained without manual intervention.

Configuration

Customize formatters through the formatter section in your config file:

json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {}
}

Each formatter configuration supports the following options:

PropertyTypeDescription
disabledbooleanSet to true to disable this formatter
commandstring[]Formatting command
environmentobjectEnvironment variables when running the formatter
extensionsstring[]File extensions this formatter handles

Disabling Formatters

Globally disable all formatters:

json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": false
}

Disable a specific formatter:

json
{
  "$schema": "https://opencode.ai/config.json",
  "formatter": {
    "prettier": {
      "disabled": true
    }
  }
}

Custom Formatters

You can override built-in formatters or add new ones:

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 of the file being formatted.