b2KIT

Kotlin Formatter

Format Kotlin code following official Kotlin style guide conventions with ktlint compatibility.

Tested tool guide Tested browser tools Checked August 16, 2026

What Kotlin Formatter does, with a checked example

Paste Kotlin code and the tool re-emits it with the spacing, indentation, and import order that the official Kotlin style guide requires, matching what ktlint produces with its default rules. It applies the conventions the compiler ignores: four-space indentation, spaces around operators and colons, comma spacing, brace placement, and lexicographic import sorting. The thing most users get wrong is that it is a formatter, not a linter or a compiler. It will not fix syntax errors, flag unused imports, or rewrite code into more idiomatic Kotlin. What goes in is what comes out, only re-spaced.

Worked example

A concrete input and expected output from the current implementation.

Input

import android.widget.TextView
import android.app.Activity

class MainActivity:Activity(){
fun greet(name:String){
val text=TextView(this)
text.text="Hello "+name
setContentView(text)
}
}

Expected output

import android.app.Activity
import android.widget.TextView

class MainActivity : Activity() {
    fun greet(name: String) {
        val text = TextView(this)
        text.text = "Hello " + name
        setContentView(text)
    }
}

The formatter sorted the two imports alphabetically (android.app before android.widget), added the spaces the style guide requires around the supertype colon, the parameter-type colon, the equals signs, and the concatenation operator, and indented the nested bodies four spaces per level. Only whitespace and import order changed; the code does exactly what it did before.

How the result is produced

1

Spacing applied by syntax role

The tool reads the code as Kotlin, not as raw text, so spacing decisions depend on what a token is. The colon in a supertype list gets a space on both sides, the colon in a parameter type gets one space after, and commas, binary operators, and opening braces get exactly one space. Text inside string literals and comments is never altered.

2

Structure is preserved

Braces, keywords, and expression shape are not rewritten, and line breaks you chose are kept wherever the style guide allows both options. The formatter's scope is whitespace, indentation, and import ordering: it does not rename identifiers, add expression bodies, or suggest rewrites. Code that does not parse as Kotlin is not something it can safely reformat.

Good uses

  • Before committing to a shared Kotlin or Android project: format the changed functions here so the default ktlint check that runs in CI passes without an extra IDE round-trip.
  • When a file arrives with mixed tabs, four-space indentation, and inconsistent spacing from a colleague or a generated source, normalize the whole file to one convention in a single pass.
  • When reviewing Kotlin on a screen without an IDE (a pull request, a gist, an answer on a forum), re-format the snippet so the whitespace stops obscuring the code.

Limits and checks

  • It cannot repair syntax errors. If the input is not valid Kotlin, the tool has no reliable way to reformat it, and it will not guess where a missing brace belongs. Fix the error first, then format.
  • ktlint-compatible means the default rule set. Projects commonly enable extra rules through .editorconfig, such as max-line-length or wrapping rules; under those configurations the tool's output can still be flagged, even though it follows the style guide.
  • Imports are sorted lexicographically, not verified. An import that names a deleted class, or one nothing uses, is still sorted neatly into place. The formatter cannot detect those; the compiler can.

Common questions

Will the output pass ktlint in our CI?

Only if your project runs ktlint with the default rule set. The tool applies the standard style-guide formatting rules, so the default checks for indentation, spacing, and import order should pass. If your project's .editorconfig enables extra rules such as max-line-length, those may still be reported, so run ktlint locally once after formatting.

Can I format a whole project, or multiple files at once?

No. The tool takes one snippet or one file at a time and returns the formatted text for you to copy back. It has no access to your project, which also means it has no package layout, .editorconfig, or custom ktlint configuration to work from. For project-wide formatting, ktlint itself is the right tool.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools