YAML Indentation and Structure

Indentation patterns, nested lists and maps, and practical commands for catching whitespace and structure mistakes.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Indentation rules

Examples that show what valid nesting looks like and how structure changes.

Two-space indentation

Use consistent spaces for nested levels.

yamlANYyamlindentationstyle
yaml
app:
  name: Cheatsheet.today
  seo:
    enabled: true

Two spaces is the most common convention and works well across teams.

Mixed indentation example

Illustrate a broken file caused by inconsistent nesting.

yamlANYyamlindentationerrors
yaml
app:
  name: Cheatsheet.today
   seo:
    enabled: true

Even one extra space can change meaning or break parsing altogether.

Sequence nested under a key

Indent list items under their parent key.

yamlANYyamllistindentation
yaml
services:
  - api
  - web
  - worker

List items should align consistently under the key they belong to.

Mappings inside a sequence

Represent a list of objects cleanly.

yamlANYyamllistmapping
yaml
users:
  - name: Jonathan
    role: admin
  - name: Sarah
    role: editor

A common pattern for config, deployments, pipelines, and API fixtures.

Linting and safe edits

Commands that reduce indentation errors before they hit production.

Run yamllint with a custom config

Use a project-specific lint profile.

bashANYyamlyamllintconfig
bash
yamllint -d '{extends: default, rules: {line-length: disable}}' config.yaml

Helpful when you want structural checks without enforcing every style rule.

Reveal hidden whitespace

Print tabs and line endings to spot indentation issues.

bashANYyamlwhitespacedebugging
bash
sed -n 'l' config.yaml

Useful when a file looks correct but still fails because of hidden whitespace or tabs.

Find tabs in YAML files

Locate tab characters that may break parsing.

bashANYyamltabslint
bash
grep -nP '\t' config.yaml

YAML should use spaces, not tabs, for indentation.

Format YAML with Prettier

Normalize indentation and layout automatically.

bashANYyamlprettierformat
bash
prettier --write config.yaml

Prettier helps reduce noisy diffs and keeps YAML files consistent across a repo.

Recommended next

No recommendations yet.