2946

I have a very long string:

Key: 'this is my very very very very very very long string'

I would like to express it over multiple shorter lines, e.g.,

Key: 'this is my very very very ' +
     'long string'

I would like to use quotes as above, so that I don't need to escape anything within the string.

2
  • 10
    Quick tip: you cannot place comment inside scalar, so you cannot comment part of multiline key or value. Have to move required lines out of declaration. stackoverflow.com/questions/20890445/… Commented Jun 30, 2021 at 8:31
  • 5
    Use this reference: yaml-multiline.info Commented May 4, 2022 at 10:17

8 Answers 8

6572

There are 5 6 NINE (or 63*, depending how you count) different ways to write multi-line strings in YAML.

TL;DR

  • Use > if you want to break a string up for readability but for it to still be treated as a single-line string: interior line breaks will be stripped out, there will only be one line break at the end:

      key: >
        Your long
        string here.
    
  • Use | if you want those line breaks to be preserved as \n (for instance, embedded markdown with paragraphs).

      key: |
        ### Heading
    
        * Bullet
        * Points
    
  • Use >- or |- instead if you don't want a line break appended at the end.

  • Use "" if you need to split lines in the middle of words or want to literally type line breaks as \n:

      key: "Antidisestab\
       lishmentarianism.\n\nGet on it."
    
  • YAML is crazy.

Block scalar styles (>, |)

These allow characters such as \ and " without escaping, and add a new line (\n) to the end of your string.

> Folded style removes single newlines within the string (but adds one at the end, and converts double newlines to singles):

Key: >
  this is my very very very
  long string

this is my very very very long string\n

Extra leading space is retained and causes extra newlines. See note below.

Advice: Use this. Usually this is what you want.

| Literal style turns every newline within the string into a literal newline, and adds one at the end:

Key: |
  this is my very very very 
  long string

this is my very very very\nlong string\n

Here's the official definition from the YAML Spec 1.2.2

Scalar content can be written in block notation, using a literal style (indicated by “|”) where all line breaks are significant. Alternatively, they can be written with the folded style (denoted by “>”) where each line break is folded to a space unless it ends an empty or a more-indented line.

Advice: Use this for inserting formatted text (especially Markdown) as a value.

Block styles with block chomping indicator (>-, |-, >+, |+)

You can control the handling of the final new line in the string, and any trailing blank lines (\n\n) by adding a block chomping indicator character:

  • >, |: "clip": keep the line feed, remove the trailing blank lines.
  • >-, |-: "strip": remove the line feed, remove the trailing blank lines.
  • >+, |+: "keep": keep the line feed, keep trailing blank lines.

