YAML Multiline Strings: Literal | vs Folded > and the Chomping Marks
Quick answer
Use | when the line breaks in your text matter, and > when you are wrapping one long paragraph over several lines. Then decide what happens at the end of the string: no mark keeps one final newline, - removes it, and + keeps every trailing blank line too.
If you are still deciding whether YAML is the right format, see choosing a config format. This article covers what comes next.
Literal and folded: two block scalar styles
The YAML 1.2.2 specification defines two block scalar styles in section 8.1: literal and folded. As Wikipedia's YAML overview puts it, one preserves newlines and the other folds them. Both start with an indicator character, then the text goes on the following lines, indented more than the key.
Literal: | keeps the lines
In the literal style, the spec says all indented characters are content, including white space, and empty lines are not folded. What you type is what you get:
literal: |
some
textThe value is "some\ntext\n". This is the right choice for shell scripts, SQL, certificates, and anything else where each line is meaningful.
Folded: > joins the lines
The folded style is subject to line folding. A single line break between two lines of text becomes a space:
folded: >
some
textThe value is "some text\n". Two details matter here. A blank line in the block becomes a real line break, so that is how you write a paragraph break. And lines that start with extra white space (the spec calls them more-indented lines) are not folded, so an indented list inside a folded block keeps its line breaks.
description: >
First paragraph, wrapped
over two lines.
Second paragraph.That gives "First paragraph, wrapped over two lines.\nSecond paragraph.\n".
Chomping: strip, clip, keep
Chomping controls how the final line break and any trailing empty lines are treated. The spec lists three methods, and you pick one with a character placed right after | or >.
| Mark | Name | Final line break | Trailing blank lines | | --- | --- | --- | --- | | - | Strip | Removed | Removed | | none | Clip (default) | Kept | Removed | | + | Keep | Kept | Kept |
There is no character for clip. It is what you get when you write no mark at all.
Here is the same one-line text under each mark, from the spec's own pattern (example 8.4):
strip: |-
text
clip: |
text
keep: |+
textThe values are "text", "text\n" and "text\n". The difference between clip and keep shows up only when blank lines follow the text. Add one blank line after text in each block and keep becomes "text\n\n", while strip and clip stay the same as before.
One edge case from the spec (example 8.6): a block that holds only empty lines gives "" for strip and clip, and "\n" for keep.
Why the last newline bites
Usually a trailing newline is harmless. It bites when you compare the value to another string, hash it, or pass it as a single-line argument. A value read with | will not equal the same words typed in plain quotes, because of that final \n. If your program expects no trailing newline, write |- or >-. The same family of problem shows up in plain files, as in why trailing newlines break parsers.
Keep (+) is rarer. Reach for it when trailing blank lines are part of the data, for example a block of text you will concatenate with another block and want to keep the spacing.
Indentation and the 1-9 indicator
You normally never write an indentation indicator. The parser looks at the first non-empty line of the block and uses its indentation. That fails when your text itself begins with spaces, because the parser cannot tell indentation from content.
For that case you add a digit from 1 to 9 after the style character. Per the spec, the content indentation level is the indentation of the block's parent plus that digit:
note: |2
indented first line
second lineThe value is " indented first line\nsecond line\n". The first line keeps two spaces of its own because the declared indentation is two.
The digit and the chomping mark can be combined in either order, as in >1- from the spec's header example. The spec also says a processor should write an explicit indicator only when detection would fail, so you'll rarely see one in the wild. If a block scalar throws an error you cannot explain, check the layout against YAML indentation errors.
How Carets fits in
Block scalars are easy to get wrong. The difference between |, >, |- and |+ is a final newline you cannot see. Carets is a native notes and code editor for iPhone, iPad, and Mac, and it fits this job for a few concrete reasons:
- Syntax highlighting for config files. YAML keys, values and block scalar headers are colored, so a stray indent stands out before you commit the file.
- Plain text, portable files. Your YAML stays a plain file you can open anywhere, and it sits organized in files, projects, and tags alongside the notes that explain it.
- Native and fast on iOS and Mac. Fix a config value from your phone without opening a full IDE.
If you also write YAML at the top of Markdown notes, YAML front matter uses the same rules.
Download Carets for iPhone, iPad, and Mac on the App Store.
Frequently Asked Questions
What is the difference between | and > in YAML?
| is the literal style and keeps every line break in the block. > is the folded style and turns single line breaks into spaces, while blank lines and more-indented lines stay as line breaks.
Which chomping mode is the default?
Clip. With no indicator the string keeps one final newline and drops any trailing blank lines. There is no explicit clip character.
How do I get a string with no trailing newline?
Add a minus sign: |- or >-. That is strip chomping, and it removes the final line break and any trailing blank lines.
When do I need an indentation indicator?
When the first content line starts with spaces that belong to the text, because the parser would otherwise take them as indentation. Write a digit from 1 to 9 after the style character, such as |2.