Appearance

YAML Formatter

/yaml-formatter/

Indent

Related tools

Related posts

Comments

0/500

About this tool

Why YAML indentation goes wrong so often

YAML expresses structure through indentation instead of brackets, so a single space of difference is a difference in data structure. When several people edit a Kubernetes manifest, a GitHub Actions workflow or an application.yml, 2-space and 4-space indentation get mixed, and a pasted block that is off by one space can make a deployment fail. The file may look fine in an editor while the parser reads a completely different structure or throws an error.

Read moreShow less

This YAML formatter rewrites pasted YAML with consistent 2-space or 4-space indentation, and if there is a syntax error it reports the line and column. It is meant for tidying formatting before a review, or for finding the broken line in a configuration file that made a deployment fail.

How the YAML formatter rewrites while keeping the document structure

Formatting uses parseDocument from the open-source yaml library (2.9.0). Instead of extracting only values, this function builds a document structure that also holds comments, anchors and quoting style, and toString writes that structure back with the chosen indentation. The data itself is therefore the same before and after. No line length limit is applied, so long strings are not folded across lines.

What changes is the layout. A block indented with 3 spaces is re-indented to the chosen width, and list items under a key (lines starting with a dash) are indented one level deeper than the key. An inline mapping written as {host: localhost, port: 5432} stays on one line but gains spaces inside the braces, becoming { host: localhost, port: 5432 }. The gap between a value and a trailing comment is set to one space, and runs of blank lines shrink to one.

What stays the same is just as clear. Key order, comment text, the choice of single or double quotes, and anchors (&) and aliases (*) remain as written. Values such as yes or 1.10 are left exactly as typed, without added quotes or conversion to numbers. The principle is to polish the format without touching what the data means.

When to use a YAML formatter

The most common use is matching team conventions before committing YAML to a shared repository. Official Kubernetes and GitHub Actions examples mostly use 2 spaces, but teams may have their own rule, so look at existing files in the repository and choose accordingly. Comments are not removed, so it is safe for heavily annotated configuration files.

When a deployment pipeline stops on a YAML parse error, paste the whole file to see where the error is. If a formatted result appears, the syntax is fine, and the next suspects are things the tool cannot know about, such as misspelled key names or values of the wrong type. To see which type each value is read as, check the JSON output in the YAML to JSON Converter.

Common YAML errors and the formatter's limits

Several common errors point to their cause by their wording. Tab indentation gives 'Tabs are not allowed as indentation'; a key that is one space to the left of its siblings gives 'All mapping items must start at the same column', and one space to the right gives 'Nested mappings are not allowed in compact mappings'. A line missing the space after the colon, like c:1, is read as a plain scalar rather than a key and produces 'Implicit map keys need to be followed by map values'. The same key twice at one level gives 'Map keys must be unique'.

A string missing the space after a colon in a value position is not an error but simply a string. In b: key:value, the value of b is read as the string 'key:value', so the tool cannot catch every line written differently from what you meant. Input containing several documents separated by --- is treated as an error, so paste one document at a time.

Formatting happens only in this browser and the input is not sent to a server; your input and options are kept in this browser's localStorage for next time, and Reset clears them. Independently of this tool, passwords and API keys in configuration files should not be committed to a repository, and secrets are better handled with a dedicated secrets manager.

FAQ

Does formatting remove YAML comments?

No. The document is rewritten with its structure intact, comments included, so comment text stays. Only the gap before a trailing comment is set to one space.

Does it change key order or quotes?

No. Keys stay in the order you typed, and single and double quotes stay as you wrote them. Anchors and aliases are also kept rather than expanded.

Should I choose 2 or 4 spaces for YAML?

Most Kubernetes, GitHub Actions and Docker Compose examples use 2 spaces. If your team repository has a convention, match the existing files.

What does 'Tabs are not allowed as indentation' mean?

YAML does not allow tabs for indentation. Replace the tabs on the reported line with spaces and the file will format. Setting your editor to insert spaces for the Tab key prevents the same mistake.

Can it format multiple documents separated by ---?

No. Input with several documents produces an error, so split them and format one document at a time.

Is the YAML I paste stored on a server?

No. Parsing and formatting both happen in this browser and nothing is sent to a server. Your input and options are kept in this browser's localStorage so you can continue later, and Reset clears them.