FormatterHQ

Pretty Print YAML in the Browser, CLI and Code

Pretty print YAML here to preview the result, then reproduce it with yq, PyYAML or js-yaml using the dump options that control block style, key order and indentation.

Input YAML
Output YAML
Ready.

How to use the YAML Pretty Print

  1. Paste YAML, upload a .yaml or .yml file, or click Sample.
  2. Choose 2 or 4 spaces in Indent to match the indent setting you plan to use in code.
  3. Leave Sort keys (drops comments) off for a comment-preserving layout, or tick it to preview a serializer-style block dump.
  4. Click Format and compare the result with the output of your yq, PyYAML or js-yaml call.
  5. Copy or download the pretty printed YAML.

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=False forces block style everywhere; it has been the default since PyYAML 5.1, but older code often passes None, which writes the innermost collections in flow style.
  • sort_keys defaults to True, so keys come out alphabetical unless you switch it off.
  • indent applies to mappings. List dashes are written at the same column as their parent key, a valid but different layout from this page's.
  • width controls 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 }));
  • indent is the number of spaces per level; sortKeys accepts true or a compare function.
  • lineWidth: -1 turns off folding of long strings (the default is 80).
  • noRefs: true writes repeated objects out in full instead of generating anchors and aliases.
  • flowLevel chooses where flow style starts: -1 is block everywhere, 1 puts everything below the top level in flow style.

Which approach keeps what

ApproachCommentsStyleKey order
This page, default modeKeptYour mix of block and flowOriginal
This page, Sort keysRemovedBlockAlphabetical
yq -PGenerally keptBlockOriginal
PyYAML safe_dumpRemovedBlock with default_flow_style=FalseAlphabetical unless sort_keys=False
js-yaml dumpRemovedBlock unless flowLevel is setOriginal 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.

Frequently asked questions

How do I pretty print YAML with yq?
With the Go implementation of yq version 4, run yq -P '.' file.yaml. Add -I 4 for four-space indentation and -i to rewrite the file in place.
Why does PyYAML sort my keys?
yaml.dump and yaml.safe_dump sort mapping keys by default. Pass sort_keys=False to keep the order in which the keys were loaded.
How do I stop PyYAML from writing lists as [a, b]?
Pass default_flow_style=False. It is already the default in PyYAML 5.1 and later, so inline lists usually mean the code, or an old library version, sets it to None.
Can this page expand flow style without sorting keys?
No. The default mode keeps flow collections as flow, and only the Sort keys mode rebuilds everything in block style. To expand without sorting, use yq -P or a js-yaml dump without sortKeys.
Why are comments missing after dumping YAML in code?
Loading YAML into dictionaries and lists keeps only the data, so a dump cannot write comments back. Use a round-trip tool, such as this page's default mode or yq, when comments matter.

Last updated October 3, 2026

Related tools

CSS Pretty Print Opinionated CSS pretty print with Prettier: normalises case, leading zeros, quotes and semicolons so a team's stylesheets look alike. HTML Pretty Print Opinionated HTML pretty print with Prettier: closes implied tags, enforces a line width and gives the same output for any input layout. JavaScript Pretty Print Opinionated JavaScript pretty print with Prettier: identical output for any input layout, with indent, width, semicolon and quote options. JSON Pretty Print Pretty print JSON online with a 4-space default, 2 or 3 spaces or tabs, and see the matching JavaScript, Python, PHP and jq commands. XML Pretty Print Pretty print XML in the browser, with matching commands for xmllint, xmlstarlet, Python and .NET when it belongs in a script. YAML Validator Validate YAML syntax online. Catch bad indentation, duplicate keys and invalid values with exact line and column numbers. YAML Viewer View YAML as an interactive tree and table. Explore nested maps and lists and see how every value is parsed. YAML Beautifier Clean up messy Kubernetes, Compose and CI YAML: consistent indentation and spacing, with comments and anchors kept as written. YAML Editor Edit YAML online with syntax highlighting, live validation and a tree preview. Format and download your file when done. YAML Formatter Format YAML online with consistent indentation while keeping comments. Validate syntax and see the exact line of any error. YAML Parser Parse YAML online and see the resulting data as JSON and a tree. Check types, anchors and multi-document files. ActionScript Formatter Beautify ActionScript 3 code online with proper indentation.