Categories
newline string syntax yaml

How do I break a string in YAML over multiple lines?

2236

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.

2

1378

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.

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

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 https://yaml-multiline.info/

8

  • 1

    Thanks, but you can’t wrap this syntax in quotes, it seems: the quotes appear as literals in the resulting string.

    – jjkparker

    Sep 27, 2010 at 16:55

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

    – Rvanlaak

    May 6, 2015 at 14:48

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

    Mar 22, 2018 at 0:41

  • 10

    each line break is replaced by a space <– but a double-line-break will be a line break.

    Aug 5, 2018 at 10:57

  • @Rvanlaak and @rich-remer Use the block chomper to avoid the newline at the end: >-

    Nov 27, 2018 at 15:46

1378

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.

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

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 https://yaml-multiline.info/

8

  • 1

    Thanks, but you can’t wrap this syntax in quotes, it seems: the quotes appear as literals in the resulting string.

    – jjkparker

    Sep 27, 2010 at 16:55

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

    – Rvanlaak

    May 6, 2015 at 14:48

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

    Mar 22, 2018 at 0:41

  • 10

    each line break is replaced by a space <– but a double-line-break will be a line break.

    Aug 5, 2018 at 10:57

  • @Rvanlaak and @rich-remer Use the block chomper to avoid the newline at the end: >-

    Nov 27, 2018 at 15:46

217

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.\n

8

  • This seems to work fine for me with two lines but not with three?

    – cboettig

    Oct 23, 2013 at 22:09

  • 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, 2013 at 19:48

  • 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, 2014 at 16:52


  • When using multiline cat with delimiter this causes leading spaces (which are necessary for YAML) to be added to output.

    Apr 30, 2016 at 14:27


  • @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/…

    – Diego D

    Sep 23, 2016 at 10:45