Micru Logo

YAML Formatter

Instantly format and beautify your YAML. Choose your indentation style and get clean, readable output in one click.

All processing is done locally in your browser. No data is sent to our servers.

What is YAML Formatting?

YAML (YAML Ain't Markup Language) is a human-readable data serialisation format widely used for configuration files, CI/CD pipelines, and infrastructure-as-code. Formatting - also called pretty-printing or beautifying - re-serialises a YAML document with consistent indentation and line breaks while leaving the data content completely unchanged.

Poorly indented or machine-generated YAML is difficult to read and prone to silent misinterpretation: a single stray space can change a scalar value into a nested mapping. Formatting normalises indentation across the entire document, making structure immediately visible and preventing hard-to-diagnose indentation errors.

How the Formatter Works

The formatter parses your input using js-yaml, a strict YAML 1.2 parser, which catches any syntax errors before a single character of output is written. It then re-serialises the in-memory document with the following rules:

  1. Mappings are emitted with each key-value pair on its own line, indented by the chosen indent unit times its nesting depth.
  2. Sequences are emitted with each item on its own line, preceded by a - marker at the correct indent level.
  3. Multi-line strings are preserved using block scalars (| literal or > folded) where appropriate.
  4. Special characters in string values are quoted automatically to ensure valid YAML output.
  5. Trailing whitespace and redundant blank lines between keys are removed.

Indentation Options

2 Spaces

The de facto standard for YAML. Used by Kubernetes manifests, GitHub Actions workflows, and most modern tooling. Keeps deeply nested documents compact enough to read without horizontal scrolling.

4 Spaces

Preferred in some Python projects and Ansible playbooks where alignment with Python conventions matters. Makes nesting levels more visually distinct in shallower documents.

Tabs

Note: the YAML 1.2 specification explicitly forbids tab characters for indentation. While some parsers tolerate tabs, using them produces technically invalid YAML that may fail in strict environments like Kubernetes or GitHub Actions.

Common Use Cases

Kubernetes Manifests

Kubernetes deployment, service, and ingress files can grow complex quickly. Reformatting them restores consistent indentation after manual edits or merge conflicts, reducing the risk of kubectl apply failures caused by indentation drift.

GitHub Actions & CI Pipelines

Workflow YAML files are sensitive to indentation: a job step indented at the wrong level silently becomes a mapping key rather than a step object. Formatting catches these shifts before the pipeline runs.

Docker Compose

Multi-service docker-compose.yml files edited by multiple developers often accumulate inconsistent indentation. Reformat before committing to produce a clean diff and avoid compose parse errors.

Ansible Playbooks

Ansible tasks, vars, and role defaults are all YAML. Formatting ensures consistent style across roles and prevents subtle bugs where a task list item is misread as a mapping due to indentation inconsistency.

YAML Indentation is Semantic

Unlike JSON (which uses braces and brackets) or XML (which uses closing tags), YAML uses indentation alone to express hierarchy. This means a formatting error is also a semantic error: moving a key two spaces to the right changes its parent node and therefore its meaning in the document.

For example, in a Kubernetes manifest, a containers list item indented under spec is valid; the same item indented under metadata produces a schema error. Consistent formatting enforced by a formatter - rather than maintained by hand - is the only reliable defence against this class of bug.