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 intitle: 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.