Breaking Strings Over Multiple Lines

How to break strings over multiple lines

YAML gives you the ability to break strings over multiple lines. There are a few ways of accomplishing this. First, you can use the “literal” operator | to preserve text formatting. For example,

text: |
   This is a line
   This is another line

Will result in something like “This is a line\nThis is another line\n” with the line breaks preserved. The | operator will only strip out trailing newlines. You can modify this behavior using something called “chomping”. The “keep” chomping method |+ will preserve all line breaks while the “strip” chomping method |- will remove all line breaks.

The other main style is by using the “folding” operator > that will replace line breaks with spaces in consecutive lines. For example:

text: >
   This is a line
   This is another line

Will result in something like “This is a line This is another line”. 

text: >
   This is a line

   This is another line

Will however result in something like “This is a line\nThis is another line”. You can utilize the same chomping modifier with the > operator.

How you quote strings can also impact how the line breaks are parsed. Double quotes will preserve the \n as a line break character while single quotes will treat it as part of the string itself.

Questions?

Check out our FAQ page for more questions, or see Language Features.

%d bloggers like this: