Kotlin Formatter: Indent and Beautify Kotlin Code
Kotlin is used for Android development, backend services (Ktor, Spring), and multiplatform apps. This formatter applies consistent brace-depth indentation to Kotlin code — useful for reviewing AI-generated code, formatting snippets from documentation, or cleaning up pasted code before opening it in IntelliJ or Android Studio.
Kotlin style guide recommends 4-space indentation. Single-expression functions on one line are preserved as-is.
Kotlin vs Java Formatting
Kotlin uses the same { } block delimiters as Java but adds trailing lambda syntax, single-expression functions, and when expressions. This formatter handles all { } blocks identically — trailing lambdas on a single line have balanced braces and do not affect indentation.
Data Classes and Interfaces
Kotlin data classes (data class User(val name: String, val age: Int)) are typically one-liners and are formatted without added newlines. Regular class bodies with members use { } blocks and are indented correctly. Interface declarations work the same way.
IntelliJ Code Style
IntelliJ IDEA and Android Studio have a powerful built-in Kotlin formatter accessible via Code → Reformat Code (⌘⌥L / Ctrl+Alt+L). It enforces line length, blank lines between declarations, and more. This browser tool handles indentation only — use the IDE formatter for committed code.
Practical Examples
Formatting Kotlin from Documentation
Kotlin documentation and blog posts often show unindented code snippets.
- 1.Paste the Kotlin snippet
- 2.Formatter applies 4-space indentation (Kotlin style guide standard)
- 3.Review class and function structure
- 4.Copy to your editor or IDE for further editing
What Gets Formatted
- class, object, interface, fun, if, when, for, while blocks
- Trailing lambdas { } on same line treated as inline
- 4-space indentation default (Kotlin style guide)
- Multiple blank lines collapsed
Good Use Cases
- Formatting AI-generated Kotlin code
- Reviewing Kotlin snippets from documentation or Stack Overflow
- Cleaning up pasted code before opening in Android Studio
- Teaching Kotlin code structure to Java developers
Frequently Asked Questions
What indentation does the Kotlin style guide recommend?
The Kotlin style guide (and Android style guide) recommend 4-space indentation for all blocks. 2-space indentation is sometimes seen in projects with deeply nested code, but 4 spaces is the official recommendation.
Does this handle Kotlin lambda syntax?
Lambda expressions use { } blocks and are indented like any other block. Single-expression functions (val double = { x: Int -> x * 2 }) stay on one line since they have matching braces on the same line.
What about trailing lambdas?
Trailing lambda syntax like list.filter { it.age > 18 } has the { } on the same line, so the formatter treats it as a balanced inline block and does not change the indentation.
How do I format Kotlin in IntelliJ or Android Studio?
Use Code → Reformat Code (⌘⌥L on Mac, Ctrl+Alt+L on Windows/Linux). You can also configure the formatter in Settings → Editor → Code Style → Kotlin to match your team's style guide.