YAML block scalars are essential features for managing multi-line strings in YAML configurations. They provide a clean and readable way to represent large text blocks, preserving or folding newlines as needed.
YAML offers two types of block scalars:
Literal block scalars are denoted by the pipe character (|). They maintain all line breaks in the text.
description: |
This is a multi-line
description that preserves
line breaks.
Each line will appear as-is.
Folded block scalars use the greater-than symbol (>). They convert single line breaks to spaces, creating a single long line.
about: >
This is a long description
that will be folded into a
single line. Empty lines
create paragraph breaks.
Block scalars can be further customized with indentation and chomping indicators:
example: |2-
This block is indented by 2 spaces
and will strip all trailing newlines.
Block scalars are particularly useful for:
They're often used in application configurations, Docker files, and Kubernetes manifests.
To further enhance your YAML skills, explore these related topics:
Understanding block scalars is crucial for effectively managing complex string data in YAML. They provide a powerful tool for maintaining readability and precision in your configurations.