DevToolsmith

YAML Formatter

Paste YAML to re-indent it with 2 or 4 spaces. Comments, anchors, block scalars and multiple documents are kept, numbers stay exactly as written, and the result is checked to contain the same data as the input.

Runs 100% in your browser — nothing is uploaded

YAML
Tip: drop a file here629 chars · 629 bytes · 27 lines
Formatted YAML
557 chars · 557 bytes · 27 lines
  • ✓ Same data as the input (2 documents)

Example

A Compose file with uneven indentation, a flow mapping and two YAML 1.1 traps:

Before
services:
   web:
      image: "nginx:1.27"   # pinned
      ports:
      - 22:22
      environment: {COUNTRY: NO, DEBUG: off}
      version: 1.10
After (expand, quote gotchas)
services:
  web:
    image: "nginx:1.27" # pinned
    ports:
      - "22:22"
    environment:
      COUNTRY: "NO"
      DEBUG: "off"
    version: 1.10
  • Indentation is now two spaces at every level, and the list sits under its key.
  • 22:22, NO and off are quoted: PyYAML reads them as 1342, false and false.
  • 1.10 is left as written. It is a number, so if you mean the version string, quote it yourself.
  • The comment and the quoted image name are unchanged.

What the options do

OptionEffect
IndentSpaces per nesting level.
QuotesRewrites strings that are already quoted with double or single quotes. Unquoted strings stay unquoted.
Indent listskey: followed by   - item instead of - item.
Sort keysAlphabetical keys at every level. Merge keys (<<) stay first.
Expand { } and [ ]Turns flow collections into block style, one item per line.
Wrap at 80Folds long strings over several lines. Off by default, because folded strings are harder to search.

Format YAML on the command line

# Prettier: formats in place, keeps comments
npx prettier --write "**/*.{yaml,yml}"

# yq: re-indent (comments are kept; numbers may be rewritten)
yq -i -P '.' config.yaml

# yamlfmt (Google): a formatter made for CI
yamlfmt config.yaml

To check for errors instead, use the YAML Validator; to check the structure against a schema, use the JSON Schema Validator, which accepts YAML too.

Frequently asked questions

Does the YAML formatter keep my comments?

Yes. Comments, blank lines between sections, anchors and aliases, tags, block scalars (| and >) and document separators (---) are all kept. In the rare case a comment sits somewhere it can’t be attached, such as between a key and its value, the tool tells you how many were dropped.

Will formatting change my data?

No. Numbers, booleans and nulls are written exactly as you typed them, so 0644 stays 0644 and 1.10 stays 1.10. After formatting, the output is parsed again and compared with the input; the tool shows “Same data as the input” only when they match.

Should YAML be indented with 2 or 4 spaces?

Two spaces is the convention for Kubernetes, Docker Compose, GitHub Actions and Ansible, and it is what kubectl and Prettier produce. YAML only requires that items at the same level line up; tabs are never allowed.

Should list items be indented under their key?

Both styles are valid YAML. Indented lists (key, then " - item") are what Prettier and most editors produce; non-indented lists ("- item" directly under the key) are common in Kubernetes docs. Pick one and keep it consistent; turn off Indent lists for the second style.

What does “Quote YAML 1.1 gotchas” do?

It puts quotes around plain values that YAML 1.1 parsers such as PyYAML read differently: NO, yes, on and off become booleans and 22:22 becomes the number 1342. Quoting keeps them strings everywhere. Numbers like 0644 are left alone, because quoting them would change the value for YAML 1.2 parsers.

Can I format a file with several documents?

Yes. Documents separated by --- are formatted one by one and kept in order, so a Kubernetes manifest with a Deployment and a Service works as expected.