DevSwitch.net

YAML Multiline Strings: When to Use | or > and How to Chomp

YAML multiline strings come in two block styles: | keeps your line breaks and > folds them into spaces. A chomping indicator after either one decides what happens to the newlines at the end. Here is exactly what each form returns, so you can pick one without guessing.

Sophie ParkerPublished ยท 8 min read
Lines of text kept on separate lines on one side and folded into one line on the other, illustrating YAML multiline strings
Key takeaways The short version, before the details.
  1. | (literal) keeps every line break; > (folded) turns single line breaks into spaces.
  2. Add - to drop the final newline, + to keep all trailing newlines, or nothing to keep exactly one.
  3. Paste any snippet into the YAML to JSON converter to see the real string, with every \n spelled out.

YAML multiline strings cheat sheet

A multiline value written with | or > is called a block scalar. The first character picks the style, and an optional second character picks the chomping. Every row below uses the same input: two lines, line one and line two, followed by one blank line.

HeaderStyle and chompingLine breaks insideEnd of stringResulting string
|Literal, clipKeptExactly one newline"line one\nline two\n"
|-Literal, stripKeptNo newline"line one\nline two"
|+Literal, keepKeptEvery trailing newline"line one\nline two\n\n"
>Folded, clipBecome spacesExactly one newline"line one line two\n"
>-Folded, stripBecome spacesNo newline"line one line two"
>+Folded, keepBecome spacesEvery trailing newline"line one line two\n\n"

You can check the whole table in one go. Paste this into the YAML to JSON converter:

literal: |
  line one
  line two
literal_strip: |-
  line one
  line two
literal_keep: |+
  line one
  line two

folded: >
  line one
  line two
folded_strip: >-
  line one
  line two
folded_keep: >+
  line one
  line two

end: done

The JSON output shows each string with its newlines written as \n. That is the easiest way to see what your parser actually gets:

{
  "literal": "line one\nline two\n",
  "literal_strip": "line one\nline two",
  "literal_keep": "line one\nline two\n\n",
  "folded": "line one line two\n",
  "folded_strip": "line one line two",
  "folded_keep": "line one line two\n\n",
  "end": "done"
}

YAML pipe vs greater than: what is the difference?

Both styles start the value on the next line and end it when the indentation drops back. The only difference is what happens to the line breaks between your lines.

YAML literal block (|) keeps every line break

The pipe means "take this text as written". Each line break stays a line break, and any indentation beyond the block's own indent stays too. Use it whenever line structure matters: shell scripts, SQL, certificates, Markdown.

script: |
  echo 'start'
  if [ -f app.log ]; then
    tail -n 20 app.log
  fi

Result: "echo 'start'\nif [ -f app.log ]; then\n tail -n 20 app.log\nfi\n". The two extra spaces before tail are kept because they go beyond the block's indentation.

YAML folded style (>) joins lines with spaces

The greater-than sign means "this is one paragraph that I wrapped for readability". Single line breaks become spaces. A blank line becomes a real \n, so you can still write paragraphs.

description: >
  This service resizes uploaded images
  and stores them in the cache.

  It never touches the originals.

Result: "This service resizes uploaded images and stores them in the cache.\nIt never touches the originals.\n".

One rule surprises people: lines indented more than the rest are not folded. Their line breaks are kept, so a short command inside folded text survives.

note: >
  Run this first:
    npm install
  then start the server.

Result: "Run this first:\n npm install\nthen start the server.\n".

How do chomping indicators control trailing newlines?

Chomping only touches the end of the string. It never changes the line breaks in the middle.

  • Clip (no indicator): one final \n, and any trailing blank lines are dropped. This is the default.
  • Strip (-): no final newline at all. Pick it when the value is compared or joined with other text, such as a token, an ID or a one-line command.
  • Keep (+): the final newline plus every trailing blank line. Rarely needed, but useful for file contents that must end in blank lines.

The most common choice in practice is |-. It keeps the lines readable in the file and avoids a stray newline that can break a string comparison or end up inside a URL.

What is the YAML block scalar indentation indicator?

A parser works out the indentation of a block from its first non-empty line. That fails when your text itself should start with spaces: the parser treats those spaces as indentation and removes them.

The fix is a digit in the header that states the indentation explicitly, counted from the parent's indentation:

code: |2
    indented first line
  normal line

