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
Two-space indentation
app:
  name: Cheatsheet.today
  seo:
    enabled: true

# Use consistent spaces for nested levels.

Mixed indentation example
app:
  name: Cheatsheet.today
   seo:
    enabled: true

# Illustrate a broken file caused by inconsistent nesting.

Sequence nested under a key
services:
  - api
  - web
  - worker

# Indent list items under their parent key.

Mappings inside a sequence
users:
  - name: Jonathan
    role: admin
  - name: Sarah
    role: editor

# Represent a list of objects cleanly.

## Linting and safe edits
Run yamllint with a custom config
yamllint -d '{extends: default, rules: {line-length: disable}}' config.yaml

# Use a project-specific lint profile.

Reveal hidden whitespace
sed -n 'l' config.yaml

# Print tabs and line endings to spot indentation issues.

Find tabs in YAML files
grep -nP '\t' config.yaml

# Locate tab characters that may break parsing.

Format YAML with Prettier
prettier --write config.yaml

# Normalize indentation and layout automatically.

Recommended next

No recommendations yet.