YAML multiline strings

YAML multiline strings: | vs >, chomping and indentation

YAML has two block styles for multi-line text, three ways to treat the newline at the end, and three more forms that can span lines without being blocks. They look alike in a file and give different strings to your program. This page shows each one next to the exact string it parses to, with every newline written as \n.

Every result on this page was produced by js-yaml, the parser this validator uses, and is checked by a test that parses the examples straight from this page.

At a glance

The six block scalar indicators

The same two lines, followed by an empty line and the next key. The first character picks the style: | (literal) keeps line breaks, > (folded) joins lines with a space. The optional second character picks the chomping: none keeps one final newline, - strips it, + keeps every trailing newline.

IndicatorYAMLValue of textIn words
|
text: |
  line one
  line two

next: x
"line one\nline two\n" Literal, clip: newlines kept, one final newline
|-
text: |-
  line one
  line two

next: x
"line one\nline two" Literal, strip: no final newline
|+
text: |+
  line one
  line two

next: x
"line one\nline two\n\n" Literal, keep: the trailing empty line too
>
text: >
  line one
  line two

next: x
"line one line two\n" Folded, clip: lines joined, one final newline
>-
text: >-
  line one
  line two

next: x
"line one line two" Folded, strip: one line, no newline
>+
text: >+
  line one
  line two

next: x
"line one line two\n\n" Folded, keep: the trailing empty line too

If you only remember one row: >- turns a long wrapped value into one line with no newline, which is usually what a description or a long command-line flag needs.

Literal | vs folded >

When to use | and when to use >

Use | when the line breaks mean something: shell scripts, SQL, certificates, config files embedded in a ConfigMap. Use > for prose you only wrapped to keep the file readable: descriptions, commit messages, long one-line commands.

Literal: a script keeps its lines

YAML
run: |
  npm ci
  npm test
Value of run
"npm ci\nnpm test\n"

Folded: wrapped prose becomes one line

YAML
description: >-
  Builds the image, runs the tests
  and pushes the result to the registry.
Value of description
"Builds the image, runs the tests and pushes the result to the registry."
Chomping indicators

Chomping: what happens to the last newline

Chomping decides what happens to the final line break and any empty lines after the text. It works the same way for | and >.

Clip (no indicator)

Keeps exactly one final newline and drops trailing empty lines. This is the default, and the reason most block values end in \n.

Strip (-)

Removes the final newline and every trailing empty line. Use it for values that must not end in a newline: tokens, IDs, versions, file paths, single-line commands.

Keep (+)

Keeps the final newline and every trailing empty line. Rarely needed; it exists for text where trailing blank lines matter, such as a file you write out byte for byte.

At the very end of a file

The results on this page assume the file ends with a newline, as almost every file does. If a clipped block is the last thing in a file that has no final newline, parsers disagree: js-yaml still adds the \n, PyYAML 6.0.3 does not. Use - when the last newline matters.

Indentation indicator

Indentation indicators: |2 and >2

The parser takes the block's indentation from its first non-empty line. When that line has to start with spaces of its own, the parser cannot tell your spaces from the block's indentation, and the next line looks less indented than the block. A digit after the indicator states the indentation (relative to the parent key), so the extra spaces stay in the value. It combines with chomping in either order: |2- or |-2.

Without the indicator: a parse error

text: |
    indented first line
  second line

js-yaml reports bad indentation of a mapping entry on line 3: the block was set to 4 spaces by line 2, so the 2-space line ends it.

With |2: the leading spaces are kept

YAML
text: |2
    indented first line
  second line
Value of text
"  indented first line\nsecond line\n"
How folding works

How > treats empty lines and more-indented lines

Folding is not "replace every newline with a space". Two rules from the YAML spec decide which line breaks survive.

An empty line becomes a newline

Lines next to each other are joined with a space. An empty line between them ends the paragraph: it becomes one \n, so you can write paragraphs in a folded block.

YAML
text: >
  one
  two

  three
Value of text
"one two\nthree\n"

More-indented lines are not folded

A line indented further than the block keeps its line breaks and its extra spaces, so a list or code sample inside folded prose stays as written.

YAML
text: >
  intro
    - item a
    - item b
  outro
Value of text
"intro\n  - item a\n  - item b\noutro\n"
Quoted multi-line strings

Single- and double-quoted strings across lines

Quoted strings may continue on the next lines. They fold like >: a line break becomes a space, an empty line becomes a newline, and the indentation of continuation lines is dropped. Only double quotes understand escapes.

FormYAMLValue of textWhy
Single
text: 'It''s one line
  and the same line

  new line'
"It's one line and the same line\nnew line" Folding as with >. '' is the only escape: a single quote.
Single
text: 'C:\new\temp'
"C:\\new\\temp" Backslashes are plain characters (shown here as JSON \\).
Double
text: "first\nsecond\tTabbed"
"first\nsecond\tTabbed" \n and \t are escapes for a newline and a tab.
Double
text: "line one
  line two"
"line one line two" A line break inside the quotes folds to a space.
Double
text: "https://example.com/very/long/\
  path?page=2"
"https://example.com/very/long/path?page=2" A \ at the end of a line joins the lines with no space.
Plain multi-line scalars

Unquoted text over several lines

A plain (unquoted) value can also continue on more-indented lines, and folds the same way. It cannot contain : or #, and a # starts a comment that ends the value. It is fine for short wrapped prose; for anything else, use a block scalar.

Folds like >, with no final newline

YAML
text: line one
  line two

  line three
Value of text
"line one line two\nline three"

A # cuts the value

YAML
text: line one
  line two # a comment, not text
Value of text
"line one line two"
Common mistakes

Common mistakes with multiline strings

A trailing newline nobody asked for

With plain | or > the value ends in \n. In a script body that is harmless, but a value meant as one line (a token, a version, a path, an argument) now carries a newline into string comparisons, URLs and headers. Use |- or >- for those.

YAML
token: |
  abc123
Value of token
"abc123\n"

A # inside a block scalar is text, not a comment

Everything indented inside a block belongs to the string, including lines that look like YAML comments. That is right for shell comments in a script, and a surprise when you meant to comment out a line of YAML.

YAML
script: |
  echo hi # still part of the script
  # so is this line
Value of script
"echo hi # still part of the script\n# so is this line\n"

Tabs in the indentation

YAML indentation must be spaces. The line below is indented with a tab, and js-yaml stops with tab characters must not be used in indentation. A tab after the indentation, inside the text, is fine and stays in the value.

text: |
	indented with a tab

A line less indented than the first one

The first non-empty line sets the indentation of the whole block. A later line with fewer spaces is not part of the string; here it breaks the mapping. Indent every line of the block at least as far as the first, or use an indentation indicator when the first line starts with spaces.

script: |
    echo one
  echo two

Further reading

To reuse a block of configuration instead of repeating it, see YAML anchors, aliases and merge keys. For values that change type when left unquoted, such as NO or 3.10, see YAML gotchas.

Check your YAML in the validator →