Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose multi-line command [duplicate]

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.

like image 250
jjkparker Avatar asked Sep 24 '10 19:09

jjkparker


3 Answers

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

TL;DR

  • Use > most of the time: interior line breaks are stripped out, although you get one at the end:

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

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

  • Use "..." if you need to split lines in the middle of words or want to literally type linebreaks 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

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:

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" (\n in JavaScript) except under "Other features". "Leading space" applies after the first line (which establishes the indent)

> | " ' >- >+ |- |+
Spaces/newlines converted as:
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
How to create a literal:
Single quote ' ' ' ' '' ' ' ' '
Double quote " " " \" " " " " "
Backslash \ \ \ \\ \ \ \ \ \
Other features
In-line newlines with \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.

like image 148
Steve Bennett Avatar answered Sep 19 '22 11:09

Steve Bennett


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/

like image 45
Matt Williamson Avatar answered Sep 18 '22 11:09

Matt Williamson


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"

like image 41
Ali Shakiba Avatar answered Sep 20 '22 11:09

Ali Shakiba