YAML Beautifier

Re-indent YAML to two spaces with comments, anchors and multiple documents kept, and get the exact line and column of syntax errors or duplicate keys.

The interactive tool loads here when JavaScript runs.

About this tool

What is the YAML Beautifier?

The YAML Beautifier parses your YAML with a full YAML 1.2 parser and prints it back with consistent two-space indentation. Paste a Kubernetes manifest, a GitHub Actions workflow or a docker-compose file into Input YAML, click Format YAML, and copy or download the result as formatted.yaml.

Because it parses the file instead of shuffling lines around, the nesting in the output is always the nesting the file really has. Comments, anchors and aliases, block scalars (| and >), quoted strings and multiple documents separated by --- all survive the round trip.

If the input is not valid YAML, nothing is printed. You get the parser message with the line and column, for example a flow sequence missing its closing ] or a mapping that defines the same key twice.

  • Full YAML 1.2 parser, not a line-by-line re-indenter
  • Keeps comments, anchors, aliases, block scalars and multiple documents
  • Reports syntax errors and duplicate keys with line and column
  • Copy or download as formatted.yaml
  • Runs in your browser; files are not uploaded
Read the guide: Formatting HTML, XML and YAML Without Changing What They Mean

Updated

How to use the YAML Beautifier

  1. Paste your YAML into the Input YAML box. Several documents separated by --- are fine.
  2. Click Format YAML.
  3. If the input has a syntax error, read the message under the button. It names the problem and shows the line and column where the parser stopped.
  4. Otherwise copy the Formatted YAML with the copy button, or download it as formatted.yaml.

Worked example

Four-space config with an anchor

Input

defaults: &defaults
      adapter: postgres
      pool: 5
development:
      <<: *defaults
      database: app_dev   # local only

Output

defaults: &defaults
  adapter: postgres
  pool: 5
development:
  <<: *defaults
  database: app_dev # local only

Indentation drops to two spaces, the &defaults anchor and *defaults alias are kept as written, and the trailing comment stays on its line with a single space before the #.

Background and insight

YAML is indentation-sensitive in a stricter way than it looks. A key belongs to whichever mapping its column lines up with, so moving a line two spaces left can silently move a setting from a container definition up to the pod. Tabs are not allowed for indentation at all. That is why reformatting YAML by hand, or with a line-based script, is risky: it is easy to change meaning while only meaning to change looks.

This tool uses the open-source yaml library for JavaScript, which follows YAML 1.2. Version 1.2 (2009) cleaned up the type rules of 1.1: on, off, yes and no are plain strings rather than booleans. Some older parsers, including PyYAML, still apply 1.1 rules, so country: NO can turn into false there. Formatting does not change your scalars, but it is worth knowing which spec your consumer uses.

Duplicate keys are a real-world bug that many tools let through. Most parsers keep the last value and drop the first without a warning, which is how a second "image:" line in a manifest ends up deployed. This beautifier treats a duplicate key as an error and tells you where it is.

Common use cases

  • Kubernetes and Helm manifests

    Normalise indentation in a manifest that was assembled from several snippets before applying it.

  • CI workflow files

    Find the line that breaks a GitHub Actions or GitLab CI file when the CI service only says the YAML is invalid.

  • Catching duplicate keys

    Detect a key defined twice in the same mapping, which most tools resolve silently by keeping the last one.

  • Consistent style before review

    Re-indent a hand-edited config so the pull request diff shows real changes only.

Tips and limitations

  • Inline comments are kept, but the spacing before the # is normalised to one space, so column-aligned comments lose their alignment.
  • A file that holds only comments formats to an empty result, because there is no document to print.
  • Long strings are not re-wrapped. Line length is left as you wrote it.
  • Flow collections such as [1, 2] stay in flow style, printed with spaces inside the brackets.
  • Validation here is syntax only. A schema check (for example kubeconform for Kubernetes) is still needed to catch a misspelled field name.

Sources and further reading

  1. YAML Ain't Markup Language (YAML) revision 1.2.2, YAML Language Development Team. Indentation rules, the ban on tab indentation and unique mapping keys
  2. Boolean Language-Independent Type for YAML Version 1.1, YAML Language Development Team. The 1.1 rule that makes yes, no, on and off booleans
  3. YAML, eemeli/yaml project. Documentation for the YAML 1.2 JavaScript library this tool uses

Frequently asked questions

How do I fix "bad indentation of a mapping entry" in YAML?

That error means a key does not line up with its siblings, usually because one line has an extra or missing space or a tab. Paste the file here and click Format YAML: the error message gives the exact line and column, and once fixed the output shows the nesting the parser sees.

Does the YAML Beautifier keep my comments?

It does. Full-line and inline comments are carried through to the output. The one change is that the gap before an inline # is reduced to a single space.

Can it format a file with several YAML documents?

Each document separated by --- is formatted and the separators are kept, so a multi-resource Kubernetes file comes back in the same order with the same boundaries.

Are anchors and aliases expanded when formatting?

No. &anchor and *alias references are printed as written, and merge keys (<<: *defaults) stay as merge keys. The tool changes layout, not the data model.

Why does the formatter reject my file for a duplicate key?

The YAML spec says mapping keys must be unique. Many parsers accept duplicates and keep the last value, which hides mistakes. This tool reports the second occurrence so you can decide which value you meant.

Should I use tabs or spaces in YAML?

Spaces only. YAML forbids tab characters for indentation. Two spaces per level is the most common convention and is what this formatter outputs.

Is there an in-depth guide to the YAML Beautifier?

Yes. “Formatting HTML, XML and YAML Without Changing What They Mean” covers the background, how it works, common pitfalls and worked examples. Read the full guide.