yaml multiline strings

As you know YAML document contains key and value pairs.

In yaml document, if value is larger to accommodated in single line,you can wrap string in multi 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 pipe operator

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

important points about literal styles

  • The new line are preserved
  • Leading spaces or indentation are removed.
  • Strings can have with or without quotation marks

storing the new line using 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
  • new line are not preserved
  • new line are converted to spaces.
string1: >
   Line1
   line2
   "line3"   
  line4