How to Format YAML
YAML Formatting
When to Format YAML
Format YAML from Kubernetes manifests, GitHub Actions workflows, Docker Compose files, and Ansible playbooks. Consistent indentation makes diffs readable and errors easier to spot.
YAML vs JSON
YAML supports comments, multi-line strings, and anchors. It is human-friendly but indentation-sensitive — a single wrong space can break parsing. Always validate after formatting.
Common Mistakes
- Using tabs for indentation — YAML requires spaces
- Expecting comments to survive re-formatting — they may be stripped
- Formatting invalid YAML — fix syntax errors before pretty-printing
Developer Tips
- Run YAML Validate after formatting before git commit
- Keep Kubernetes manifests at 2-space indent for ecosystem consistency
- Convert to JSON if you need programmatic diffing in CI
Frequently asked questions
Will formatting change my YAML semantics?
Formatting re-parses and re-serializes the document. Comments and custom tags may be lost. Review the output before committing to version control.
What indentation does the formatter use?
Output uses 2-space indentation, the most common YAML convention.