YAML Cheatsheet
A complete, bookmark-worthy reference for YAML syntax — from basics to advanced patterns. All examples are copy-paste ready.
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
| Type | Example | Notes |
|---|---|---|
| String | name: Alice | Quotes optional unless special chars present |
| Integer | port: 8080 | Decimal, hex (0xFF), octal (0o77) |
| Float | ratio: 0.95 | Also .inf, -.inf, .nan |
| Boolean | enabled: true | true/false (lowercase in YAML 1.2) |
| Null | value: null | Also ~ or empty value |
| Date | date: 2024-01-15 | ISO 8601 format |
| Datetime | ts: 2024-01-15T10:00:00Z | With timezone |
| Binary | data: !!binary | | Base64 encoded |
| Force string | zip: "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
| Character | Context | Fix |
|---|---|---|
: | In a value | Quote the value: "host:port" |
# | Inline in value | Quote it: "color #fff" |
@, ` | Start of value | Always quote |
{, [ | Start of value | Quote or use block style |
*, & | Start of value | Quote to avoid anchor/alias parsing |
|, > | Start of value | Quote to avoid block scalar |
! | Start of value | Quote (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