What is YAML Formatting?
YAML formatting means taking YAML data — often with mixed indentation, inconsistent spacing, or pasted from different sources — and normalizing it into a clean, readable structure with uniform indent levels. Unlike JSON, where whitespace is cosmetic, YAML uses indentation to define its structure. A YAML formatter re-parses the data and re-serializes it with consistent spacing, making it easier to read, review in pull requests, and maintain over time.
Messy YAML is more than an aesthetic problem. Inconsistent indentation causes subtle bugs: a key that looks nested under one parent might actually belong to another if the indent is off by a single space. Running YAML through a formatter catches these structural issues by producing output where the visual layout matches the logical structure.
How to Format YAML
- Paste your YAML into the input area — a Kubernetes manifest, Docker Compose file, CI pipeline config, or any YAML document.
- Choose your preferred indentation in settings (2 spaces or 4 spaces).
- Click “Format” or press
Ctrl+Enter. - Copy the formatted result with the “Copy” button or
Ctrl+Shift+C.
The formatter parses your YAML into a data structure and re-serializes it with clean, uniform indentation. Optionally enable “Sort keys” to alphabetize object keys at every level — useful for keeping configuration files deterministic and diff-friendly.
Compact Mode: Strip Comments and Blank Lines
The Compact button does the opposite of beautifying: it removes clutter without re-serializing your document. Click it to strip every full-line and inline # comment, collapse runs of blank lines into a single separator, and trim trailing whitespace — while leaving your existing indentation and key order untouched.
This is the right choice when you want a smaller, noise-free config to paste into a ticket, a chat message, or a kubectl apply -f - command, but you don’t want the formatter to rewrite the whole file. Where Format rebuilds the document from a parsed data structure (and therefore drops comments as a side effect), Compact is a lightweight cleanup that keeps the file recognizably yours.
Common YAML Formatting Problems
Mixed Indentation from Copy-Paste
Copying YAML snippets from documentation, Stack Overflow, or different files often produces a document with mixed 2-space and 4-space indentation. This is syntactically valid (YAML allows any consistent indent per level), but it makes the file hard to read and error-prone to edit. The formatter normalizes everything to your chosen indent size.
Tabs Mixed with Spaces
YAML forbids tab characters for indentation — this is the single most common YAML error. Tabs can sneak in from editors configured for other languages or from pasting terminal output. The formatter eliminates this by re-serializing with spaces only.
Deep Nesting with Inconsistent Levels
Kubernetes manifests and Helm charts routinely nest 5-8 levels deep. When different contributors use different indent widths at different levels, the result is a file that’s technically valid but visually chaotic:
spec:
template:
spec:
containers:
- name: app
image: app:1.0
After formatting with 2-space indent:
spec:
template:
spec:
containers:
- name: app
image: app:1.0
Flow Style Mixed with Block Style
YAML supports both block style (indented, multi-line) and flow style (inline, JSON-like). Mixing the two in a single file is valid but hurts readability. The formatter converts everything to block style for consistency.
YAML Gotchas That Silently Break Configs
Some YAML problems aren’t about indentation at all — they’re about how YAML interprets bare, unquoted values. These are the bugs that pass every “is it valid YAML?” check and still ship the wrong config.
The Norway Problem
The most infamous YAML gotcha: unquoted no, yes, true, and false are parsed as booleans, not strings. Write a list of country codes and NO (Norway) silently becomes false:
countries:
- US
- GB
- NO # parsed as the boolean false, not "NO"
Run it through the formatter and the value normalizes to false, making the bug obvious. The fix is to quote it: - "NO". The same trap catches any string that merely looks like a boolean.
Version Numbers and Leading Zeros
Unquoted version: 1.10 is read as the number 1.1 — the trailing zero vanishes. A build number or ZIP code like 08544 can be treated as a number and lose its leading zero. Always quote values that are semantically strings: version: "1.10", zip: "08544".
Duplicate Keys
YAML doesn’t error on duplicate keys in the same mapping — the last one silently wins. If a config has replicas: 2 near the top and replicas: 5 fifty lines down, only 5 survives after parsing. Formatting collapses the duplicates so the winner is visible; review the diff to confirm it’s the value you intended.
“mapping values are not allowed here”
This cryptic parser error almost always means an unquoted value contains a colon — a URL (url: http://example.com), a timestamp (time: 12:00), or a Windows path. Wrap the value in quotes and the error disappears. This formatter auto-quotes ambiguous values for you, so pasting the broken snippet and formatting it is often the fastest way to spot and fix it.
Formatting YAML for DevOps
Kubernetes Manifests
Kubernetes YAML files are where formatting matters most. A Deployment, Service, or ConfigMap with inconsistent indentation is harder to review, easier to misread, and more likely to cause an accidental field relocation during editing. Format your manifests before committing — especially multi-document files separated by ---.
Docker Compose
docker-compose.yml files grow quickly as services multiply. Formatting keeps the services, volumes, networks, and environment sections visually aligned, making it obvious which keys belong to which service.
CI/CD Pipelines
.gitlab-ci.yml, .github/workflows/*.yml, and .circleci/config.yml are edited frequently and often by multiple people. Formatting before committing reduces diff noise in pull requests and ensures the file stays consistent even as the team grows.
Ansible Playbooks
Ansible playbooks and roles are deeply nested YAML that benefits heavily from consistent formatting. A formatter turns a messy playbook into one where tasks, handlers, and vars are immediately scannable.
Block Scalars: Multi-Line Strings in YAML
Long strings — a shell script in a CI job, a certificate block, an embedded config — use block scalars, and formatting them wrong is a common source of broken pipelines. YAML has two styles:
- Literal (
|) preserves every newline exactly as written. Use it for scripts and anything where line breaks matter. - Folded (
>) joins lines with spaces into a single flowing paragraph, collapsing the newlines. Use it for long prose that should wrap.
Each can take a chomping indicator that controls the trailing newline: - strips it (|-), + keeps every trailing blank line (|+), and the bare form keeps exactly one. The formatter parses these blocks, preserves the literal-vs-folded distinction, and re-indents the body under its parent key so the block stays valid:
script: |
set -e
npm ci
npm test
If a multi-line block “disappears” or merges into the wrong key after editing, it’s almost always an indentation problem — the block body must be indented further than the key that introduces it. Running it through the formatter re-establishes that relationship.
YAML Formatter vs YAML Validator
These tools solve different problems and work best together:
| YAML Formatter | YAML Validator | |
|---|---|---|
| Purpose | Clean up indentation and style | Check for syntax errors |
| Input | Valid YAML (or close to it) | Any text that should be YAML |
| Output | Re-formatted YAML | Error report or “valid” |
| Comments | Stripped (data is re-serialized) | Preserved (text is analyzed) |
| Best for | Making YAML readable and consistent | Finding bugs before deploy |
Workflow: validate first to catch syntax errors, then format to normalize style. Our YAML Validator handles the first step.
YAML Formatting Best Practices
Pick One Indent Size and Enforce It
Two spaces is the community standard. Set it in your editor, your .editorconfig, and your CI linter. Don’t mix indent sizes across files in the same project.
Sort Keys for Deterministic Output
Alphabetically sorted keys produce smaller, cleaner diffs when configurations change. This is especially valuable for values.yaml in Helm and docker-compose.yml — two files that change frequently and are reviewed by multiple people.
Format Before Committing
Add YAML formatting to your pre-commit hook or CI pipeline. Tools like Prettier (with prettier-plugin-yaml), yamlfmt (Google’s Go-based formatter), or this online tool for quick one-off checks ensure every committed YAML file meets the same standard.
Use Block Style Over Flow Style
Block style (one key-value pair per line, indented) is more readable and produces better diffs than flow style (inline {key: value}). Reserve flow style for very short, simple values like ports: [80, 443].