Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 947
Anestis Kivranoglou Avatar asked Feb 15 '17 11:02

Anestis Kivranoglou


People also ask

How many spaces is an indent?

An indent has one space between the first character of the indent and the first character of each subsequent indent, while a tab has two spaces between each tab. Paragraphs have three spaces between each paragraphs.

Does YAML allow spaces?

Spaces are allowed in keys according to the specification and for those you don't need quotes (double or single, each with their own use). It is just a scalar string containing a space.

What does indentation mean in YAML?

In YAML block styles, structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line. To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently.

Do not use for indentation in YAML?

A YAML file use spaces as indentation, you can use 2 or 4 spaces for indentation, but no tab. In other words, tab indentation is forbidden: Why does YAML forbid tabs? Tabs have been outlawed since they are treated differently by different editors and tools.


2 Answers

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 
like image 116
flyx Avatar answered Oct 02 '22 18:10

flyx


The YAML spec for v 1.2 merely says that

In YAML block styles, structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line.

To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently. Note that most modern editors may be configured so that pressing the tab key results in the insertion of an appropriate number of spaces.

The amount of indentation is a presentation detail and must not be used to convey content information.

So you can set the indent depth to your preference, as long as you use spaces and not tabs. Interestingly, IntelliJ uses 2 spaces by default.

like image 34
sevenr Avatar answered Oct 02 '22 17:10

sevenr