"Flow" scalar styles ( , ", ')

These have limited escaping, and construct a single-line string with no new line characters. They can begin on the same line as the key, or with additional newlines first, which are stripped. Doubled newline characters become one newline.

plain style (no escaping, no # or : combinations, first character can't be ", ' or many other punctuation characters ):

Key: this is my very very very 
  long string

Advice: Avoid. May look convenient, but you're liable to shoot yourself in the foot by accidentally using forbidden punctuation and triggering a syntax error.

double-quoted style (\ and " must be escaped by \, newlines can be inserted with a literal \n sequence, lines can be concatenated without spaces with trailing \):

Key: "this is my very very \"very\" loooo\
  ng string.\n\nLove, YAML."

"this is my very very \"very\" loooong string.\n\nLove, YAML."

Advice: Use in very specific situations. This is the only way you can break a very long token (like a URL) across lines without adding spaces. And maybe adding newlines mid-line is conceivably useful.

single-quoted style (literal ' must be doubled, no special characters, possibly useful for expressing strings starting with double quotes):

Key: 'this is my very very "very"
  long string, isn''t it.'

"this is my very very \"very\" long string, isn't it."

Advice: Avoid. Very few benefits, mostly inconvenience.

Block styles with indentation indicators

Just in case the above isn't enough for you, you can add a "block indentation indicator" (after your block chomping indicator, if you have one):

- >8
        My long string
        starts over here
- |+1
 This one
 starts here

Note: Leading spaces in Folded style (>)

If you insert extra spaces at the start of not-the-first lines in Folded style, they will be kept, with a bonus newline. (This doesn't happen with flow styles.) Section 6.5 says:

In addition, folding does not apply to line breaks surrounding text lines that contain leading white space. Note that such a more-indented line may consist only of such leading white space.

- >
    my long
      string
                    
    many spaces above
- my long
      string
                    
    many spaces above

["my long\n string\n \nmany spaces above\n","my long string\nmany spaces above"]

Summary

In this table: _ means space character, \n means "newline character" except were noted. "Leading space" refers to an additional space character on the second line, when the first is only spaces (which establishes the indent).

> | >- |- >+ |+ " '
Spaces/newlines converted to:
Trailing space → _ _ _ _ _ _
Leading space → \n_ \n_ \n_ \n_ \n_ \n_
Single newline → _ \n _ \n _ \n _ _ _
Double newline → \n \n\n \n \n\n \n \n\n \n \n \n
Final newline → \n \n \n \n
Final double newline → \n \n \n\n \n\n
How to create a literal:
Single quote ' ' ' ' ' ' ' ' ''
Double quote " " " " " " " \" "
Backslash \ \ \ \ \ \ \ \\ \
Other features
In-line newlines with literal \n 🚫 🚫 🚫 🚫 🚫 🚫 🚫 🚫
Spaceless newlines with \ 🚫 🚫 🚫 🚫 🚫 🚫 🚫 🚫
# or : in value 🚫
Can start on same
line as key
🚫 🚫 🚫 🚫 🚫 🚫

Examples

Note the trailing spaces on the line before "spaces."

- >
  very "long"
  'string' with

  paragraph gap, \n and        
  spaces.
- | 
  very "long"
  'string' with

  paragraph gap, \n and        
  spaces.
- very "long"
  'string' with

  paragraph gap, \n and        
  spaces.
- "very \"long\"
  'string' with

  paragraph gap, \n and        
  s\
  p\
  a\
  c\
  e\
  s."
- 'very "long"
  ''string'' with

  paragraph gap, \n and        
  spaces.'
- >- 
  very "long"
  'string' with

  paragraph gap, \n and        
  spaces.

[
  "very \"long\" 'string' with\nparagraph gap, \\n and         spaces.\n", 
  "very \"long\"\n'string' with\n\nparagraph gap, \\n and        \nspaces.\n", 
  "very \"long\" 'string' with\nparagraph gap, \\n and spaces.", 
  "very \"long\" 'string' with\nparagraph gap, \n and spaces.", 
  "very \"long\" 'string' with\nparagraph gap, \\n and spaces.", 
  "very \"long\" 'string' with\nparagraph gap, \\n and         spaces."
]

*2 block styles, each with 2 possible block chomping indicators (or none), and with 9 possible indentation indicators (or none), 1 plain style and 2 quoted styles: 2 x (2 + 1) x (9 + 1) + 1 + 2 = 63

Some of this information has also been summarised here.

Sign up to request clarification or add additional context in comments.

17 Comments

Among the 63 syntaxes, do you think there is a single one that allows you to spell in multiple lines a string that should not have newlines nor spaces? I mean what one would write as "..." + "..." in most programming languages, or backslash before newline in Bash.
@pepoluan I tried every possible combination and found only one that allows for spaceless concatenation: put double quotes around the string and a backslash before newline (and indentation.) Example: data:text/plain;base64,dGVzdDogImZvb1wKICBiYXIiCg==
@wvxvw on the contrary, I think YAML is the worst format for many common use-cases (e.g., config files), not least because most people are drawn in by its apparent simplicity only to realize much later that it's an extremely complex format. YAML makes wrong things look right - for example, an innocuous colon : within one string in a string array makes YAML interpret it as an array of objects. It violates the principle of least astonishment.
@VickyChijwani We are lost in translation. My comment was sarcastic, but sarcasm doesn't translate well. I very much agree with you.
I always struggled to remember which one of '|' or '>' keeps or removes the line feeds. At some point I realized that, if read from left to right, the operators tell you how they transform the string. '|' has the same height on both sides meaning that the string will also stay the same height; while '>' is smaller on the right than on the left, meaning it will "compress" the string from many to just one line. Just wanted to leave that mnemonic here for those who haven't discovered it yet.
|
1478

Using YAML folded style. The indention in each line will be ignored. A line break will be inserted at the end.

Key: >
  This is a very long sentence
  that spans several lines in the YAML
  but which will be rendered as a string
  with only a single carriage return appended to the end.

See The YAML Format.

You can use the "block chomping indicator" to eliminate the trailing line break, as follows:

Key: >-
  This is a very long sentence
  that spans several lines in the YAML
  but which will be rendered as a string
  with NO carriage returns.

In either case, each line break is replaced by a space.

There are other control tools available as well (for controlling indentation for example).

See YAML Multiline.

8 Comments

Thanks, but you can't wrap this syntax in quotes, it seems: the quotes appear as literals in the resulting string.
Somehow a carriage return is added right after the end of the translation in my app. That way Javascript sees it as multiple lines and fails. {{- 'key'|trans -}} does not work either.
In my experience, this syntax appends a \n at the end of the string. This may or may not be what you are looking for.
each line break is replaced by a space <-- but a double-line-break will be a line break.
@Rvanlaak and @rich-remer Use the block chomper to avoid the newline at the end: >-
|
246

To preserve newlines, use |. For example,

Key: |
  This is a very long sentence
  that spans several lines in the YAML
  but which will be rendered as a string
  with newlines preserved.

is translated to "This is a very long sentence‌**\n** that spans several lines in the YAML‌**\n**, but which will be rendered as a string‌**\n** with newlines preserved.\n"

6 Comments

This seems to work fine for me with two lines but not with three?
Thanks, works fine there just like you say. For some reason in Pandoc's yaml headers I need to repeat the | on each line, for reasons that are not obvious to me: groups.google.com/forum/#!topic/pandoc-discuss/xuqEmhWgf9A
Isn't an issue the fact that if I write: - field1: | one two - field1: | three for' I get: one\ntwo\n and three\nfor? I would aspect the \n after 2 to do not be there...
When using multiline cat with delimiter this causes leading spaces (which are necessary for YAML) to be added to output.
@Rubytastic to have those break lines also in your HTML page generated by Rails, you need some precautions. I already answered here: stackoverflow.com/questions/10982706/…
|
174

1. Block notation (plain, flow-style, scalar): Newlines become spaces and extra newlines after the block are removed

---
# Note: It has one new line after the string
content:
    Arbitrary free text
    over multiple lines stopping
    after indentation changes...

...

Equivalent JSON

{
 "content": "Arbitrary free text over multiple lines stopping after indentation changes..."
}

2. Literal block scalar: A literal block scalar | will include the newlines and any trailing spaces. But it removes extra newlines after the block.

---
# After string we have two spaces and two new lines
content1: |
 Arbitrary free text
 over "multiple lines" stopping
 after indentation changes...


...

Equivalent JSON

{
 "content1": "Arbitrary free text\nover \"multiple lines\" stopping\nafter indentation changes...  \n"
}

3. + indicator with literal block scalar: keep extra newlines after block

---
# After string we have two new lines
plain: |+
 This unquoted scalar
 spans many lines.


...

Equivalent JSON

{
 "plain": "This unquoted scalar\nspans many lines.\n\n\n"
}

4. – indicator with literal block scalar: means that the newline at the end of the string is removed.

---
# After the string, we have two new lines
plain: |-
 This unquoted scalar
 spans many lines.


...

Equivalent JSON

{
 "plain": "This unquoted scalar\nspans many lines."
}

5. Folded block scalar (>):

will fold newlines to spaces and, but removes extra newlines after the block.

---
folded_newlines: >
 this is really a
 single line of text
 despite appearances


...

Equivalent JSON

{
 "fold_newlines": "this is really a single line of text despite appearances\n"
}

1 Comment

Nr. 1 is so simple that it seems it was missed by the most upvoted solution, but it's exactly what the OP requested: from multiple lines in the YAML file, get a one-line string.
97

To concatenate long lines without whitespace, use double quotes and escape the newlines with backslashes:

key: "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\
  orincididuntutlaboreetdoloremagnaaliqua."

2 Comments

Thanks, this really helped me to define Docker volumes over multiple lines! If someone has the same problem, here is my solution on an Online YAML Parser
Ah finally. I was trying to wrap long ssh-keys in Puppet's Hiera yaml files over multiple lines but always got unwanted spaces until I used your answer. Thanks.
56

You might not believe it, but YAML can do multi-line keys too:

?
 >
 multi
 line
 key
:
  value

5 Comments

Explanation needed (what is "?").
@ilyaigpetrov exactly as written, "multi-line" key. Usually you do things like key:value, but if your key contains new-line, you can do it as described above
Any example of a real-world use-case for this?
@ilyaigpetrov the ? is the key indicator (as in key in a mapping). In many situations you may leave out the key indicator, when the (required) value indicator : after the key makes parsing unambiguous. But that is not the case, you'll have to use this to explicitly mark the key.
PROPOSAL: any team members that do this must buy coffee for the team for 1 week - per offense :)
22

In case you're using YAML and Twig for translations in Symfony, and want to use multi-line translations in JavaScript, a carriage return is added right after the translation. So even the following code:

var javascriptVariable = "{{- 'key'|trans -}}";

Which has the following YAML translation:

key: >
    This is a
    multi line
    translation.

Will still result into the following code in HTML:

var javascriptVariable = "This is a multi line translation.
";

So, the minus sign in Twig does not solve this. The solution is to add this minus sign after the greater than sign in YAML:

key: >-
    This is a
    multi line
    translation.

Will have the proper result, multi line translation on one line in Twig:

var javascriptVariable = "This is a multi line translation.";

1 Comment

This looks like a bug. Did you have a chance to file a bug report?
11

For situations where the string might contain spaces or not, I prefer double quotes and line continuation with backslashes:

key: "String \
  with long c\
  ontent"

But note about the pitfall for the case that a continuation line begins with a space, it needs to be escaped (because it will be stripped away elsewhere):

key: "String\
  \ with lon\
  g content"

If the string contains line breaks, this needs to be written in C style \n.

See also this question.

1 Comment

If it is stripped away elsewhere, i.e. not in that position, can you update your answer with information about where it will be stripped away. Please also write which parser (for which language) does that? I have only seen parsers strip such leading/trailing spaces in multiline quotes strings in place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.