YAML Formatting Guide for Config Files
YAML is popular because it is compact and readable. You see it in CI pipelines, Docker Compose files, Kubernetes manifests, static site configs, package metadata, and documentation systems. That readability comes with a tradeoff: YAML is indentation-sensitive, and small formatting mistakes can change the meaning of a file.
Use the YAML Formatter to validate and clean YAML snippets. If you need to move between formats, use the JSON to YAML Converter.
YAML Is Built Around Indentation
In JSON, braces and brackets define structure. In YAML, indentation defines structure.
site:
name: QuickToolFlow
tools:
- JSON Formatter
- YAML FormatterHere, name and tools belong to site. The list items belong to tools. If the indentation changes, the structure changes.
Maps and Lists
Most YAML files use two basic structures: maps and lists.
A map stores key-value pairs:
name: QuickToolFlow
category: 'Developer Tools'A list stores ordered values:
tools:
- JSON Formatter
- CSV Formatter
- XML FormatterLists can contain maps:
tools:
- name: JSON Formatter
category: Developer Tools
- name: CSV Formatter
category: Developer ToolsCommon YAML Formatting Rules
Use spaces, not tabs Tabs can break YAML parsing. Use spaces consistently.
Keep indentation consistent Two spaces per level is common. Four spaces can also work, but mixing indentation styles makes files hard to maintain.
Quote ambiguous strings YAML can interpret values like true, false, yes, no, on, off, or numeric-looking strings. If a value must remain text, quote it.
feature_flag: 'off'
zip_code: '00123'Be careful with colons A colon followed by a space usually starts a key-value pair. Quote strings that contain colon-space sequences.
message: 'Status: ready'Ambiguous Values to Quote
YAML parsers can interpret certain plain values as booleans, numbers, nulls, or dates. This is useful sometimes, but surprising when you intended to store text.
| Value | Possible interpretation | Safer text form |
|---|---|---|
00123 | Number with leading zeros lost | '00123' |
off | Boolean-like value in some parsers | 'off' |
2026-06-10 | Date-like value | '2026-06-10' |
1.0 | Number | '1.0' if it is a version |
user: admin | Key-value syntax if placed incorrectly | 'user: admin' |
Config files often contain versions, feature flags, IDs, zip codes, and labels. Quote values when preserving the exact text matters.
Formatting vs Preserving Comments
Many YAML formatters parse YAML and then dump it again. This is useful for validating structure and normalizing indentation, but comments may be removed because comments are not part of the parsed data model.
If comments are important, keep a source copy and use formatters mainly for validation or cleanup of generated YAML.
Anchors and Repeated Configuration
YAML supports anchors and aliases for repeated values:
defaults: &defaults
retries: 3
timeout: 30
job:
<<: *defaults
name: import-dataThis can reduce duplication in configuration files, but it also makes the final resolved structure less obvious to readers. Use anchors carefully in shared files, and avoid them when a platform or tool does not support merge keys consistently.
If a formatter changes or removes advanced YAML syntax, review the output before replacing the original config.
YAML and JSON Are Closely Related
Most practical YAML files can be represented as JSON objects and arrays. This is why converting JSON to YAML is common for config workflows.
JSON:
{
"name": "QuickToolFlow",
"private": true
}YAML:
name: QuickToolFlow
private: trueYAML is often easier for humans to edit. JSON is often easier for machines to generate and validate.
YAML in CI and Deployment Files
YAML mistakes are especially costly in CI pipelines and deployment manifests because the file may look readable while doing something different from what you intended.
For example, indentation controls whether an environment variable belongs to a job, a step, a container, or a service. A misplaced block can cause:
- a deployment step to run in the wrong stage
- an environment variable to be unavailable
- a Kubernetes field to be ignored
- a Docker Compose service to receive the wrong setting
- a static site build to use the wrong config value
After formatting YAML, compare meaningful changes with the Text Diff Checker before committing config updates.
Common Mistakes in Config Files
Wrong indentation This is the most common YAML problem. A single extra space can move a value into the wrong parent object.
Unquoted special values If a value looks like a boolean or number but should stay text, quote it.
Mixed list styles Use block lists for readability in config files instead of squeezing everything into inline arrays.
Duplicate keys Some parsers allow duplicate keys and keep only the last value. Avoid duplicates because they create confusing configuration behavior.
Broken multi-line strings YAML uses | and > for multi-line text. They look similar but behave differently. Use | when line breaks must be preserved and > when folded text is acceptable.
literal: |
Line one
Line two
folded: >
Line one
Line twoAssuming every tool supports every YAML feature Some tools support only a subset of YAML. If a platform has its own configuration schema, follow that schema first and YAML style second.
Safe YAML Editing Workflow
- Keep a copy of the original config.
- Format or validate the YAML snippet.
- Check whether comments or anchors were preserved.
- Compare the old and new version.
- Run the tool that consumes the YAML if possible.
- Commit config changes separately from unrelated edits.
This workflow is slower than pasting over a file, but it is much safer for deployment and automation configs.
Related Guides
- JSON vs YAML vs XML explains when YAML is a better fit than JSON or XML.
- JSON to YAML conversion guide is useful when moving config examples between formats.
- Browse related utilities in the Code Formatting Tools collection.
Related QuickToolFlow Tools
- YAML Formatter for validating and formatting YAML.
- JSON to YAML Converter for moving structured data between formats.
- JSON Formatter & Validator for checking JSON before converting it.
- Text Diff Checker for comparing config changes.
Keep going