YAML Multiline
Find the right syntax for your YAML multiline strings

Two Types of Formats for Strings

There are two types of formats that YAML supports for scalar strings. (Scalars are what YAML calls basic values like numbers or strings, as opposed to complex types like arrays or objects.)

Although they are parsed by different rules, they all express strings, allowing authors to choose the option that expresses best what they need the string to be.

Flow Scalars

Single-quoted

YAML
example: 'A simple string.'
Result
A simple string.
YAML
example: 'Several lines of text,\n
··containing ''single quotes'' and "double quotes".
··Escapes (like \n) don''t do anything.\n
··\n
··Newlines can be added by leaving a blank line.\n
····Leading whitespace on lines is ignored.'\n
Result
Several lines of text, containing 'single quotes' and "double quotes". Escapes (like \n) don't do anything.\n
Newlines can be added by leaving a blank line. Leading whitespace on lines is ignored.

Double-quoted

YAML
example: "A simple string."
Result
A simple string.
YAML
example: "Several lines of text,\n
··containing \"double quotes\" and 'single quotes'.
··Escapes (like \\n) work.\nIn addition,\n
··newlines can be esc\\n
··aped to prevent them from being converted to a space.\n
··\n
··Newlines can also be added by leaving a blank line.\n
····Leading whitespace on lines is ignored."\n
Result
Several lines of text, containing "double quotes" and 'single quotes'. Escapes (like \n) work.\n
In addition, newlines can be escaped to prevent them from being converted to a space.\n
Newlines can also be added by leaving a blank line. Leading whitespace on lines is ignored.\n

Plain

YAML
example: A simple string.
Result
A simple string.
YAML
example: Several lines of text,\n
··with some "quotes" of various 'types'.\n
··Escapes (like \n) don't do anything.\n
··\n
··Newlines can be added by leaving a blank line.\n
····Additional leading whitespace is ignored.\n
Result
Several lines of text, with some "quotes" of various 'types'. Escapes (like \n) don't do anything.\n
Newlines can be added by leaving a blank line. Additional leading whitespace is ignored.

⚠️ Plain flow scalars (unquoted strings) have several pitfalls:

If your strings are affected by any of these caveats you should use one of the quoted styles instead.

Block Scalars

A block scalar header has three parts:

Block Style Indicator: The block style indicates how newlines inside the block should behave. If you would like them to be kept as newlines, use the literal style, indicated by a pipe (|). If instead you want them to be replaced by spaces, use the folded style, indicated by a right angle bracket (>). (To get a newline using the folded style, leave a blank line by putting two newlines in. Lines with extra indentation are also not folded.)

Block Chomping Indicator: The chomping indicator controls what should happen with newlines at the end of the string. The default, clip, puts a single newline at the end of the string. To remove all newlines, strip them by putting a minus sign (-) after the style indicator. Both clip and strip ignore how many newlines are actually at the end of the block; to keep them all put a plus sign (+) after the style indicator.

Indentation Indicator: Ordinarily, the number of spaces you're using to indent a block will be automatically guessed from its first line. You may need a block indentation indicator if the first line of the block starts with extra spaces. In this case, simply put the number of spaces used for indentation (between 1 and 9) at the end of the header.

Demo

Block Scalar Style (?)
Block Chomping (?)
Indentation
Indent by spaces
YAML
example: |>-+\n
··Several lines of text,\n
··with some "quotes" of various 'types',\n
··and also a blank line:\n
··\n
··and some text with\n
··extra indentation\n
··on the next line,\n
··plus another line at the end.\n
··\n
··\n
Result
Several lines of text,\n
 with some "quotes" of various 'types',\n
 and also a blank line:\n
\n
and some text with\n
  extra indentation\n
on the next line,\n
 plus another line at the end.\n
\n
\n