FormatterHQ

YAML Formatter

Normalize indentation and spacing in YAML files without losing comments. Errors are reported with line and column, and everything runs in your browser.

Input YAML
Output YAML
Ready.

How to use the YAML Formatter

  1. Paste YAML into the input editor, upload a .yaml or .yml file, use Load URL, or click Sample.
  2. Choose an Indent of 2 or 4 spaces. Leave Sort keys (drops comments) unticked if you want to keep comments.
  3. Click Format. Use Validate to check the file only, or To JSON to see the parsed data as JSON.
  4. If the status bar shows an error, fix the line and column it names and format again.
  5. Copy the result or use Download to save it.

A YAML formatter that keeps your comments

This YAML formatter rewrites a YAML document with consistent indentation and spacing while leaving its meaning untouched. It is built for the files people edit by hand every day: Kubernetes manifests, Docker Compose files, CI pipelines, Ansible playbooks and application settings. Comments stay where they are, block and flow styles are kept, and the document is parsed first, so a file that would fail in your deployment tool fails here too, with a line number.

Example

A settings file that grew by copy and paste often mixes 2- and 6-space nesting and uneven spacing after colons:

# Deployment settings for the inventory API
service:
    name:   inventory-api
    ports: [8080, 9090]
env:
  - {name: LOG_LEVEL, value: info}
limits:
      cpu: 500m
      memory: 512Mi

With the default 2-space indent, Format returns:

# Deployment settings for the inventory API
service:
  name: inventory-api
  ports: [8080, 9090]
env:
  - { name: LOG_LEVEL, value: info }
limits:
  cpu: 500m
  memory: 512Mi

The status bar confirms Formatted (comments preserved). Every nesting level now uses the same step, the extra spaces after name: are gone and flow mappings get a space inside the braces.

How the options work

  • Indent – 2 or 4 spaces per level. YAML forbids tab characters for indentation, so tabs are not offered.
  • Sort keys (drops comments) – rewrites the document from its parsed data with keys in alphabetical order. Because the data model has no place for comments, they are removed in this mode, flow collections are expanded to block style and strings that could be misread are quoted. Aliases are expanded into copies and merge keys (<<: *defaults) are applied, so the merged keys are written out in full, while numbers keep their original text (3.10, 0755). Use it when you want a canonical file for diffing, not when you want to keep annotations.
  • Validate – parses the file and shows the resulting data as JSON, which is handy for checking what a value really became.
  • To JSON – converts the YAML to JSON in one step.

Errors the formatter catches

Formatting starts with a full parse, so invalid YAML is never silently rewritten. Messages you are likely to see:

  • tab characters must not be used in indentation – replace tabs at the start of the line with spaces.
  • bad indentation of a mapping entry – a key is indented differently from its siblings, or a plain value contains : , as in title: Note: read me. Quote such values: title: "Note: read me".
  • duplicated mapping key – the same key appears twice in one mapping; most tools would silently keep only one of them.
  • end of the stream or a document separator is expected – usually text left over after a value, as in [1, 2] extra.

Things formatting will not change

Scalar values are preserved in the default mode, so "false" stays a string and 01234 is not rewritten. Quoted strings stay quoted, although single quotes are written as double quotes. That also means the formatter will not fix values that YAML interprets differently from what you intended, such as an unquoted version number 1.10, which a parser reads as the number 1.1. Run Validate and inspect the JSON if you are unsure how a value is typed.

Multi-document files separated by --- are supported and each document is formatted in place.

Related tools

To check a file without changing it, use the YAML validator. When you need the data in another format, the YAML to JSON converter gives you plain JSON, and the JSON to YAML converter goes the other way.

Privacy

The YAML is parsed and formatted in your browser; it is not uploaded while you work. Only Load URL (which fetches the file through our server) and Save & Share (which stores the content to create a link) send data to the server.

Format YAML in Python

With PyYAML, formatting means loading the document and dumping it again: yaml.safe_load parses it and raises yaml.YAMLError if it is invalid, and yaml.safe_dump writes it back with uniform indentation. Pass sort_keys=False, or the keys come back in alphabetical order. Be aware of what the round trip loses: comments are dropped, and inline collections such as [80, 443] are rewritten in block style.

# pip install pyyaml
import yaml

messy = """
# app settings
name:   demo
ports: [80, 443]
db: {host: localhost,   ssl: true}
"""
data = yaml.safe_load(messy)          # raises yaml.YAMLError on invalid YAML

formatted = yaml.safe_dump(data, sort_keys=False, indent=2, allow_unicode=True)
print(formatted)

The formatter above keeps comments and inline collections, so prefer it for files that people edit by hand.

Frequently asked questions

Does the YAML formatter remove comments?
No, not in the default mode. Comments, blank-line structure and quoting are kept. The Sort keys option removes comments, because it rebuilds the file from the parsed data. In the rare case that the comment-preserving formatter cannot read a valid file, the same rebuild is used and the status bar says that comments were dropped.
Can I use tabs to indent YAML?
No. The YAML specification does not allow tab characters for indentation, and parsers reject them with an error. Use 2 or 4 spaces; most editors can convert tabs to spaces automatically for .yaml files.
Why does my file fail with "bad indentation of a mapping entry"?
Either a key is not aligned with the other keys at the same level, or an unquoted value contains a colon followed by a space. Align the key or wrap the value in quotes.
Is formatting safe for Kubernetes and Docker Compose files?
In the default mode formatting only changes whitespace, line breaks in long flow collections and single quotes to double quotes, so the parsed data stays identical. The Sort keys mode re-serialises the data: comments are dropped and aliases and merge keys are written out in full, so the text changes more while the data stays the same. It is a good idea to review the diff before committing, as with any automated change.
What is the difference between .yaml and .yml?
None for the content. Both extensions are used for YAML files; .yaml is the extension recommended by the YAML project, while .yml is common in older tools. The formatter accepts either.

Last updated October 3, 2026

Guides

Related tools