YAML

Enjoy this cheat sheet at its fullest within Dash, the macOS documentation browser.

Collection Indicators

?

Key indicator

:

Value indicator

-

Nested series entry indicator

,

Separate in-line branch entries

[]

Surround in-line series branch

{}

Surround in-line keyed branch

Scalar Indicators

''

Surround in-line unescaped scalar ('' escaped ')

"

Surround in-line escaped scalar (see escape codes below)

|

Block scalar indicator

>

Folded scalar indicator

-

Strip chomp modifier (|- or >-)

+

Keep chomp modifier (|+ or >+)

1-9

Explicit indentation modifier (|1 or >2)

Modifiers can be combined (|2-, >+1)

Alias Indicators

&

Anchor property

*

Alias indicator

Tag Property (usually unspecified)

none

Unspecified tag (automatically resolved by application)

!

Non-specific tag (by default, !!map/!!seq/!!str)

!foo

Primary (by convention, means a local !foo tag)

!!foo

Secondary (by convention, means tag:yaml.org,2002:foo)

!h!foo

Requires %TAG !h! <prefix> (and then means <prefix>foo)

!<foo>

Verbatim tag (always means foo)

Document Indicators

%

Directive indicator

---

Document header

...

Document terminator

Misc Indicators

#

Throwaway comment indicator

`@

Both reserved for future use

Special Keys

=

Default "value" mapping key

<<

Merge keys from another mapping

Core Types (default automatic tags)

!!map

{ Hash table, dictionary, mapping }

!!seq

{ List, array, tuple, vector, sequence }

!!str

Unicode string

More Types

!!set

{ cherries, plums, apples }

!!omap

[ one: 1, two: 2 ]

Language Independent Scalar Types

{ ~, null }

Null (no value)

[ 1234, 0x4D2, 02333 ]

[ Decimal int, Hexadecimal int, Octal int ]

[ 1_230.15, 12.3015e+02 ]

[ Fixed float, Exponential float ]

[ .inf, -.Inf, .NAN ]

[ Infinity (float), Negative, Not a number ]

{ Y, true, Yes, ON }

Boolean true

{ n, FALSE, No, off }

Boolean false

? !!binary >
  R0lG...BADS=
>-
  Base 64 binary value

Escape Codes

Numeric

"\x12"       : 8-bit
"\u1234"     : 16-bit
"\U00102030" : 32-bit

Protective

"\\"     : '\'
"\""     : '"'
"\ "     : ' '
"\<TAB>" : TAB

C

"\0": NUL
"\a": BEL
"\b": BS
"\f": FF
"\n": LF
"\r": CR
"\t": TAB
"\v": VTAB

Additional

"\e": ESC
"\_": NBSP
"\N": NEL
"\L": LS
"\P": PS

Examples

Scalars

# scalar = value
a: 1
a: 1.234
b: 'abc'
b: "abc"
b: abc
c: false    # boolean type
d: 2015-04-05   # date type

# Enforcing strings
b: !str 2015-04-05

Sequences

# sequence
array:
- 132
- 2.434
- 'abc'

# sqeuence of sequences
my_array:
- [1, 2, 3]
- [4, 5, 6]

Hashes

# Nest hash
my_hash:
  subkey:
    subsubkey1: 5
    subsubkey2: 6
  another:
    somethingelse: 'Important!'

# Hash of hashes
my_hash: {nr1: 5, nr2: 6}

HereDoc

# block notation (newlines become spaces)
content:
  Arbitrary free text
  over multiple lines stopping
  after indentation changes...

# literal style (newlines are preserved)
content: |
  Arbitrary free text
  over "multiple lines" stopping
  after indentation changes...

# + indicator (keep extra newlines after block)
content: |+
  Arbitrary free text with two newlines after


# - indicator (remove extra newlines after block)
content: |-
  Arbitrary free text without newlines after it


# folded style (folded newlines are preserved)
content: >
  Arbitrary free text
  over "multiple lines" stopping
  after indentation changes...

Multiple documents

---
content: doc1
---
content: doc2

Reference content

---
values:
- &ref Something to reuse
- *ref  # Reused content

Merging keys

default_settings:
  install:
    dir: /usr/local
    owner: root
  config:
    enabled: false

# Derive settings for 'my_app' from default and change install::owner
my_app_settings:
  <<: *default_settings
  install:
    owner: my_user

Complex mapping

---
? - key
:
  - value
# Note: key and value can be multiple, complex structures

Notes