Skip to main content

YAML Formatter

Run Format to see the result here.

Format YAML with consistent indentation while keeping comments and values.

What it does

YAML Formatter rewrites YAML with consistent indentation and spacing, so it is easier to read and to compare. It changes how the text is laid out, not what it means: keys, values and comments are kept.

Every result is checked before you see it. The tool reads the formatted text back and compares its structure and its comments, both their text and where they sit, with your input, and if they differ it refuses instead of giving you text that could mean something else.

What it supports

  • YAML read under the YAML 1.2 Core rules, as one document or several separated by ---: mappings, lists, comments, anchors and aliases, multi-line text blocks (| and >) and the standard YAML tags such as !!str.
  • With several documents, every document is formatted and the separators are kept. Comments around a separator stay on their side of it, and a document that is empty stays empty. Each document has its own anchors, so an alias cannot point to an anchor in another document, and the same anchor name can be used again in the next document.
  • Indentation of 2 or 4 spaces, never tabs. Line breaks in the result are always the same kind, whatever your text used.
  • Comments are kept, including comments at the end of a line. A run of blank lines becomes one blank line, and the spacing before a comment is made even.
  • Values keep their meaning: numbers such as 0o17, 0x1F, 1e400, 9007199254740993 and -0 are written exactly as you wrote them, text is not turned into a number, a date or a yes/no value, and multi-line text blocks keep their line breaks.
  • The quote style can change: text in single quotes is usually written in double quotes. The text itself does not change.
  • Anchors and aliases are kept. When one value has both an anchor and a tag, the tool may swap their order.
  • The merge key << is kept as an ordinary key, with a warning, because YAML 1.2 Core has no merge keys.

Good to know

  • These are refused, with no result: a key repeated in one mapping; a tag that is not a standard YAML tag, or a standard tag that does not fit its value, such as !!int abc; a key that is not text (a number, true, false, null, an empty key, or a key written as a list or mapping); and a directive such as %YAML or %TAG. Syntax errors are refused too, in any document, with their line and column counted from the start of the whole text. The tool never repairs YAML.
  • Safety check: after formatting, the tool reads the result back and compares its structure and comments, including where each comment sits, with your input. If anything differs, it refuses with a note that formatting could change the meaning. Typical causes are a tab at the end of a line inside a multi-line text block (| or >), a number after the block marker (such as |2) that the chosen indentation would break, and a comment inside [ ] or { } that formatting would have to move out of them, whether it sits alone on a line between the brackets or on the same line as the opening bracket. Fixing that part (move such a comment outside the brackets), or choosing 2 spaces, usually helps.
  • A text with only comments or blank lines has nothing to format, so you get a note asking for content.
  • Large files can be slow to format, and files with many comments are slower still. If a run is stopped at the time limit, try a smaller input.
  • If indentation would make the result far larger than the size limit, the tool stops before formatting. Use 2 spaces or less nesting.
  • The limits count the whole text: the size limits add up all the documents, while the nesting limit applies to each document on its own.

How to use it

  1. Paste or type your YAML in the input box. It can hold one document or several, separated by ---. Nothing runs while you type.
  2. Choose the indentation (“2 spaces” or “4 spaces”) and press “Format”.
  3. Read the result next to your input. If your YAML is refused, the message says why and, when it can, where.
  4. Press “Copy” to put the result on your clipboard, or “Download” to save it as formatted.yaml.

Examples

Tidy a messy file

Paste this YAML and press “Format” with “2 spaces” selected. Both comments are kept. The extra spaces go, the list items get the chosen indentation, the single quotes around two become double quotes (the text is still two), three blank lines become one, and the spacing inside { } is made even.

# settings
name:    demo   # the name
items:
      -   one
      -   'two'



flags: {a: 1,   b: 2}
# settings
name: demo # the name
items:
  - one
  - "two"

flags: { a: 1, b: 2 }

A repeated key

The key “port” appears twice in the same mapping, which YAML does not allow. The tool never chooses one of the two values for you, so it refuses and points at the repeat.

port: 80
port: 8080

A key is repeated in the same mapping. YAML requires the keys of a mapping to be unique. Remove or rename the repeated key.

Line 2, column 1

More than one document

Paste this YAML and press “Format”. The two documents are separated by ---. Each one is formatted on its own: the separator stays between them, both comments stay in their document, and the extra spaces and the indentation are fixed in both.

# first
name:    one
---
# second
name:   two
items:
      - a
# first
name: one
---
# second
name: two
items:
  - a

A custom tag

The tag !money is not one of the standard YAML tags. YAML Validator accepts this text with a warning, but YAML Formatter supports only the standard tags, so it refuses instead of risking a change to your data.

price: !money 12.50

This YAML uses a tag this tool does not support: a custom or non-Core tag, or a Core tag (such as !!int) that does not fit its value. The syntax is valid, but the YAML Formatter and Viewer support only the YAML 1.2 Core tags, so this tool refused instead of changing or hiding your data.

Line 1, column 8

Valid YAML that is not safe to reformat

The only thing between the brackets is a comment on its own line. The text is valid YAML, but formatting would have to move that comment out of the brackets, which changes where it sits, so the tool refuses and leaves your text as it is. A comment on the same line as the opening bracket, as in ports: [ # note, is refused for the same reason.

a: [
  # c
]

This YAML is valid, but formatting it could change its meaning, so the tool refused instead of risking your data. Typical causes are a tab at the end of a line inside a multi-line text block (| or >), a number after the block marker (such as |2) that the chosen indentation would break, or a comment inside [ ] or { } that formatting would have to move out of them. Fix that part or choose 2 spaces and try again.

Limits and privacy

Size limits

  • Input: up to 2,000,000 bytes of UTF-8 text (about 2 MB). Accented letters and emoji take more than one byte each. Larger input is rejected.
  • Result: also up to 2,000,000 bytes. If the complete result would be larger, the tool shows only an incomplete preview of the first 100,000 bytes, and that preview cannot be copied or downloaded.
  • Nesting: up to 256 levels of mappings and lists inside each other. Deeper YAML is refused because it is beyond what the tool processes.
  • Time: a run that takes longer than its time limit is stopped. Try a smaller input.

Your privacy

The text you type or paste, or open from a local file, is processed locally in your browser, in a dedicated Web Worker. It is never uploaded or sent to the server (a file you open is read in your browser only), and it is not written to storage, cookies or the address bar.

“Copy” puts the result on your clipboard and “Download” saves it as a file, but only when you press the button. The file is created in your browser, so nothing is uploaded.

For the full details, see the Privacy Policy

Related tools and pages

Your text stays in the page when you switch tools in the same tab, so you can try the same text in another tool. Reloading or closing the tab ends it.

All tools