scalafmt

Opinionated Scala code formatter

TLDR

Format files
$ scalafmt [file.scala]
Format in place
$ scalafmt -i [file.scala]
Check formatting
$ scalafmt --check [file.scala]
Format directory
$ scalafmt [src/]
Use config file
$ scalafmt -c [.scalafmt.conf] [file.scala]
Show diff
$ scalafmt --diff [file.scala]
Format stdin
$ cat [file.scala] | scalafmt --stdin

SYNOPSIS

scalafmt [-i] [--check] [-c config] [options] [files]

DESCRIPTION

scalafmt is an opinionated code formatter for Scala that enforces consistent style across projects. It reformats source code according to configurable rules covering line length, indentation, alignment, and import ordering, supporting both Scala 2 and Scala 3 syntax through dialect configuration.The --check mode verifies formatting without modifying files, returning a non-zero exit code when changes would be made, which is useful for CI pipeline enforcement. The -i (in-place) mode directly modifies files during development. Configuration is defined in a .scalafmt.conf file at the project root using HOCON format.

PARAMETERS

-i, --inplace

Modify files in place.
--check
Check only, no changes.
-c, --config FILE
Config file.
--diff
Show changes.
--stdin
Read from stdin.
--stdout
Write to stdout.
--exclude PATTERN
Exclude files.
--help
Show help.

CONFIGURATION

.scalafmt.conf

Project-level configuration file in HOCON format defining formatting rules including maxColumn, dialect, align settings, and rewrite rules.

INSTALL

nix profile install nixpkgs#scalafmt

CAVEATS

May change code structure subtly. Configuration options are many. Editor integration recommended.

HISTORY

scalafmt was created by Olafur Pall Geirsson for Scala formatting. It's widely used in the Scala ecosystem for code style enforcement.

SEE ALSO

scala(1), sbt(1), metals(1)