Home » Developer Tools » Kotlin Formatter
Paste Kotlin code and get clean, readable formatting in seconds, right in your browser.
Last updated 13 November 2026
This formatter changes whitespace only. It never compiles, runs, validates, adds, removes, or renames code tokens.
Messy Kotlin code with collapsed indentation is hard to read, review and debug. The Kotlin Formatter fixes indentation, line breaks and spacing so your code is easy to follow again.
Everything runs in your browser, so your code is never uploaded to a server. Paste your code, format it, and copy the result straight into your project.
Kotlin is the main language for Android apps, and it is also used for server side code and small tools. A Kotlin formatter takes Kotlin code that is hard to read and lays it out again with clean indentation, line breaks and spacing. It changes whitespace only, so the code still does exactly what it did before, only now a person can follow it. This page is a formatter, not a compiler, so it never checks whether your code is correct.
You paste code into the input box, pick 2 or 4 spaces per level, press Format, and copy the result or download it as a kt file. Everything runs in your browser, so your code never leaves your device, there is nothing to install, and no account is needed. If you work in more than one language, the same clean layout is available for Java code with the Java Formatter, for Go code with the Go Formatter, and for Python code with the Python Formatter.
Here is a short program run through the tool, with the exact numbers. Input, 69 characters:
import java.util.*; fun main() { val m: Map<String, Int> = mapOf(); }
Output, 73 characters, 4 lines:
import java.util.*;
fun main() {
val m: Map<String, Int> = mapOf();
}
The output is 4 characters longer than the input because the indent spaces are added back. That is the point: the extra characters are the structure made visible. Notice two details. The wildcard import stays exactly as written, with the star glued to the path. The generic type Map<String, Int> stays on one line instead of splitting at the comma.
A lambda shows the same care. Input, 25 characters:
val f = { a, b -> a + b }
Output, 29 characters, 3 lines:
val f = {
a, b -> a + b
}
The lambda parameters stay on one line, and there is exactly one space between the equals sign and the opening brace. The old version printed two spaces there and split the parameters across lines.
The Load Sample button shows the same effect on a bigger file. Its 156 character sample, a greet function with a when block and a string template, formats to 192 characters and 8 lines, with each when entry on its own line and the template Hello ${userName} copied exactly.
Most Kotlin code runs on Android, so it helps to know the three shapes you will meet most often. A data class holds a small bundle of values, such as a user with a name and an age, and the formatter keeps a short data class on one line. A sealed class lists a fixed set of types, such as the states of a screen, and each branch of a when block that reads it gets its own line. Coroutines run work in the background without blocking the screen, and the formatter lays out their blocks the same way it lays out any other block. In every case the tool formats layout only. It never checks whether the code compiles, and it never renames a class, a function or a value.
The formatter follows a small set of rules, and it applies them the same way every time. Knowing the rules helps you predict the output before you press Format.
| Rule | What it means |
|---|---|
| Wildcard imports kept intact | An import such as import java.util.*; keeps its star glued to the path |
| Generics kept together | A generic type such as Map<String, Int> stays on one line, even when it is nested inside another generic type |
| Lambda parameters kept together | The parameters of a lambda stay on one line, with one space after the equals sign |
| Type parameters keep their space | A declaration such as fun <T, U> keeps its space after fun |
| When entries on their own lines | Each entry of a when block sits on its own line, with its arrow spaced on both sides |
| String templates copied exactly | Text inside quotes is copied exactly, so an interpolated value is never reformatted |
| Indent 2 or 4 spaces | Each level inside a block moves right by the indent size you pick |
| One statement per line | Each statement ends on its own line, so you can read the code from top to bottom |
| Braces on the same line | An opening brace stays on the same line as the declaration, and the closing brace gets its own line |
This tool is deliberately narrow, and it is honest about its limits. It never compiles, runs or validates your code, so it cannot tell you whether your code is correct. It does not check your code against the ktlint style rules, and it does not apply the ktfmt Google style, so do not expect output that matches either tool. There is no file upload, so paste your code into the input box, and there is no format as you type, so press Format when you are ready. It does not wrap long lines and it has no max line length setting, so a long line stays on one line. It does not offer tabs or 8 spaces, and it does not add line numbers or syntax highlighting. There is also no diff view to compare the input with the output side by side. If you need any of those, use ktlint or ktfmt in your own project. Use this tool for the quick job: take messy code and make it readable in seconds.
Formatting is most useful when Kotlin code arrives in a shape you cannot read. Code copied from a chat message or a forum answer often arrives on one line or with collapsed indentation, and the first step in understanding it is to expand it into readable lines. The same goes for code pasted from a log file.
It also helps in code review. A clean layout shows the real structure, while a single line hides it. Format the snippet first, read it, then run ktlint on the project itself before you commit.
If you work with Swift code too, the Swift Formatter gives the same clean layout, and the C# Formatter does the same for C# code.
Yes, it is completely free with no sign up.
No. It changes whitespace only, meaning indentation, line breaks and spacing. It never adds, removes, renames or reorders code tokens, and it never compiles or runs your code.
No. Text inside string literals, character literals and comments is left untouched, even when it looks like code.
No. It never compiles, runs or validates code, so it cannot tell you whether your code is correct. It only makes the layout readable.
No. All formatting happens in your browser on your own device, so your code never leaves your computer.
When entries are placed on their own lines, and string templates are copied exactly, so interpolated values are never reformatted.
No. This tool is a whitespace only formatter that runs in your browser. ktlint checks your code against a set of style rules, and ktfmt applies the Google style. This tool does neither, and it never claims to. It only fixes indentation, line breaks and spacing. Use ktlint or ktfmt in your own project when you need their style.
No. There is no file upload on this page. Paste your code into the input box and press Format. You can download the result as a .kt file for your project.
Yes. A wildcard import such as import java.util.*; keeps its star glued to the path, and a generic type such as Map<String, Int> stays on one line, even when it is nested inside another generic type such as Map<String, Map<String, Int>>. A declaration such as fun <T, U> keeps its space after fun, and lambda parameters stay on one line.
Yes, as layout. A short data class stays on one line, each branch of a when block gets its own line, and coroutine blocks are laid out like any other block. The formatter never checks whether the code compiles or runs, so it cannot tell you whether a data class, a sealed class or a coroutine is written correctly.
If you also work with other languages, try the Java Formatter for the same clean formatting.