YAML Cheatsheet

A complete, bookmark-worthy reference for YAML syntax — from basics to advanced patterns. All examples are copy-paste ready.

🛠 Got some YAML to check? Validate and format it instantly.

Open Validator →

Basics Start here

Structure rules
# Comments start with #
# Indentation uses SPACES only — never tabs
# YAML is case-sensitive
# Strings don't require quotes (usually)
# --- marks the start of a document
# ... marks the end of a document
⚠️ YAML uses spaces only for indentation. Tab characters will cause a parse error.

Scalars Values

Strings
unquoted: Hello World
single_quoted: 'It''s a test'
double_quoted: "Line 1\nLine 2"
with_colon: "value: with colon"
with_hash: "value # not a comment"
Numbers & Booleans
integer: 42
negative: -17
float: 3.14
scientific: 1.5e10
bool_true: true
bool_false: false
null_value: null
also_null: ~

Lists Sequences

Block style (recommended)
fruits:
  - apple
  - banana
  - cherry

ports:
  - 80
  - 443
  - 8080
Inline (flow) style
fruits: [apple, banana, cherry]

matrix:
  - [1, 2, 3]
  - [4, 5, 6]

empty_list: []

Maps Mappings

Block style
server:
  host: localhost
  port: 8080
  tls: true
Inline (flow) style
server: {host: localhost, port: 8080}

empty_map: {}

Multi-line Strings Block scalars

Literal block ( | ) — preserves newlines
message: |
  Line one
  Line two
  Line three
# Result: "Line one\nLine two\nLine three\n"
Folded block ( > ) — newlines become spaces
description: >
  This is a long sentence
  that wraps across lines
  in the source file.
# Result: "This is a long sentence that wraps..."
Strip trailing newline ( |- )
script: |-
  echo "hello"
  echo "world"
# No trailing newline at end
Keep extra newlines ( |+ )
text: |+
  Hello


# Keeps all trailing blank lines

Data Types Reference

TypeExampleNotes
Stringname: AliceQuotes optional unless special chars present
Integerport: 8080Decimal, hex (0xFF), octal (0o77)
Floatratio: 0.95Also .inf, -.inf, .nan
Booleanenabled: truetrue/false (lowercase in YAML 1.2)
Nullvalue: nullAlso ~ or empty value
Datedate: 2024-01-15ISO 8601 format
Datetimets: 2024-01-15T10:00:00ZWith timezone
Binarydata: !!binary |Base64 encoded
Force stringzip: "01234"Quote to prevent type coercion
💡 Use quotes around values like "true", "null", "1.0", or "01234" when you need them treated as strings, not their native types.

Anchors & Aliases DRY configs

Define & reuse with & and *
defaults: &defaults
  timeout: 30
  retries: 3
  log_level: info

production:
  <<: *defaults
  log_level: warn  # override

staging:
  <<: *defaults
  timeout: 60    # override
Reuse a scalar value
image_tag: &tag v1.4.2

services:
  api:
    image: myapp/api:*tag
  worker:
    image: myapp/worker:*tag

Nested Structures Real-world

List of maps (common in K8s, CI configs)
steps:
  - name: Checkout
    uses: actions/checkout@v4
  - name: Build
    run: npm run build
    env:
      NODE_ENV: production
  - name: Test
    run: npm test

Special Characters Quoting guide

CharacterContextFix
:In a valueQuote the value: "host:port"
#Inline in valueQuote it: "color #fff"
@, `Start of valueAlways quote
{, [Start of valueQuote or use block style
*, &Start of valueQuote to avoid anchor/alias parsing
|, >Start of valueQuote to avoid block scalar
!Start of valueQuote (used for type tags)

Multi-document Files Advanced

Separate documents with ---
---  # Document 1: Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
---  # Document 2: Service
apiVersion: v1
kind: Service
metadata:
  name: my-app-svc