Two ways to pretty print YAML
Every tool that can pretty print YAML falls into one of two families. Round-trip formatters work on the text: they fix indentation and spacing but keep comments, anchors, key order and your choice of flow or block style. Serializers load the data and dump it again: the layout is rebuilt from scratch in block style, and anything that is not data, such as comments, is gone. This page offers both, controlled by one checkbox, which makes it a quick way to preview what a script would produce before you write it.
It runs the same engine as the YAML Formatter. Use that page for the option reference and the list of parse errors; use this one when you are deciding how to pretty print YAML in a pipeline, a script or application code.
Block style versus flow style
Block style uses indentation and dashes; flow style packs the same data into braces and brackets. Both parse to identical values, as the YAML 1.2 specification describes, but block style is easier to read and to diff. Take this one-line job definition:
job: {name: nightly, schedule: "0 3 * * *", steps: [lint, test, build], env: {CI: true}}
In the default mode the braces stay. Because the line is longer than 80 characters, the mapping is split over several lines, still in flow style, with a trailing comma:
job:
{
name: nightly,
schedule: "0 3 * * *",
steps: [lint, test, build],
env: { CI: true },
}
With Sort keys (drops comments) ticked, the data is dumped again in block style with keys in alphabetical order, and quotes that are not needed are removed:
job:
env:
CI: true
name: nightly
schedule: 0 3 * * *
steps:
- lint
- test
- build
There is no setting on this page that expands flow style while keeping the original key order; for that, use one of the tools below.
Pretty print YAML with yq
The Go implementation of yq (version 4) has a dedicated flag. -P (--prettyPrint) resets every node to block style, -I sets the indent and -i rewrites the file in place:
yq -P '.' app.yaml
yq -P -I 4 '.' app.yaml
yq -i -P '.' app.yaml
yq -P 'sort_keys(..)' app.yaml
yq works on a node tree rather than plain data, so it generally keeps comments and does not reorder keys unless you ask with sort_keys.
PyYAML dump options
import yaml
with open("app.yaml") as f:
data = yaml.safe_load(f)
print(yaml.safe_dump(data, default_flow_style=False, sort_keys=False,
indent=2, allow_unicode=True, width=1000))
default_flow_style=Falseforces block style everywhere; it has been the default since PyYAML 5.1, but older code often passesNone, which writes the innermost collections in flow style.sort_keysdefaults toTrue, so keys come out alphabetical unless you switch it off.indentapplies to mappings. List dashes are written at the same column as their parent key, a valid but different layout from this page's.widthcontrols line wrapping of long strings; a large value keeps them on one line. Comments are always lost, because the data model does not contain them.
js-yaml dump options
The Sort keys mode on this page is built on a js-yaml dump. The page additionally applies merge keys (<<) and keeps the original text of numbers such as 3.10 and 0755, which a plain load and dump writes as 3.1 and 755. Apart from that, the following call reproduces it in Node.js:
import fs from 'node:fs';
import { load, dump } from 'js-yaml';
const data = load(fs.readFileSync('app.yaml', 'utf8'));
console.log(dump(data, { indent: 2, sortKeys: true, lineWidth: -1, noRefs: true }));
indentis the number of spaces per level;sortKeysacceptstrueor a compare function.lineWidth: -1turns off folding of long strings (the default is 80).noRefs: truewrites repeated objects out in full instead of generating anchors and aliases.flowLevelchooses where flow style starts:-1is block everywhere,1puts everything below the top level in flow style.
Which approach keeps what
| Approach | Comments | Style | Key order |
|---|---|---|---|
| This page, default mode | Kept | Your mix of block and flow | Original |
| This page, Sort keys | Removed | Block | Alphabetical |
| yq -P | Generally kept | Block | Original |
| PyYAML safe_dump | Removed | Block with default_flow_style=False | Alphabetical unless sort_keys=False |
| js-yaml dump | Removed | Block unless flowLevel is set | Original unless sortKeys is set |
Before trusting a dumped file, check that the data survived: the YAML validator shows the parsed values as JSON, and the YAML viewer lets you browse large documents as a tree. If your data starts life as JSON, JSON to YAML writes block-style YAML directly. For a broader look at the format itself, see JSON vs YAML vs TOML.