Blog

YAML Multiline Strings: Literal Block vs Folded Style

YAML has two multiline string styles — literal (|) and folded (>). Here's what each one does, when to use it, and how newlines are handled.

YAML's multiline string syntax trips up almost every developer the first time. The | and > characters look similar but behave very differently.

Literal block scalar (|)

The | style preserves every newline exactly as written:

yaml
message: |
  Line one
  Line two
  Line three

This produces the string "Line one\nLine two\nLine three\n". Each line break stays a line break. Use this for scripts, code snippets, or any content where newlines are meaningful.

Folded block scalar (>)

The > style folds newlines into spaces — useful for long prose that you want to wrap in your editor without affecting the output:

yaml
description: >
  This is a very long sentence that I want
  to wrap across multiple lines in my YAML file
  but keep as a single paragraph in the output.

This produces "This is a very long sentence that I want to wrap across multiple lines in my YAML file but keep as a single paragraph in the output.\n". Use this for human-readable descriptions, commit messages, or prose that doesn't care about line breaks.

Trailing newlines: the chomping indicator

Both styles append a trailing newline by default. Add a - to strip it or + to keep all trailing newlines:

yaml
no_trailing: |-
  Line one
  Line two

keep_trailing: |+
  Line one
  Line two

IndicatorBehavior
`or>`
`-or>-`
|+ or >+All trailing newlines preserved

Indentation inside a block

YAML detects indentation from the first content line. You can override it explicitly if needed (e.g., |2 for 2-space indent). Content lines must all be indented at least as much as the opening.

Quick decision guide

  • Preserving line breaks (shell scripts, code, poems)? → |
  • Wrapping long prose without semantic newlines? → >
  • No trailing newline (embedding in a template)? → |- or >-

Try it

Paste your YAML into the JSON/YAML/TOML converter to validate it and see the parsed output. If your multiline string isn't behaving as expected, convert to JSON to see exactly what value YAML is producing. For common pitfalls, see how to fix YAML errors.

Got a file to check?
Paste it in. Errors come back in plain English.
Open Config Validator →