Search code examples
rubyyamlamazon-elastic-beanstalk

YAML How many spaces per indent?


Is there any difference if i use one space, two or four spaces per indent level in YAML?

Are there any specific rules for space numbers per Structure type??

For example 4 spaces for nesting maps , 1 space per list item etc??

I am writing a yaml configuration file for elastic beanstalk .ebextensions and i am having really hard time constructing this correctly. Although i have valid yaml in YAML Validator elastic beanstalk seems to understand a different structure.


Solution

  • There is no requirement in YAML to indent any concrete number of spaces. There is also no requirement to be consistent. So for example, this is valid YAML:

    a:
     b:
         - c
         -  d
         - e
    f:
        "ghi"
    

    Some rules might be of interest:

    • Flow content (i.e. everything that starts with { or [) can span multiple lines, but must be indented at least as many spaces as the surrounding current block level.
    • Block list items can (but don't need to) have the same indentation as the surrounding block level because - is considered part of the indentation:
    a:    # top-level key
    - b   # value of that key, which is a list
    - c
    c:    # next top-level key
     d    # non-list value which must be more indented
    

    If you're using a linter, that might require a defined number of spaces, but the YAML spec itself and conforming YAML implementations don't.