Scala Code Formatter (Scalafmt Style)
Guide
Scala Code Formatter (Scalafmt Style)
Paste raw Scala source and get back code formatted in the Scalafmt house style — 2-space indentation, balanced braces, normalized operator spacing, sorted imports, and consistent trailing commas. Output is deterministic, so the same input always produces the same result, which means cleaner diffs in code review and no more bikeshedding over whitespace.
How to Use
- Paste your Scala code into the input box, or load the sample to see how it works.
- Pick an indent style (2 spaces is the Scalafmt default) and a maximum column width (80, 100, 120, or 160).
- Toggle the options you want: trailing commas in multi-line lists, sorted/grouped imports, operator spacing, comment preservation, and blank-line collapsing.
- Click Format. Copy the result with the copy button or download it as a
.scalafile.
Features
- Scalafmt default style – Mirrors the conventions used by the official scalafmt tool, including indentation, brace handling, and binary operator spacing.
- Configurable indentation – Choose 2 spaces, 4 spaces, or tabs to match your project’s style guide.
- Max column width – Pick 80, 100, 120, or 160 to suit your team’s wrap policy.
- Sorted & grouped imports – Reorders imports alphabetically and groups them into stdlib, third-party, and local buckets with blank lines between groups.
- Trailing commas – Adds trailing commas to multi-line argument and parameter lists for cleaner version-control diffs.
- Comment preservation – Keeps line comments, block comments, and Scaladoc intact while still tidying the surrounding code.
- String & interpolation safe – Reformatting never touches the contents of strings, character literals, triple-quoted strings, or
s"..."/f"..."interpolations. - Scala 3 friendly – Understands modern Scala 3 keywords like
given,using,then,enum, andderives. - Runs entirely in your browser – No upload, no server round-trip, no telemetry. Your code never leaves your machine.
Common Use Cases
- Tidy up a quick code snippet before pasting it into a Slack thread, Stack Overflow answer, or technical blog post.
- Normalize formatting in legacy projects that don’t yet have a scalafmt config checked in.
- Pre-format generated code (macro output, schema-derived case classes, etc.) so it reads like hand-written Scala.
- Standardize style across mixed-author files where each contributor used a slightly different IDE setting.
- Sanity-check a small refactor without spinning up sbt or the Scala build pipeline.
FAQ
-
What is Scalafmt and why is consistent Scala formatting important?
Scalafmt is the de facto code formatter for the Scala ecosystem. It enforces a single, deterministic style across an entire codebase, which removes formatting from code-review discussions, reduces noisy diffs caused by whitespace changes, and helps new contributors blend in with the rest of the project. Consistent formatting also makes it easier to scan unfamiliar code because indentation and spacing always carry the same meaning.
-
Why does Scalafmt default to a maximum column width of 80?
The 80-column convention dates back to early terminals and punched cards, but it survives today because narrower lines are easier to read in side-by-side diffs, code review tools, and split editor panes. An 80-column limit also encourages shorter identifiers, less deeply nested expressions, and more frequent extraction of helper methods. Many teams relax this to 100 or 120 columns, which is why modern formatters let you configure it.
-
What are trailing commas and why are they useful?
A trailing comma is a comma after the last element of a multi-line list, argument list, or parameter list. They became common in formatters because they make line-based diffs cleaner: adding a new element only adds one line instead of also modifying the previous line to add a comma. Trailing commas also make it easier to reorder lines, since every element ends the same way. Scala has supported trailing commas in argument and parameter lists since version 2.12.2.
-
Why are imports typically grouped into stdlib, third-party, and local?
Grouping imports by origin is a long-standing convention borrowed from languages like Go and Python (with isort). It makes dependency provenance visible at a glance: standard-library imports come first, followed by third-party libraries, followed by code from the same project. The blank lines between groups act as visual anchors so reviewers can quickly spot when a change introduces a new external dependency, which is often a signal worth discussing.
-
What is the difference between syntactic and semantic code formatting?
A syntactic formatter rearranges whitespace, line breaks, and indentation based purely on the source text — it doesn't need to understand types, resolve symbols, or run the compiler. A semantic formatter, by contrast, can rename identifiers, reorder methods based on usage, or remove unused imports because it has access to the compiler's view of the program. Scalafmt is primarily syntactic, which makes it fast and safe to run on incomplete or non-compiling code.
Install Our Extensions
Add IO tools to your favorite browser for instant access and faster searching
恵 Scoreboard Has Arrived!
Scoreboard is a fun way to keep track of your games, all data is stored in your browser. More features are coming soon!
Must-Try Tools
View All New Arrivals
View AllUpdate: Our latest tool was added on Jun 16, 2026
