yaml multiline strings

As you know YAML document contains key and value pairs.

In the yaml document, if the value is larger to accommodate a single line, you can wrap the string in multiple lines. In this post, We are going to learn how to break a string in yaml over multiple lines.

For example, We have a string

keystring: long string in longer longer longer longer longer longer longer longer

So we want to rewrite a longer string into multiple lines.

keystring: 'long string in longer
            longer longer longer longer longer longer longer'

There are two syntax ways we can represent multi-line strings

  • literal block
  • folder block

Literal styles with the pipe operator

Multi-line strings are stored using pipe character (|).

important points about literal styles

  • The new line is preserved

  • Leading spaces or indentations are removed.

  • Strings can have with or without quotation marks

storing the new line using the pipe character

string1: |
   Line1
   line2
   "line3"
  line4

folded block styles with > character

The strings in multiple lines are enclosed as folded form using > character Important rules about folder syntax

  • Leading (beginning) indentation or spaces of a line are not considered
  • Trailing or end-of-line spaces are not preserved
  • newlines are not preserved
  • newlines are converted to spaces.
string1: >
   Line1
   line2
   "line3"
  line4