# YAML Formatierung mehrzeiliger Strings

Quelle: [https://stackoverflow.com/a/21699210](https://stackoverflow.com/a/21699210)

There are <s>5</s> <s>*6*</s> ***NINE*** (or 63\*, depending how you count) different ways to write multi-line strings in YAML.

### TL;DR

<div id="bkmrk-use-%3E-most-of-the-ti"><div>- Use `>` most of the time: interior line breaks are stripped out, although you get one at the end:
    
    ```
    <span class="hljs-attr">key:</span> <span class="hljs-string">>
      Your long
      string here.
    </span>
    ```
- Use `|` if you want those linebreaks to be preserved as `\n` (for instance, embedded markdown with paragraphs).
    
    ```
    <span class="hljs-attr">key:</span> <span class="hljs-string">|
      ### Heading
    </span>
      <span class="hljs-string">*</span> <span class="hljs-string">Bullet</span>
      <span class="hljs-string">*</span> <span class="hljs-string">Points</span>
    
    ```
- 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`:
    
    ```
    <span class="hljs-attr">key:</span> <span class="hljs-string">"Antidisestab\
     lishmentarianism.\n\nGet on it."</span>
    
    ```
- YAML is crazy.

</div></div>### 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](http://www.yaml.org/spec/1.2/spec.html#id2796251)* removes single newlines within the string (but adds one at the end, and converts double newlines to singles):

<div id="bkmrk-key%3A-%3E-this-is-my-ve"><div>```
<span class="hljs-attr">Key:</span> <span class="hljs-string">>
  this is my very very very
  long string
</span>
```

</div></div>→ `this is my very very very long string\n`

`|` *[Literal style](http://www.yaml.org/spec/1.2/spec.html#id2795688)* turns every newline within the string into a literal newline, and adds one at the end:

<div id="bkmrk-key%3A-%7C-this-is-my-ve"><div>```
<span class="hljs-attr">Key:</span> <span class="hljs-string">|
  this is my very very very 
  long string
</span>
```

</div></div>→ `this is my very very very\nlong string\n`

Here's the official definition from the [YAML Spec 1.2](http://www.yaml.org/spec/1.2/spec.html#id2760844)

> 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 “&gt;”) 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](http://www.yaml.org/spec/1.2/spec.html#id2794534) character:

<div id="bkmrk-%3E%2C-%7C%3A-%22clip%22%3A-keep-t"><div>- `>`, `|`: "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.

</div></div>### "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](http://www.yaml.org/spec/1.2/spec.html#id2788859)* (no escaping, no `#` or `:` combinations, limits on first character):

<div id="bkmrk-key%3A-this-is-my-very"><div>```
<span class="hljs-attr">Key:</span> <span class="hljs-string">this</span> <span class="hljs-string">is</span> <span class="hljs-string">my</span> <span class="hljs-string">very</span> <span class="hljs-string">very</span> <span class="hljs-string">very</span> 
  <span class="hljs-string">long</span> <span class="hljs-string">string</span>

```

</div></div>*[double-quoted style](http://www.yaml.org/spec/1.2/spec.html#style/flow/double-quoted)* (`\` and `"` must be escaped by `\`, newlines can be inserted with a literal `\n` sequence, lines can be concatenated without spaces with trailing `\`):

<div id="bkmrk-key%3A-%22this-is-my-ver"><div>```
<span class="hljs-attr">Key:</span> <span class="hljs-string">"this is my very very \"very\" loooo\
  ng string.\n\nLove, YAML."</span>

```

</div></div>→ `"this is my very very \"very\" loooong string.\n\nLove, YAML."`

*[single-quoted style](http://www.yaml.org/spec/1.2/spec.html#style/flow/single-quoted)* (literal `'` must be doubled, no special characters, possibly useful for expressing strings starting with double quotes):

<div id="bkmrk-key%3A-%27this-is-my-ver"><div>```
<span class="hljs-attr">Key:</span> <span class="hljs-string">'this is my very very "very"
  long string, isn'</span><span class="hljs-string">'t it.'</span>

```

</div></div>→ `"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 for the "in-line newlines" row, where it means literally a backslash and an n).

<div id="bkmrk-%3E-%7C-%22-%27-%3E--%3E%2B-%7C--%7C%2B-"><div>```
                      <span class="hljs-string">></span>     <span class="hljs-string">|</span>            <span class="hljs-string">"     '     >-     >+     |-     |+
-------------------------|------|-----|-----|-----|------|------|------|------  
Trailing spaces   | Kept | Kept |     |     |     | Kept | Kept | Kept | Kept
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 dbl nl's => |      |      |     |     |     |      | Kept |      | Kept  
In-line newlines  | No   | No   | No  | \n  | No  | No   | No   | No   | No
Spaceless newlines| No   | No   | No  | \   | No  | No   | No   | No   | No 
Single quote      | '    | '    | '   | '   | ''  | '    | '    | '    | '
Double quote      | "</span>    <span class="hljs-string">|</span> <span class="hljs-string">"    | "</span>   <span class="hljs-string">|</span> <span class="hljs-string">\"</span>  <span class="hljs-string">|</span> <span class="hljs-string">"   | "</span>    <span class="hljs-string">|</span> <span class="hljs-string">"    | "</span>    <span class="hljs-string">|</span> <span class="hljs-string">"
Backslash         | \    | \    | \   | \\  | \   | \    | \    | \    | \
"</span> <span class="hljs-comment">#", ": "        | Ok   | Ok   | No  | Ok  | Ok  | Ok   | Ok   | Ok   | Ok</span>
<span class="hljs-string">Can</span> <span class="hljs-string">start</span> <span class="hljs-string">on</span> <span class="hljs-string">same</span> <span class="hljs-string">|</span> <span class="hljs-literal">No</span>   <span class="hljs-string">|</span> <span class="hljs-literal">No</span>   <span class="hljs-string">|</span> <span class="hljs-literal">Yes</span> <span class="hljs-string">|</span> <span class="hljs-literal">Yes</span> <span class="hljs-string">|</span> <span class="hljs-literal">Yes</span> <span class="hljs-string">|</span> <span class="hljs-literal">No</span>   <span class="hljs-string">|</span> <span class="hljs-literal">No</span>   <span class="hljs-string">|</span> <span class="hljs-literal">No</span>   <span class="hljs-string">|</span> <span class="hljs-literal">No</span>
<span class="hljs-string">line</span> <span class="hljs-string">as</span> <span class="hljs-string">key</span>       <span class="hljs-string">|</span>

```

</div></div>## Examples

*Note the trailing spaces on the line before "spaces."*

<div id="bkmrk---%3E-very-%22long%22-%27str"><div>```
<span class="hljs-bullet">-</span> <span class="hljs-string">>
  very "long"
  'string' with
</span>
  <span class="hljs-string">paragraph</span> <span class="hljs-string">gap,</span> <span class="hljs-string">\n</span> <span class="hljs-string">and</span>        
  <span class="hljs-string">spaces.</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">| 
  very "long"
  'string' with
</span>
  <span class="hljs-string">paragraph</span> <span class="hljs-string">gap,</span> <span class="hljs-string">\n</span> <span class="hljs-string">and</span>        
  <span class="hljs-string">spaces.</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">very</span> <span class="hljs-string">"long"</span>
  <span class="hljs-string">'string'</span> <span class="hljs-string">with</span>

  <span class="hljs-string">paragraph</span> <span class="hljs-string">gap,</span> <span class="hljs-string">\n</span> <span class="hljs-string">and</span>        
  <span class="hljs-string">spaces.</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">"very \"long\"
  'string' with

  paragraph gap, \n and        
  s\
  p\
  a\
  c\
  e\
  s."</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">'very "long"
  '</span><span class="hljs-string">'string'</span><span class="hljs-string">' with

  paragraph gap, \n and        
  spaces.'</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">>- 
  very "long"
  'string' with
</span>
  <span class="hljs-string">paragraph</span> <span class="hljs-string">gap,</span> <span class="hljs-string">\n</span> <span class="hljs-string">and</span>        
  <span class="hljs-string">spaces.</span>

[
  <span class="hljs-string">"very \"long\" 'string' with\nparagraph gap, \\n and         spaces.\n"</span>, 
  <span class="hljs-string">"very \"long\"\n'string' with\n\nparagraph gap, \\n and        \nspaces.\n"</span>, 
  <span class="hljs-string">"very \"long\" 'string' with\nparagraph gap, \\n and spaces."</span>, 
  <span class="hljs-string">"very \"long\" 'string' with\nparagraph gap, \n and spaces."</span>, 
  <span class="hljs-string">"very \"long\" 'string' with\nparagraph gap, \\n and spaces."</span>, 
  <span class="hljs-string">"very \"long\" 'string' with\nparagraph gap, \\n and         spaces."</span>
]

```

</div></div>### Block styles with indentation indicators

Just in case the above isn't enough for you, you can add a "[block indentation indicator](http://www.yaml.org/spec/1.2/spec.html#id2793979)" (after your block chomping indicator, if you have one):

<div id="bkmrk---%3E8-my-long-string-"><div>```
<span class="hljs-bullet">-</span> <span class="hljs-string">>8</span>
        <span class="hljs-string">My</span> <span class="hljs-string">long</span> <span class="hljs-string">string</span>
        <span class="hljs-string">starts</span> <span class="hljs-string">over</span> <span class="hljs-string">here</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">|+1</span>
 <span class="hljs-string">This</span> <span class="hljs-string">one</span>
 <span class="hljs-string">starts</span> <span class="hljs-string">here</span>

```

</div></div>### Addendum

If you insert extra spaces at the start of not-the-first lines in Folded style, they will be kept, with a bonus newline. This doesn't happen with flow styles:

<div id="bkmrk---%3E-my-long-string--"><div>```
<span class="hljs-bullet">-</span> <span class="hljs-string">>
    my long
      string
</span><span class="hljs-bullet">-</span> <span class="hljs-string">my</span> <span class="hljs-string">long</span>
    <span class="hljs-string">string</span>

```

</div></div>→ `["my long\n  string\n", "my long string"]`

I can't even.

*`*`2 block styles, each with 2 possible block chomping indicators (or none), and with 9 possible indentation indicators (or none), 1 plain style and 2 quoted styles: 2 x (2 + 1) x (9 + 1) + 1 + 2 = 63*

Some of this information has also been summarised [here](http://yaml-multiline.info/).