What this does
Re-indents a YAML file to a consistent, chosen indent size, with comments kept exactly where they were — this re-serializes through the parser's own document model rather than converting to plain data and back, which is what preserves comments that a naive parse-and-re-dump approach would silently drop.
Multi-document files (separated by a line containing just ---) are formatted document by document and reassembled with the same separators. A file that fails to parse is reported with the error rather than formatted into something that merely looks plausible.
FAQ
Does this reorder or sort keys?
No — key order is left exactly as written. Reordering risks changing meaning in some YAML consumers (a few tools treat first-seen keys specially in edge cases), so this only touches whitespace and line wrapping.
Why would I want line wrapping instead of "no wrapping"?
Wrapping keeps long scalar values from running off the side of an editor or diff view; "no wrapping" keeps each logical value on one line, which is easier to grep or diff cleanly. Neither changes the parsed value — both round-trip to the same data.
Does this validate the YAML too?
It validates enough to format — syntax errors, duplicate keys, and unresolved aliases are all reported instead of silently formatting broken YAML. For the fuller set of checks (tab indentation as its own explicit note, the YAML 1.1/1.2 boolean ambiguity), see YAML Validator.
Try it yourself
Validate first with YAML Validator. For Kubernetes manifests specifically — including conventional field ordering — see Kubernetes Manifest Formatter.