Break string in YAML over multiple lines

  • Use > most of the time: interior line breaks are stripped out, although you get one at the end: key: > Your long string here.
  • Use | if you want those linebreaks to be preserved as \\n (for instance, embedded markdown with paragraphs). key: | ### Heading * Bullet * Points
  • Use >- or |- instead if you don’t want a linebreak appended at the end.
  • Use "..." if you need to split lines in the middle of words or want to literally type linebreaks as \\n: key: "Antidisestab\\ lishmentarianism.\\n\\nGet on it."
  • YAML is crazy.

Block scalar styles (>|)

These allow characters such as \\ and " without escaping, and add a new line (\\n) to the end of your string.

> Folded style removes single newlines within the string (but adds one at the end, and converts double newlines to singles):

Key: >
  this is my very very very
  long string

→ this is my very very very long string\\n

| Literal style turns every newline within the string into a literal newline, and adds one at the end:

Key: |
  this is my very very very
  long string

→ this is my very very very\\nlong string\\n

Here’s the official definition from the YAML Spec 1.2

Scalar content can be written in block notation, using a literal style (indicated by “|”) where all line breaks are significant. Alternatively, they can be written with the folded style (denoted by “>”) where each line break is folded to a space unless it ends an empty or a more-indented line.

Block styles with block chomping indicator (>-|->+|+)

You can control the handling of the final new line in the string, and any trailing blank lines (\\n\\n) by adding a block chomping indicator character:

  • >|: “clip”: keep the line feed, remove the trailing blank lines.
  • >-|-: “strip”: remove the line feed, remove the trailing blank lines.
  • >+|+: “keep”: keep the line feed, keep trailing blank lines.

“Flow” scalar styles (, "')

These have limited escaping, and construct a single-line string with no new line characters. They can begin on the same line as the key, or with additional newlines first.

plain style (no escaping, no # or : combinations, limits on first character):

Key: this is my very very very
  long string

double-quoted style (\\ and " must be escaped by \\, newlines can be inserted with a literal \\n sequence, lines can be concatenated without spaces with trailing \\):

Key: "this is my very very \\"very\\" loooo\\
  ng string.\\n\\nLove, YAML."

→ "this is my very very \\"very\\" loooong string.\\n\\nLove, YAML."

single-quoted style (literal ' must be doubled, no special characters, possibly useful for expressing strings starting with double quotes):

Key: 'this is my very very "very"
  long string, isn''t it.'

→ "this is my very very \\"very\\" long string, isn't it."

Summary

In this table, _ means space character\n means “newline character” (\n in JavaScript) except under “Other features”. “Leading space” applies after the first line (which establishes the indent)

>|"'>->+|-|+
Spaces/newlines converted as:
Trailing space →______
Leading space →\n_\n_\n_\n_\n_\n_
Single newline →_\n_____\n\n
Double newline →\n\n\n\n\n\n\n\n\n\n\n\n
Final newline →\n\n\n\n
Final double newline →\n\n\n\n
How to create a literal:
Single quote
Double quote\”
Backslash\\\\\\\\\\
Other features
In-line newlines with \n🚫🚫🚫🚫🚫🚫🚫🚫
Spaceless newlines with \🚫🚫🚫🚫🚫🚫🚫🚫
# or :  in value🚫
Can start on same
line as key
🚫🚫🚫🚫🚫🚫

Examples

Note the trailing spaces on the line before “spaces.”

- >
  very "long"
  'string' with

  paragraph gap, \\n and
  spaces.
- |
  very "long"
  'string' with

  paragraph gap, \\n and
  spaces.
- very "long"
  'string' with

  paragraph gap, \\n and
  spaces.
- "very \\"long\\"
  'string' with

  paragraph gap, \\n and
  s\\
  p\\
  a\\
  c\\
  e\\
  s."
- 'very "long"
  ''string'' with

  paragraph gap, \\n and
  spaces.'
- >-
  very "long"
  'string' with

  paragraph gap, \\n and
  spaces.

[
  "very \\"long\\" 'string' with\\nparagraph gap, \\\\n and         spaces.\\n",
  "very \\"long\\"\\n'string' with\\n\\nparagraph gap, \\\\n and        \\nspaces.\\n",
  "very \\"long\\" 'string' with\\nparagraph gap, \\\\n and spaces.",
  "very \\"long\\" 'string' with\\nparagraph gap, \\n and spaces.",
  "very \\"long\\" 'string' with\\nparagraph gap, \\\\n and spaces.",
  "very \\"long\\" 'string' with\\nparagraph gap, \\\\n and         spaces."
]

Block styles with indentation indicators

Just in case the above isn’t enough for you, you can add a “block indentation indicator” (after your block chomping indicator, if you have one):

- >8
        My long string
        starts over here
- |+1
 This one
 starts here