Fundamentals 5 min read

Mastering YAML Multi‑Line Strings: Preserve Paragraphs or Flatten Text

This guide explains how to write multi‑line text in YAML configuration files, covering techniques to keep paragraph breaks when displaying the value and methods to collapse line breaks for a single‑line output, complete with code examples and practical differences.

Programmer DD
Programmer DD
Programmer DD
Mastering YAML Multi‑Line Strings: Preserve Paragraphs or Flatten Text

When to Keep Paragraphs or Collapse Lines in YAML

Sometimes a configuration file needs to store a block of text. Two common requirements are:

Display the text exactly as written, preserving paragraph breaks.

Write the text over several lines for editing convenience, but display it as a single line without extra breaks.

Scenario 1: Keep Paragraphs in Both Config and Display

Example string:

I am a coder.<br/>My blog is didispace.com.<br/>

Method 1: Use \n for line breaks

string: "I am a coder.
        My blog is didispace.com."

The \n sequence forces a line break when the value is rendered. The trailing backslash ( \) is required to continue the string on the next line without adding an extra space.

Method 2: Use YAML block scalars

Three block scalar indicators behave differently: |: preserves line breaks and adds one empty line at the end. |+: preserves line breaks and adds two empty lines at the end. |-: preserves line breaks without adding extra lines.

string: |
  I am a coder.
  My blog is didispace.com.

string: |+
  I am a coder.
  My blog is didispace.com.

string: |-
  I am a coder.
  My blog is didispace.com.

Scenario 2: Write Over Multiple Lines but Display as One Line

Example string:

I am a coder.My blog is didispace.com.<br/>

Method 1: Directly write the string on one line

string: 'I am a coder.
         My blog is didispace.com.'

Both single and double quotes work because no escape sequences are needed.

Method 2: Use folded scalars

Folded scalars replace line breaks with spaces unless a trailing indicator modifies the behavior: >: does not fold line breaks and adds one empty line at the end. >+: does not fold line breaks and adds two empty lines at the end. >-: does not fold line breaks and adds no extra line.

string: >
  I am a coder.
  My blog is didispace.com.

string: >+
  I am a coder.
  My blog is didispace.com.

string: >-
  I am a coder.
  My blog is didispace.com.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DevOpsYAMLstring formattingMultiline Strings
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.