Skip to content

YAML Block Style

Learn how YAML block styles (literal, folded, keep, strip) control line breaks and formatting for scalars, with examples of chomping indicators (-, default, +) and their effects on output.

The YAML Block Style is less compact and more easily understood by humans.

An example of block style:

host: tutorialreference-vm  
datacenter:   
  location: europe  
  cab: 123  
animals:  
  - lion  
  - elephant  
  - crocodile  

where:

  • the first line shows a key-value mapping. (key is host and value is tutorialreference-vm)
  • The second line shows a key-value mapping indentation. (it has an indentation under the datacenter)
  • The animals key shows a list indentation. (note indentation and dashes for the list items)

YAML Block Style

There are four types of block styles:

  • literal style
  • folded style
  • keep style
  • strip style

Consider this Block Chomping scenario:

%YAML 1.2
---
!!map {
? !!str "strip"
: !!str "# text",
? !!str "clip"
: !!str "# text\n",
? !!str "keep"
: !!str "# text\n",
}

And what follows is the output generate with three formats in JSON:

{
	"strip": "# text",
	"clip": "# text\n",
	"keep": "# text\n"
}

Chomping in YAML controls the final breaks and trailing empty lines which are interpreted in various forms.

Stripping

In stripping, the final line break and empty lines are excluded for scalar content. It is specified by the chomping indicator -.

Clipping

Clipping is considered as a default behavior if no explicit chomping indicator is specified.

The final break character is preserved in the scalar's content.

The best example of clipping is demonstrated in the example above. It terminates with newline \n character.

Keeping

Keeping refers to the addition with representation of + chomping indicator.

The additional lines are not subject to folding.

Navigation