YAML Scalars

A scalar in YAML is a single, standalone value. It is not a list and not a group of key-value pairs. Scalars are the simplest form of data in YAML. Every string, number, boolean, null, and date value is a scalar.

Understanding scalars deeply helps in writing precise YAML files, especially when dealing with strings that span multiple lines, special characters, or values that look like one type but should be treated as another.

Types of YAML Scalars

Scalar TypeExampleNotes
Plain stringHello WorldNo quotes needed for simple text
Single-quoted string'It''s YAML'Escapes only single quote using ''
Double-quoted string"Line1\nLine2"Supports escape sequences like \n, \t
Integer100Whole numbers
Float3.14Decimal numbers
Booleantrue / falseLogical on/off values
Nullnull / ~Empty or missing value
Date2024-06-15ISO 8601 format

Plain Scalars (Unquoted Strings)

A plain scalar is a value written without any quotes. YAML reads it and determines the type automatically. Plain scalars work for most simple text values.

name: Alice
city: New Delhi
status: active

Plain scalars must not start with special characters like :, #, [, {, or &. If a value starts with these, it needs quotes.

Single-Quoted Scalars

Single quotes prevent YAML from interpreting any special characters inside the value. What is inside single quotes is treated as pure text — no escape sequences work here.

message: 'Hello World'
path: 'C:\Users\Alice\Documents'

The backslash in the path is treated as a literal backslash, not an escape character.

Escaping a Single Quote Inside Single Quotes

note: 'It''s a sunny day'

To include a single quote inside a single-quoted string, write two single quotes together. The result is one single quote in the final value.

Double-Quoted Scalars

Double quotes allow escape sequences. These are special combinations starting with a backslash that produce characters like newlines, tabs, or Unicode symbols.

greeting: "Hello\nWorld"
tab_example: "Column1\tColumn2"
unicode: "\u0041"     # This is the letter A
Escape SequenceMeaning
\nNew line
\tTab
\\Literal backslash
\"Double quote character
\uXXXXUnicode character

Scalar Style Diagram

+----------------------------------------------+
|           YAML Scalar Styles                 |
+----------------------------------------------+
|                                              |
|  Plain:          name: Alice                 |
|                                              |
|  Single-Quoted:  name: 'Alice Smith'         |
|                                              |
|  Double-Quoted:  name: "Alice\nSmith"        |
|                                              |
|  Block (Literal): |                          |
|                    Line 1                    |
|                    Line 2                    |
|                                              |
|  Block (Folded):  >                          |
|                    This is folded            |
|                    into one line             |
+----------------------------------------------+

Block Scalars – Literal Style

Block scalars are used for multiline text. The literal block style uses the pipe character | and preserves every line break exactly.

address: |
  123 Baker Street
  London
  UK

The value of address is three separate lines. Every line break is preserved.

Block Scalars – Folded Style

The folded block style uses the greater-than symbol >. It folds multiple lines into one paragraph, replacing line breaks with spaces.

description: >
  This is a long description
  that spans multiple lines
  but becomes one sentence.

The final value becomes: This is a long description that spans multiple lines but becomes one sentence.

Chomp Indicators

Chomp indicators control what happens to trailing newlines at the end of a block scalar.

IndicatorBehaviorExample
Default (no indicator)One trailing newline kept|
ClipSame as default|- removes all trailing newlines
Strip (-)All trailing newlines removed|-
Keep (+)All trailing newlines kept|+
# Strip trailing newlines
notes: |-
  Line one
  Line two

# Keep all trailing newlines
notes: |+
  Line one
  Line two

When to Use Which Style

SituationBest Style
Simple word or numberPlain (unquoted)
Value with special characters like colonsDouble-quoted
File path with backslashesSingle-quoted
Address or multi-line text with exact formattingLiteral block (|)
Long description textFolded block (>)

Summary

  • A scalar is any single standalone value in YAML
  • Plain scalars need no quotes for simple text
  • Single quotes treat everything as literal text, no escape sequences
  • Double quotes allow escape sequences like \n and \t
  • Literal block (|) preserves exact line breaks in multiline text
  • Folded block (>) joins lines into a single paragraph
  • Use chomp indicators (- or +) to control trailing newlines

Leave a Comment