Jay Taylor's notes

back to listing index

In YAML, how do I break a string over multiple lines?

[web search]
Original source (stackoverflow.com)
Tags: yaml yml stackoverflow.com
Clipped on: 2016-03-14

In YAML, I have a string that's very long. I want to keep this within the 80-column (or so) view of my editor, so I'd like to break the string. What's the syntax for this?

In other words, I have this:

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

and I'd like to have this (or something to this effect):

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

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

asked Sep 24 '10 at 19:47
Image (Asset 2/8) alt=
jjkparker
2,22151827
up vote 215 down vote accepted
>
  This is a very long sentence
  that spans several lines in the YAML
  but which will be rendered as a string
  without carriage returns.

http://symfony.com/doc/current/components/yaml/yaml_format.html

answered Sep 24 '10 at 19:54
Image (Asset 4/8) alt=
Matt Williamson
14.9k63859
   upvote
  flag
Thanks, but you can't wrap this syntax in quotes, it seems: the quotes appear as literals in the resulting string. – jjkparker Sep 27 '10 at 16:55
13 upvote
  flag
I don't think you add the quotes. The > infers it is a string. – Matt Williamson Sep 28 '10 at 4:31
22 upvote
  flag
s/infers/implies/ – Justin Force Sep 4 '13 at 18:23
4 upvote
  flag
+1 for the documentation reference. – Paolo Stefan Feb 12 '14 at 12:27
2 upvote
  flag
Thanks, that was my own answer ;) – Rvanlaak Jul 2 '15 at 8:55

There are 5 6 NINE different ways to write multi-line strings in YAML.

Block scalar styles (>, |)

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

> Folded style may be what you want:

Key: >
  this is my very very very
  long string

this is my very very very long string\n

If you use the | literal style method, a newline character is inserted into the string at each end of line:

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

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.

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.

plain style (no escaping, no # or : combinations, limits on first character):

Key: this is my very very very 
  long string

double-quoted style (\ and " must be escaped by \):

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

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'

Summary

In this table, _ means space character. \n means "newline character" (\n in JavaScript), except for the "in-line newlines" row, where it means literally a backslash and an n).

                      >     |            "     '     >-     >+     |-     |+
-------------------------|------|-----|-----|-----|------|------|------|------  
Trailing spaces   | Kept | Kept |     |     |     | Kept | Kept | Kept | Kept
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 dbl nl's => |      |      |     |     |     |      | Kept |      | Kept  
In-line newlines  | No   | No   | No  | \n  | No  | No   | No   | No   | No
Single quote      | '    | '    | '   | '   | ''  | '    | '    | '    | '
Double quote      | "    | "    | "   | \"  | "   | "    | "    | "    | "
Backslash         | \    | \    | \   | \\  | \   | \    | \    | \    | \
" #", ": "        | Ok   | Ok   | No  | Ok  | Ok  | Ok   | Ok   | Ok   | Ok
Can start on same | No   | No   | Yes | Yes | Yes | No   | No   | No   | No
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        
  spaces."
- '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."
]
answered Feb 11 '14 at 10:27
Image (Asset 5/8) alt=
Steve Bennett
14.6k136087
   upvote
  flag
So much for generating your own parser ;) – twicejr Sep 9 '14 at 19:51
   upvote
  flag
Perhaps add the line-folding style (>-) as well: yaml.org/spec/1.2/spec.html#id2779048 – equaeghe Oct 7 '15 at 10:19
1 upvote
  flag
Holy crap, thanks. YAML is nuts. 6 different formats for multi-line strings. – Steve Bennett Oct 7 '15 at 23:17
1 upvote
  flag
this should be the accepted answer. MUCH more useful than only describing > as in the current accepted answer. – m_x Oct 29 '15 at 14:56
1 upvote
  flag
>- is a combination of 8.1 "Block Scalar Styles" and 8.1.1.2 "Block Chomping Indicator". In this case, > (Block Scalar Style = Literal) and - (Block Chomping Indicator = Strip). Other combinations are possible, such as |-, >+, and |+ (+ is Keep, which keeps all trailing empty lines instead of just one). So... that brings us to 9? :-) – Brandon Bonds Dec 2 '15 at 16:48

To preserve newlines use |, for example:

|
  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."

answered Mar 12 '13 at 15:28
Image (Asset 6/8) alt=
Ali Shakiba
5,88753768
   upvote
  flag
This seems to work fine for me with two lines but not with three? – cboettig Oct 23 '13 at 22:09
7 upvote
  flag
   upvote
  flag
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 – cboettig Oct 24 '13 at 19:48
   upvote
  flag
This example does NOT convert to new lines in rails 4! – Rubytastic Nov 13 '13 at 16:03
   upvote
  flag
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... – Alain1405 Jan 14 '14 at 16:52

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

?
 >
 multi
 line
 key
:
  value
answered Oct 24 '14 at 21:17
Image (Asset 7/8) alt=
Mohsen
27.5k1888125
   upvote
  flag
this is bloody awesome – avi Aug 9 '15 at 7:42

In case you're using yml 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 yml 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 yml:

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.";
answered May 6 '15 at 15:02
Image (Asset 8/8) alt=
Rvanlaak
1,207514
   upvote
  flag
This looks like a bug. Did you have a chance to file a bug report? – dreftymac Jan 20 at 4:45

Your Answer

asked

5 years ago

viewed

80907 times

active

19 days ago

Hot Network Questions

Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Programmers
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. more (13)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Seasoned Advice (cooking)
  6. Home Improvement
  7. Personal Finance & Money
  8. Academia
  9. more (9)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. Arqade (gaming)
  7. Bicycles
  8. Role-playing Games
  9. more (21)
  1. Mathematics
  2. Cross Validated (stats)
  3. Theoretical Computer Science
  4. Physics
  5. MathOverflow
  6. Chemistry
  7. Biology
  8. more (5)
  1. Stack Apps
  2. Meta Stack Exchange
  3. Area 51
  4. Stack Overflow Careers
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.3.14.3342