Xerobit

TOML Formatter: Prettify Your TOML Instantly

Paste your TOML text below to reformat it with consistent indentation, sorted keys and proper quoting. This toml formatter runs entirely in your browser, so nothing is uploaded to any server. It parses the input into a data structure and emits cleanly formatted output that follows the TOML v1.0.0 specification.

Formatted TOML

How to use the toml formatter

  1. Paste your raw or minified TOML into the TOML input field.
  2. Choose your preferred indentation style: 2 spaces, 4 spaces or tab.
  3. Check Sort keys alphabetically if you want keys ordered within each table.
  4. The formatted result appears instantly as you type or change options.

How the toml formatter works

This toml formatter reads your input character by character, building an internal tree of tables, arrays and key-value pairs. It handles bare keys, quoted keys, dotted keys, inline tables and arrays of tables according to the TOML v1.0.0 specification.

Once parsed, the emitter walks the tree and writes each element using your chosen indentation. If sorting is enabled, keys at every level are compared alphabetically before emission. Comments are stripped during parsing because preserving their exact position relative to reformatted keys would require a full source-map implementation.

The parser distinguishes between standard tables defined with single brackets and array-of-tables defined with double brackets. When it encounters a double-bracket header like [[servers]], it creates a new object inside an array rather than overwriting an existing table. This distinction matters for configuration files that define lists of similar items such as database replicas or build targets.

TOML syntax quick reference

TOML (Tom's Obvious Minimal Language) uses a small set of constructs:

ConstructExample
Key/value pairname = "Alice"
Table[database]
Array of tables[[servers]]
Inline tablepoint = { x = 1, y = 2 }
Arrayports = [80, 443]
Dotted keysite.name = "Xerobit"
Multi-line stringbio = """Line 1\nLine 2"""
Literal stringpath = 'C:\Users\admin'

The formatter normalizes all of these into a consistent style. Bare keys may contain only letters, digits, hyphens and underscores. Any other characters require quoting the key with single or double quotes.

When to format TOML

Configuration files for tools like Cargo, Poetry, Hugo and InfluxDB use TOML. Over time these files accumulate inconsistent spacing, mixed indentation and unsorted keys. Running them through a toml formatter restores readability without changing meaning.

If you need to convert the result to another format, you can pass the formatted output to a TOML to JSON converter or transform it with TOML to YAML. To check whether a file is syntactically valid before formatting, use a TOML validator.

Developers working with environment variables might also find the env formatter useful for related configuration tasks. Keeping configuration files consistently formatted reduces merge conflicts when multiple team members edit the same file in version control.

Common mistakes in hand-written TOML

A few errors frequently break parsers:

  • Adding keys to a table after defining a sub-table. Once [a.b] appears, you cannot add c = 1 under [a] later.
  • Using commas instead of dots in table headers: [a, b] is invalid; use [a.b].
  • Forgetting quotes around string values that contain special characters like # or =.
  • Mixing tabs and spaces inconsistently, which this tool resolves by applying uniform indentation.
  • Defining the same key twice in one table scope, which the specification forbids.

The parser reports the line number when it encounters these issues, making them straightforward to fix. Pay particular attention to the difference between [table] and [[array]] syntax, as confusing them produces subtle bugs that are hard to trace in large configuration files.

Formatting options explained

Indentation controls the whitespace added inside tables and inline structures. Two spaces is the most common convention in Rust and Python projects. Four spaces matches some style guides, and tabs are preferred by developers who configure their editor width independently.

Sort keys arranges keys alphabetically within each table scope. Top-level keys are sorted first, then keys inside each [table], and so on. Sorting makes diffs cleaner in version control because new keys slot into a predictable position rather than appending at the end.

The toml formatter applies these choices uniformly across the entire document. Inline tables always stay on one line, because TOML 1.0 does not allow line breaks inside them. Arrays stay on one line while they fit in 80 columns and are broken across indented lines when they do not. Strings, numbers and dates are copied exactly as you wrote them, so a multi-line string stays multi-line, 0xFF stays hexadecimal and 1979-05-27 stays a date.

Worked examples

ExampleInputResult
Basic configTOML input: title="My App" / [server] / port=8080 / host="localhost"; Indentation: 2; Sort keys alphabetically: yestitle = "My App" / [server] / host = "localhost" / port = 8080 /
Array of tablesTOML input: [[products]] / name="Hammer" / sku=738594937 / [[products]] / name="Nail" / sku=284758393 / color="gray"; Indentation: 2; Sort keys alphabetically: no[[products]] / name = "Hammer" / sku = 738594937 / [[products]] / name = "Nail" / sku = 284758393 / color = "gray" /
Dotted keysTOML input: site.name="Xerobit" / site.url="https://xerobit.dev"; Indentation: 4; Sort keys alphabetically: yes[site] / name = "Xerobit" / url = "https://xerobit.dev" /

FAQ

Does this toml formatter upload my data?

No. Parsing and formatting happen entirely in your browser using JavaScript. Nothing leaves your machine.

Are comments preserved after formatting?

No. The parser builds a data tree and discards comments. Preserving them while reordering keys would require a more complex source-mapping approach.

What TOML version is supported?

The parser targets TOML v1.0.0, including dotted keys, inline tables, multi-line strings and arrays of tables.

Can I format a very large TOML file?

Yes, but extremely large files may take a moment since parsing runs synchronously in the browser. Typical configuration files format instantly.

How do I validate TOML before formatting?

Use a TOML validator to check syntax. The formatter also reports parse errors with line numbers if the input is invalid.

Why sort keys alphabetically?

Sorted keys produce deterministic output, which reduces noise in version control diffs and makes configuration files easier to scan.