YAML Cheat Sheet

A quick reference for YAML syntax, data types, and common patterns. Save this page for when you need a fast lookup.

Comments

# This is a comment
name: Alice  # inline comment

Scalars (Strings, Numbers, Booleans)

# Strings
name: Alice
quoted: "Hello, World!"
single: 'It''s a trap'

# Multiline strings
literal: |
  Line one
  Line two
  (newlines preserved)

folded: >
  This is a long sentence
  that wraps in the source
  but becomes one line.

# Numbers
integer: 42
float: 3.14
scientific: 1.5e10
hex: 0xFF
octal: 0o77

# Booleans (YAML 1.2)
enabled: true
disabled: false

# Null
nothing: null
also_null: ~

Sequences (Lists / Arrays)

# Block style (preferred)
fruits:
  - apple
  - banana
  - cherry

# Inline / flow style
colors: [red, green, blue]

# List of objects
users:
  - name: Alice
    role: admin
  - name: Bob
    role: viewer

Mappings (Objects / Dictionaries)

# Block style
person:
  name: Alice
  age: 30
  active: true

# Inline / flow style
point: {x: 10, y: 20}

# Nested
config:
  database:
    host: localhost
    port: 5432
  cache:
    ttl: 300

Anchors & Aliases (Reuse)

# Define a reusable anchor with &
defaults: &defaults
  timeout: 30
  retries: 3

# Merge with <<
production:
  <<: *defaults
  host: prod.example.com

staging:
  <<: *defaults
  host: staging.example.com
  timeout: 60   # override

Multi-Document Files

---
# Document 1
name: Alice
role: admin
---
# Document 2
name: Bob
role: viewer
...

Use --- to separate documents. ... marks the explicit end of a document.

Dates & Timestamps

date: 2026-02-26
datetime: 2026-02-26T14:30:00Z
datetime_local: 2026-02-26T14:30:00+05:30

Complex Keys

# Key with spaces (use quotes)
"full name": Alice Smith

# Complex key with ? notation
? - x
  - y
: point

Kubernetes Deployment Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: my-app:latest
          ports:
            - containerPort: 8080
          env:
            - name: NODE_ENV
              value: production

Docker Compose Example

version: "3.9"
services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html
  db:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: mydb
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:

GitHub Actions Example

name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci
      - run: npm test

Common Gotchas

Tabs are not allowed

YAML requires spaces for indentation. Tabs cause parse errors. Always use 2 or 4 spaces.

Special string values need quoting

Values like yes, no, on, off are parsed as booleans in YAML 1.1. Use quotes if you mean the string: "yes".

Colons in values need quoting

A colon followed by a space starts a mapping. Wrap values with colons in quotes: url: "https://example.com".

Indentation must be consistent

All items at the same level must share the same indentation depth. Mixing 2- and 4-space indentation in the same block causes errors.

Validate Your YAML

Paste your YAML into the formatter to check for syntax errors and format it instantly.

Open YAML Formatter