YAML Syntax Cheatsheet
YAML (YAML Ain't Markup Language) is a human-readable data serialization format widely used for configuration files, CI/CD pipeline definitions, and cross-language data exchange. This cheatsheet covers common patterns from basic syntax to advanced features like anchors, multiline strings, and type system.
Updated: 2026-07-17·32 commands
Overview
YAML (YAML Ain't Markup Language) is a human-readable data serialization format commonly used for configuration files. This cheatsheet covers YAML syntax, data types, multiline strings, anchors, and advanced features.
Quick Start
``yaml
key: value
list:
- item1
- item2
map:
subkey: subvalue
``
Refer to the command table below for detailed usage.
Basic Syntax(6)
| Command | Level | ||
|---|---|---|---|
key: valueBasic key-value mapping | Basic | name: John Doe | |
- itemList/array definition | Basic | - apple
- banana
- cherry | |
nested mappingNested mapping (indentation for nesting) | Basic | person:
name: Alice
age: 30
address:
city: Beijing | |
list of mappingsList of mappings (each element is key-value pairs) | Basic | - name: Alice
age: 30
- name: Bob
age: 25 | |
# commentComment (from # to end of line) | Basic | # This is a YAML comment
key: value # inline comment | |
indentation rulesIndentation rules (2 spaces, no tabs) | Basic | parent:
child:
grandchild: value |
Data Types(7)
| Command | Level | ||
|---|---|---|---|
'single quoted'Single-quoted string (no escape processing) | Basic | 'Don''t escape \n here' | |
"double quoted"Double-quoted string (escapes processed) | Basic | "Line1\nLine2\tTabbed" | |
unquoted stringUnquoted string (simple values) | Basic | hello world | |
true / falseBoolean values (case-insensitive) | Basic | enabled: true
debug: no
verbose: on | |
null / ~Null value representations | Basic | value: null
also_null: ~
blank: | |
integer / floatInteger and floating-point numbers | Basic | count: 42
pi: 3.14
hex: 0x1A
sci: 1e6 | |
ISO 8601 timestampISO 8601 timestamp | Intermediate | created: 2026-07-17T14:30:00Z
updated: 2026-07-17 |
Multiline(4)
| Command | Level | ||
|---|---|---|---|
|Literal block scalar (preserves newlines) | Intermediate | script: |
echo hello
echo world | |
>Folded block scalar (newlines become spaces) | Intermediate | description: >
This is a long
description that will
be folded into one line | |
|-Literal block with trailing newline stripped | Intermediate | script: |-
line1
line2 | |
|+Literal block with all trailing newlines kept | Intermediate | block: |+
keep
blank lines |
Anchors(5)
| Command | Level | ||
|---|---|---|---|
&-nameDefine an anchor | Intermediate | defaults: &defaults
timeout: 30
retries: 3 | |
*-nameReference an anchor (alias) | Intermediate | server1:
<<: *defaults
host: host1.example.com | |
<<Merge key (merge anchor mapping into current) | Intermediate | base: &base
x: 1
y: 2
item:
<<: *base
z: 3 | |
nested anchorsNested anchors and aliases | Intermediate | conn: &conn
host: localhost
port: 5432
db:
<<: *conn
name: mydb | |
multiple aliasesSingle anchor referenced multiple times | Intermediate | shared: &shared
env: prod
srv1:
<<: *shared
name: web
srv2:
<<: *shared
name: worker |
Advanced(10)
| Command | Level | ||
|---|---|---|---|
GitHub Actions workflowYAML in GitHub Actions | Intermediate | name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4 | |
GitLab CI pipelineYAML in GitLab CI | Intermediate | stages:
- build
- test
build_job:
stage: build
script:
- make build | |
yq evalyq read YAML value | Basic | yq eval '.name' config.yaml | |
yq eval arrayyq traverse array elements | Basic | yq eval '.items[]' config.yaml | |
yq eval with pipeyq piped queries | Intermediate | yq eval '.users[] | select(.active == true)' config.yaml | |
yq eval -iyq in-place edit YAML file | Intermediate | yq eval -i '.version = "2.0"' config.yaml | |
yq --yaml-allyq process all documents in multi-doc YAML | Expert | yq eval --yaml-all '.kind' multi.yaml | |
yq path expressionsyq path expressions (wildcard, recursive) | Expert | yq eval '.. | select(. == "target")' config.yaml | |
merge YAML filesMerge multiple YAML files | Expert | yq eval-all '. as $item ireduce ({}; . * $item)' a.yaml b.yaml | |
multi-document YAMLMulti-document YAML (separated by ---) | Intermediate | ---
apiVersion: v1
kind: Pod
---
apiVersion: v1
kind: Service |
FAQ
This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-17.