Skip to content

Instantly share code, notes, and snippets.

@Siddhant-K-code
Created May 30, 2024 20:00
Show Gist options
  • Save Siddhant-K-code/eceeb86163821fc7f0fad3d747d3f86d to your computer and use it in GitHub Desktop.
Save Siddhant-K-code/eceeb86163821fc7f0fad3d747d3f86d to your computer and use it in GitHub Desktop.
`>` v/s `|` in YAML files

In YAML, the | and > characters are used to denote different styles of multi-line strings. Here's a brief explanation of each:

| (Literal Block Scalar)

The | character is used to preserve the exact formatting of the multi-line string, including line breaks and leading spaces. It's useful when you want to maintain the formatting of the text exactly as it is.

Example:

example_literal: |
  This is a literal block scalar.
  It preserves line breaks and
  leading spaces.

This will result in:

This is a literal block scalar.
It preserves line breaks and
leading spaces.

> (Folded Block Scalar)

The > character is used to fold the multi-line string, which means line breaks are converted to spaces. This is useful for long paragraphs of text where you want to preserve the paragraph structure but don't need the exact line breaks.

Example:

example_folded: >
  This is a folded block scalar.
  It folds line breaks into spaces,
  making the text more compact.

This will result in:

This is a folded block scalar. It folds line breaks into spaces, making the text more compact.

Comparison Summary

  • Literal Block Scalar (|): Preserves line breaks and leading spaces.
  • Folded Block Scalar (>): Converts line breaks to spaces, making the text more compact.

Choose | when you need to preserve the exact formatting, and > when you prefer a more compact representation that merges lines into a single paragraph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment