Skip to content

Formatters

Code formatting with opencode

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.

Built-in Formatters

opencode comes with several built-in formatters for popular languages and frameworks:

FormatterLanguages/ExtensionsRequirements
gofmt.gogofmt command available
mix.ex, .exs, .eex, .heex, .leex, .neex, .sfacemix command available
prettier.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and moreprettier dependency in package.json
biome.js, .jsx, .ts, .tsx, .html, .css, .md, .json, .yaml, and morebiome.json config file
zig.zig, .zonzig command available
clang-format.c, .cpp, .h, .hpp, .ino, and more.clang-format config file
ktlint.kt, .ktsktlint command available
ruff.py, .pyiruff command available with config
rubocop.rb, .rake, .gemspec, .rurubocop command available
standardrb.rb, .rake, .gemspec, .rustandardrb command available
htmlbeautifier.erb, .html.erbhtmlbeautifier command available

Formatters are automatically enabled when their requirements are met in your project environment.

Configuration

You can customize formatters through the formatter section in your opencode.json configuration file.

Disabling Formatters

To disable a specific formatter, set its disabled property to true:

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

Custom Formatters

You can override the default formatters or add new ones by specifying the command, environment variables, and file extensions:

opencode.json
{
"$schema": "https://opencode.ai/config.json",
"formatter": {
"custom-prettier": {
"command": ["npx", "prettier", "--write", "$FILE"],
"environment": {
"NODE_ENV": "development"
},
"extensions": [".js", ".ts", ".jsx", ".tsx"]
}
}
}

The $FILE placeholder in the command will be replaced with the path to the file being formatted.

Configuration Options

Each formatter configuration supports these properties:

PropertyTypeDescription
disabledbooleanSet to true to disable the formatter
commandstring[]The command to run for formatting
environmentobjectEnvironment variables to set when running the formatter
extensionsstring[]File extensions this formatter should handle

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 seamlessly in the background, ensuring your code maintains consistent formatting without requiring manual steps.