BudiBadu Logo
Samplebadu

YAML by Example: Multi Line Strings

1.2

Handle long text blocks. Preserve or fold line breaks.

Code

# Literal Block Scalar (|)
# Preserves newlines
description: |
  This is a text block.
  Line breaks are preserved.
  
  Indentation is ignored.

# Folded Block Scalar (>)
# Converts newlines to spaces
summary: >
  This text will be
  folded into a single
  line.
  
  Double newlines start a new paragraph.

# Strip chomping (|-) removes trailing newlines
# Keep chomping (|+) preserves all trailing newlines
code: |-
  console.log("Hello");

Explanation

The literal scalar | preserves line breaks exactly as written, perfect for code snippets or preformatted text. The folded scalar > converts single newlines into spaces, creating one long line, which helps write readable paragraphs in your source file.

Chomping indicators control trailing newlines. The minus sign - strips all trailing newlines, the plus + keeps them all, and the default clips to one newline. Choose based on whether you want clean output or need to preserve exact formatting.

Code Breakdown

3
| starts a literal block. Newlines inside are real newlines.
10
> starts a folded block. Newlines become spaces.