Result: " indented first line\nnormal line\n". The block indent is 2, so the first line keeps its extra two spaces. Without the digit, YAML would take 4 as the indent from the first line and then reject the less indented second line. The digit and a chomping indicator can be combined in either order, for example |2- or |-2.

How to write a YAML long string without a block scalar

Block scalars are not the only way to spread a long value over several lines. Plain and quoted strings can span lines too, and they fold like >: a single line break becomes a space, a blank line becomes \n.

Plain and single-quoted multiline strings

plain: This is a long value
  wrapped over two lines
single: 'It''s also fine
  in single quotes'

Result: "This is a long value wrapped over two lines" and "It's also fine in single quotes". Neither ends with a newline. Plain strings cannot contain : or #, and single quotes have no escapes apart from '' for a quote.

Double-quoted multiline strings with escapes

Double quotes add escape sequences, which gives you full control. Write \n for a line break, and end a line with a backslash to join it to the next line without a space.

message: "Hello\nWorld"
path: "/api/\
  v2/users"

Result: "Hello\nWorld" with a real line break, and "/api/v2/users". The backslash at the end of the line removes the line break and the leading spaces of the next line, which no block scalar can do.

How do you add a YAML line break inside a string?

Pick the method that matches the value:

  • Many real line breaks: a literal block with | or |-.
  • Long prose with the odd paragraph break: > with a blank line between paragraphs.
  • One line break in a short value: double quotes with \n, such as "first\nsecond".

Writing \n in a plain, single-quoted or block value does not work: there it stays two characters, a backslash and an n.

When to use YAML multiline for scripts and configs

A few patterns cover almost every real file:

  • CI steps and shell commands: run: | in workflow files, so each command sits on its own line. See the GitHub Actions workflow syntax for the run key.
  • Embedded files: a config file or script stored inside a Kubernetes ConfigMap uses | so the file keeps its lines and its final newline.
  • SQL queries: |- keeps the query readable and drops the trailing newline.
  • Descriptions and help text: > or >- lets you wrap long sentences in the file while the app receives one line.
steps:
  - name: Build and test
    run: |
      npm ci
      npm test
  - name: Report
    env:
      SUMMARY: >-
        Build finished on the main branch
        and all tests passed.

If you are still deciding whether YAML is the right format for a config, keep in mind that JSON has no multiline syntax at all, which is one reason scripts so often live in YAML. Going the other way, the JSON to YAML converter turns a JSON config into block-style YAML.

Common YAML block scalar mistakes

  • Indenting with tabs. YAML only allows spaces for indentation. The YAML to JSON converter shows the error with its line and column.
  • Expecting a comment to work. Inside a block scalar, # note is part of the text, not a comment.
  • Forgetting the default newline. Plain | adds a \n at the end. If a key, token or ID does not match, check for it and switch to |-.
  • Using > for code. Folding merges your lines into one, which breaks scripts. Use | for anything line-based.
  • Text after the indicator. Only a comment may follow | or > on the header line; the content starts on the next line.

The exact rules are in the YAML 1.2.2 specification, chapter 8 on block styles. The converter reads YAML with js-yaml using the YAML 1.2 rules, so what you see there is what a spec-compliant parser returns.

YAML block scalar converted into a JSON string with its line breaks made visible
YAML to JSON converter

Not sure what your multiline string really contains?

Paste the YAML and the JSON output shows every line break and trailing newline as \n, so you can tell at a glance whether you need |, |- or >.

Convert YAML to JSONRuns in your browser

Frequently asked questions

Put | or > after the key, then write the text on the following lines, indented further than the key. The pipe keeps each line break, while the greater-than sign joins lines with spaces. Plain and quoted strings can also span lines, but they fold line breaks into spaces.

The literal style | keeps every line break exactly as written, so it suits scripts and code. The folded style > turns single line breaks into spaces and keeps only blank lines as newlines, so it suits long sentences. Both end with one newline unless you add a chomping indicator.

It is a literal block with the strip chomping indicator. Line breaks inside the text are kept, but the final newline and any trailing blank lines are removed. Two lines a and b become "a\nb" instead of "a\nb\n". It is the usual choice for tokens, commands and SQL.

A plain | or > already keeps exactly one trailing newline. To keep the blank lines after the text as well, use the keep indicator, |+ or >+. In a double-quoted string, end the value with \n, for example "last line\n".

Yes. Single and double-quoted strings may continue on the next line. Each line break becomes a space, while a blank line becomes a newline. Double quotes also accept \n for a real line break, and a backslash at the end of a line joins it to the next line without any space.