YAML is everywhere in modern development — Kubernetes manifests, GitHub Actions workflows, Docker Compose files, Ansible playbooks, and application config. Despite its "human-friendly" reputation, YAML is full of subtle traps that cause hard-to-debug parse errors and unexpected behavior. Following solid YAML formatting best practices from the start saves hours of frustration. This guide covers everything from indentation rules to anchors, with a focus on the gotchas that trip up even experienced developers.
If you need to validate or convert a YAML file right now, use our YAML to JSON Converter — paste your YAML and instantly see if it parses correctly.
Indentation: The Foundation of Valid YAML
YAML uses indentation to define structure, similar to Python. The rules are strict:
- Use spaces only — never tabs. YAML parsers treat tab characters as errors. Most editors can be configured to insert spaces when you press Tab.
- Be consistent within a file. While YAML allows any number of spaces per level (1, 2, 4, etc.), pick one and stick with it. Two spaces is the community standard.
- Child keys must be indented further than parent keys. The exact number doesn't matter as long as it's consistent within a block.
{codeIndent}
Configure your editor: in VS Code, set "editor.insertSpaces": true and "editor.tabSize": 2 in your workspace settings. The YAML extension will also highlight indentation errors in real time.
Strings: When to Quote and When Not To
YAML's automatic type inference is convenient but dangerous. Unquoted values are parsed as strings, booleans, integers, floats, or null depending on their content. The safest rule: quote values that could be misinterpreted.
{codeStrings}
Use double quotes (") when you need escape sequences like \n, \t, or Unicode escapes. Use single quotes (') when the value should be taken literally — single-quoted strings don't process escape sequences. Use no quotes for simple alphanumeric strings that can't be misread.
Booleans and Nulls: The YAML 1.1 Trap
This is one of the most common sources of bugs in YAML configs. YAML 1.1 (used by many older parsers including PyYAML by default) treats yes, no, on, and off as boolean values. YAML 1.2 (the current spec) only recognizes true and false.
{codeBoolNull}
The classic bug: a country code list that contains NO (Norway) or a config key set to on. Always use true/false for booleans, and always quote values that happen to look like these keywords.
Multi-Line Strings: Literal vs. Folded
YAML provides two block scalar styles for multi-line strings, and choosing the right one matters for readability and correctness:
{codeMultiline}
Key differences:
-
Literal (
|): Preserves newlines as-is. Use for shell scripts, SQL queries, or any content where line breaks are meaningful. -
Folded (
>): Replaces single newlines with spaces, preserves blank lines as newlines. Use for long prose descriptions that should flow as a single paragraph. - The chomping indicator (
-to strip,+to keep) controls trailing newlines. The default clips to exactly one trailing newline.
Anchors and Aliases: DRY Config Files
YAML supports a native reuse mechanism called anchors (&) and aliases (*). Combined with the merge key (<<), this lets you define shared defaults once and extend them per environment — a powerful pattern for multi-environment configs.
{codeAnchors}
Anchors are processed by the YAML parser before your application sees the data, so the result is identical to manually duplicating the keys. Use them to eliminate repetition in Docker Compose files, CI matrices, and Kubernetes Helm values.
YAML-Specific Gotchas to Memorize
- Duplicate keys — YAML technically allows them but behavior is parser-defined. Most parsers silently keep the last value. Always lint for duplicates.
- Trailing spaces — Some parsers are sensitive to trailing whitespace after values. Run a trim on save.
-
Octal integers —
0755is parsed as octal 493, not the integer 755. In YAML 1.2, octal is0o755. Quote file permission strings to be safe. -
Float ambiguity —
1e2is parsed as float 100.0. If you mean the string "1e2", quote it. -
Colon in values — A colon followed by a space (
:) is a key-value separator. If your value contains a colon-space sequence, quote the entire value.
Linting with yamllint and Prettier
Linting catches formatting issues before they cause runtime failures. Two tools dominate:
{codeLint}
For JavaScript/Node.js projects, Prettier with the @prettier/plugin-yaml plugin can auto-format YAML files on save. Add it to your pre-commit hooks via lint-staged so malformed YAML never makes it into the repository.
For quick ad-hoc validation, paste your YAML into our YAML to JSON Converter — if it converts successfully, the YAML is structurally valid.
Want these tools available offline? The DevToolkit Bundle ($9 on Gumroad) packages 40+ developer tools into a single downloadable kit — no internet required.
Summary
- Always use spaces (2 per level), never tabs.
- Quote values that look like booleans, numbers, or nulls but aren't.
- Use only
true/falsefor booleans to avoid YAML 1.1 / 1.2 differences. - Use
|for content where newlines matter,>for flowing prose. - Use anchors and merge keys (
<<) to DRY up multi-environment configs.
- Lint every YAML file with yamllint and add it to your CI pipeline.
Free Developer Tools
If you found this article helpful, check out DevToolkit — 40+ free browser-based developer tools with no signup required.
Popular tools: JSON Formatter · Regex Tester · JWT Decoder · Base64 Encoder
🛒 Get the DevToolkit Starter Kit on Gumroad — source code, deployment guide, and customization templates.
Top comments (0)