Xerobit

YAML Formatter: Beautify and Format YAML

Paste your YAML into the input box to format it instantly. This yaml formatter parses block mappings, sequences, flow collections, quoted strings, and block scalars, then re-emits clean canonical YAML. It runs entirely in your browser so nothing is uploaded to any server.

Formatted YAML

How to use the yaml formatter

  1. Paste or type your YAML into the YAML input text area.
  2. Choose 2 spaces or 4 spaces for indentation.
  3. Check Sort keys alphabetically if you want mapping keys ordered.
  4. The formatted result appears immediately below as you type.

How the yaml formatter works

This tool uses a hand-written parser that reads a practical subset of YAML 1.2. It processes block mappings (key-value pairs), block sequences (lists starting with -), flow collections written as [...] or {...}, plain and quoted scalars, and block scalars introduced by | or >. Comments beginning with # are removed (the tool shows a note when it dropped any).

After building an internal tree of objects, arrays, and strings, the emitter walks the tree and writes canonical YAML using your chosen indent size. Flow collections are preserved inline rather than expanded into block form. For example, given a:\n b: 1 with a 4-space indent, the parser creates the object {a: {b: 1}} and the emitter outputs a:\n b: 1.

Supported YAML features

The parser handles the constructs most configuration files rely on:

  • Block mappings: nested key-value pairs indented consistently.
  • Block sequences: list items starting with - .
  • Flow collections: inline arrays [1, 2] and objects {a: 1}, preserved as flow style in the output.
  • Scalars: unquoted strings, single-quoted '...', double-quoted "..." with escape processing, booleans (true, false), numbers, and null.
  • Block scalars: literal | and folded > styles, including chomping indicators - and +.

The tool is checked so that parsing the formatted output gives the same data as parsing the original input, apart from key order when sorting is on. Duplicate keys and tab indentation are rejected.

Unsupported features

To avoid silently producing wrong results, the parser rejects several advanced YAML features outright. If your input contains any of these, the tool returns a clear error message instead of guessing:

  • Anchors (&name) and aliases (*name)
  • Tags (!!str, !custom)
  • Merge keys (<<)
  • Multi-document streams separated by --- or ...

If you need to convert between formats rather than just beautify, tools like YAML to JSON or XML to YAML handle those transformations. To check syntax without reformatting, use the YAML validator.

Common formatting mistakes

YAML is sensitive to whitespace. A frequent error is mixing tabs and spaces. The parser treats tabs as invalid indentation characters because the YAML specification forbids them for structure. Always use spaces.

Another common issue is inconsistent indentation within the same mapping level. Every child key must be indented by exactly the same number of spaces relative to its parent. If one key uses two spaces and another uses three, the parser cannot determine the nesting depth and will report an error.

Unquoted colons inside plain scalars also cause problems. A value like time: 10:30 is ambiguous. Wrap such values in quotes: time: "10:30". The formatter does not keep your original quote style: strings that are safe as plain text are written without quotes, anything else is quoted, and words that YAML 1.1 readers treat as booleans (yes, no, on, off) are always quoted so every YAML version reads them as text. Numbers, booleans and null are copied exactly as you wrote them (so 1.50 and 0x1F stay as they are), and multi-line strings, including folded > ones, are written as literal | blocks.

When to sort keys

Enabling alphabetical key sorting is useful when comparing configuration files or preparing YAML for version control. Sorted keys produce deterministic output, which means identical data always generates identical text. This reduces noise in diffs.

However, some applications depend on key order. For instance, certain CI pipeline definitions execute steps in the order they appear. In those cases, leave sorting disabled so the formatter preserves the original sequence. You can also use YAML minify to strip whitespace when file size matters more than readability.

Worked examples

ExampleInputResult
Simple mappingYAML input: name: John / age: 30 / city: New York; Indent spaces: 2; Sort keys alphabetically: noname: John / age: 30 / city: New York /
Nested with listYAML input: server: / host: localhost / ports: / - 80 / - 443 / debug: true; Indent spaces: 2; Sort keys alphabetically: noserver: / host: localhost / ports: / - 80 / - 443 / debug: true /
Flow collectionYAML input: items: [1, 2, 3] / user: {name: Alice, active: true}; Indent spaces: 2; Sort keys alphabetically: yesitems: [1, 2, 3] / user: {active: true, name: Alice} /
Block scalarYAML input: description: | / This is a multi-line / description. / count: 5; Indent spaces: 4; Sort keys alphabetically: nodescription: | / This is a multi-line / description. / count: 5 /

FAQ

Does this yaml formatter upload my data?

No. All parsing and formatting happens locally in your browser. Your YAML is never sent to any server.

What YAML version does it support?

It implements a practical subset of YAML 1.2 covering block and flow collections, quoted and plain scalars, and block scalars. Advanced features like anchors and tags are intentionally rejected with an error.

Will formatting change the meaning of my YAML?

No. The tool guarantees a correct round trip: parsing the formatted output yields the same data structure as parsing the original input.

Can I use tabs for indentation?

No. The YAML specification forbids tabs for indentation. Use spaces only. You can choose 2 or 4 spaces per level.

Why does my input produce an error about anchors?

Anchors and aliases are not supported because partially handling them could produce incorrect data. Remove them or resolve them before formatting.

Does it preserve comments?

No. Comments are removed because the formatter rebuilds the file from the parsed data, and a note tells you when any were dropped. Keep a copy of the original if the comments matter.