YAMLYAML
YAML Formatter
Pretty-print messy YAML — and keep every comment.
Paste your YAML here, or drop a file anywhere on the page
YAML appears here as you type
Free and unlimited · No sign-up · Indentation and spacing normalized — every comment you wrote is kept.
What gets normalized
- Indentation — every level is rewritten to two or four spaces, whatever mix the input had, and a sequence nested under a key is indented beneath that key.
- Spacing — one space after a colon and after a list dash, one space before an inline comment, and runs of blank lines collapsed to a single blank line.
- Line endings and trailing whitespace — Windows line endings become LF and spaces at the end of a line are removed.
- Flow collections —
[80, 443]becomes[ 80, 443 ]; they stay inline rather than being expanded into block style. - Plain numbers under YAML 1.2 — printed in canonical form, so
022becomes22,+1becomes1and1e3becomes1e+3. In YAML 1.1 mode leading-zero octals such as0644are kept. - Folded scalars (
>) — re-flowed onto one line per paragraph, with paragraph breaks preserved. - Key order — only if you turn on Sort keys; otherwise keys stay where you put them.
What stays exactly as written
- Every comment — on its own line, after a value, or as a block between sections.
- Quotes — single-quoted stays single-quoted, double stays double, bare stays bare.
- Anchors, aliases and merge keys —
&defaults,*defaultsand<<:are kept, not expanded. - Literal block scalars (
|) — line for line, including trailing newlines. - Sequence order, always, and key order unless you sort.
- Document separators (
---) and%YAMLdirectives in multi-document files. - Long lines — nothing is wrapped, so a long URL or command stays on one line.
- Empty values,
~,nulland booleans as you spelled them:TruestaysTrue.
Why most formatters delete your comments
Nearly every online YAML tool works the same way: parse the document into plain data, then print that data back out. Comments aren’t data, so they never survive the round trip. Configuration files are often more comment than configuration, and losing that’s losing the reasoning behind the values.
This page uses a different parser from the rest of the site — one that keeps a concrete syntax tree, so each comment stays attached to the line it documents. That is also why the formatter is a little stricter about syntax: it has to understand the document’s shape precisely enough to rebuild it faithfully.
Comments and structure both survive
Indentation is normalized to your chosen width; the commentary stays where you put it.
Before
# Production cluster
server:
host: 0.0.0.0
port: 8080 # internal only
features:
- fast
- privateAfter
# Production cluster
server:
host: 0.0.0.0
port: 8080 # internal only
features:
- fast
- privateMore before and after
Every example on this page reformats exactly like this in the tool above, with the default two-space indent.
Anchors, aliases and block scalars stay as written
A Docker Compose fragment with four-space indentation. The anchor, the merge key and the literal block are untouched; only the indentation changes.
Before
x-defaults: &defaults
restart: always
logging: json
services:
web:
<<: *defaults
image: nginx
command: |
sh -c "echo starting"
exec nginxAfter
x-defaults: &defaults
restart: always
logging: json
services:
web:
<<: *defaults
image: nginx
command: |
sh -c "echo starting"
exec nginxSorted keys for reviewable diffs
The same file with Sort keys turned on. Mapping keys are ordered at every level, the sequence keeps its order, and the comment moves with the key it describes.
Before
replicas: 3
name: api
# Ports the service exposes
ports:
- 443
- 80
image: api:1.2After
image: api:1.2
name: api
# Ports the service exposes
ports:
- 443
- 80
replicas: 3Formatter, validator, or converter?
All three run the same engine in your browser. Which one to reach for depends on where the file is going next.
Use the formatter when the file stays YAML
A Compose file, a Helm values file or a CI pipeline that several people have edited. You want consistent indentation and you want to keep the comments that explain the values. That is the one thing a converter round trip cannot give you.
Use the validator when something refuses to parse
The YAML validator is the same engine with the output pane focused on diagnostics: every error carries the line, the column, a plain-English cause and a fix, and every value that YAML 1.1 and 1.2 read differently is flagged.
Use the converter when the file is going somewhere that speaks JSON
API request bodies, test fixtures, a jq pipeline. The YAML to JSON converter drops comments — JSON has no syntax for them — and tells you how many it dropped, expands anchors, and can emit a multi-document stream as an array or as JSON Lines.
Frequently asked questions
Does this YAML formatter keep comments?
Yes. The formatter parses your document with a parser that keeps a concrete syntax tree, so comments on their own line, comments after a value, and comment blocks between sections all come out where they went in. If you turn on key sorting, a comment stays attached to the key it sits above. Most online formatters parse YAML into plain data and print it back, which is why they lose every comment; the JSON converter on this site does the same, unavoidably, and tells you how many were dropped.
Does formatting change my data?
Structure and text are untouched: quotes stay exactly as you wrote them, anchors, aliases and merge keys stay in place, block scalars keep their content, and key order is kept unless you ask for sorting. Two things are normalised. Plain numbers are printed in canonical form under YAML 1.2, so 022 becomes 22, +1 becomes 1 and 1e3 becomes 1e+3; switch the version to 1.1 and leading-zero octals such as 0644 are kept. Folded scalars (>) are re-flowed, with paragraph breaks preserved. If a value has to keep its exact spelling, quote it — the warnings under the output list every unquoted value that YAML 1.1 and 1.2 read differently.
What indentation does the formatter use?
Two spaces by default, or four if you choose it. Every level of the document gets the same width no matter how the input was indented, and a sequence nested under a key is indented beneath that key. YAML forbids tabs for indentation, so there is no tab option, and a document indented with tabs is reported as an error with the line number rather than reformatted.
Can it sort keys?
Yes. The Sort keys option orders the keys of every mapping alphabetically, at every level of nesting, which makes diffs and code review easier for files that several people or tools edit. Sequences keep their order, because order in a list is meaningful. Comments move with the key they belong to. Sorting is off by default: key order carries no meaning in YAML, but it often carries meaning for the people reading the file.
Does it handle multi-document YAML?
Yes. A file that holds several documents separated by --- is reformatted document by document and the separators are kept, so a Kubernetes manifest with a Deployment, a Service and an Ingress stays one file. A leading --- and a %YAML directive are kept as well, and the status line tells you how many documents were reformatted.
Why does the formatter reject YAML that the converter accepts?
The formatter uses a stricter parser than the converter, because it has to understand the exact shape of the document to rebuild it with the comments in place. A document that parses in the converter but fails here is reported with the line, a plain-English explanation and a suggested fix, the same as in the YAML validator. The common YAML errors guide walks through the six mistakes behind almost every failed parse.
Is this the same as yamllint or Prettier?
No, and they combine well. yamllint reports style and syntax problems but does not rewrite the file; this formatter rewrites indentation and spacing but does not enforce rules such as line length. Prettier’s YAML output is close to what you get here, and if your project already runs it there is no need for this page — it exists for the file in your clipboard right now, with nothing to install and nothing uploaded.
Is my YAML uploaded when I format it?
No. The formatter is JavaScript that runs in your browser tab. The yaml library loads with the page, and there is no server endpoint to send a document to; you can load the page, disconnect from the internet and keep formatting. That matters for the files people usually want tidied — Kubernetes manifests, Docker Compose files and CI pipelines carry hostnames, registries and the occasional secret.
How do I format YAML from the command line?
yq -i . config.yaml reformats a file in place and keeps comments; drop -i to print the result instead. With Prettier installed, npx prettier --write "**/*.{yml,yaml}" formats every YAML file in a project. In Python, ruamel.yaml in round-trip mode reformats while preserving comments, which PyYAML cannot do. For a one-off file, paste it into the formatter above.
If the formatter reports an error you don’t recognise, the guide Common YAML errors and how to fix them covers the six mistakes behind almost every failed parse, each with a before and after.