How to Break a String in YAML over Multiple Lines

How to Break a String in YAML
Learn the methods for writing multi-line strings in YAML.

Having a long string in YAML is a frustrating issue. You mostly want to keep your string within the view of your editor. It’s not an issue that you can’t solve though! YAML allows you to break a string over multiple lines, which can be helpful when the string is too wide for your screen. 

In this article, I’ll discuss the definition and uses of multi-line strings. I’ll also guide you through the methods to write multi-line strings on YAML and will discuss the parts for each method. First, let’s go over what YAML is before we dig any deeper.

What Is a YAML String?

YAML is a human-readable data serialization format. You can use it to store data for programs. You can often use YAML files to store configuration files too. YAML allows you to break a string over multiple lines. This can be helpful when the string is too wide for your screen. Alternatively, in Python, you can enclose a string in triple quotes to split it across multiple lines.

YAML gained popularity as an alternative for those seeking more control over their formats, like JSON or XML. Its object serialization capabilities also make it easier than others when you need certain types of information packed into one file without losing any part of the original.

Now, I’ll explain the methods you can use to write multi-line strings on YAML. 

Methods to Write Multi-Line Strings on YAML

You can write multi-line strings on YAML in 2 common ways

  1. Block scalar format
  2. Flow scalar format 

While both ways have their benefits and drawbacks, one may be more suitable for your purposes. That depends on the type of data you’re dealing with. Find out which one is best for your particular needs below. First, I’ll explain the block scalar format and its 3 parts. 

1. Block Scalar Format

The block scalar format controls how you interpret new lines. This makes it easy for you to get a different result than you expected. A block scalar header has 3 parts:

1. Block Style Indicator 

The block style indicator shows how the new lines behave inside the block. It has 2 styles: 

  1. Literal style [indicated by pipe (|)] 
  2. Folded style [Indicated by right angle bracket (>)] 

You must indent the content of a block scalar up to the level specified in its metadata. These measurements don’t include leading spaces. 

Each block scalar has indented content. Leading spaces are also included up to the scalar’s indentation level. You can adjust this level on each line by adding or removing leading zeros, depending on your preference for clarity versus space usage.

Screenshot of block chomping scenario
An example of a block chomping scenario.

Example

  • Literal

| # Empty header↓

  • Folded

>1 # Indentation indicator↓

{ “literal\n“, 

” folded\n“}

2. Block Chomping Indicator (>-, |-, >+, |+)

Screenshot of a YAML code for block chomping indicator.
Chomping will affect training lines and empty lines in a block scalar.

The chomping indicator is responsible for what happens with new lines at the end of the string. It has 3 chomping methods:

  1. Clip: Adds a new line at the end of the string.

c-chomping-indicator(CLIP)  ::= “”

  1. Strip: [indicated by (-)] Removes all new lines.

c-chomping-indicator(STRIP) ::= ‘-‘

  1. Keep: [indicated by (+)] Keeps track of all added new lines.

c-chomping-indicator(KEEP)  ::= ‘+’

Examples

  • strip: |-

   text↓

  • clip: |

   text↓

  • keep: |+

   text↓

{ “strip”: “text”,

  “clip”: “text\n“,

  “keep”: “text\n” }

3. Indentation Indicator

Indentation is crucial for writing clean and well-formatted code. For example, suppose you want your blocks to stay nicely aligned. In that case, they should have a suitable number of spaces. You should also indent your blocks exactly as their header states–either 1 through 9 for regular indenters or a unique icon if you’re using a new indenter instead.

Each block scalar has a content indentation level. The amount specified in your input sentence indents the space of each line up to this point. That said, anything after the indentation starts new lines without extra leading spaces on them until we reach our wrap-around distance at 1,000 characters per paragraph or less.

The block scalar’s indentation level is the number of indented spaces. A content item with an indicator character sets its level to this value plus one space unless CSS or HTML framework rules specify otherwise.

c-indentation-indicator ::=

  [x31-x39]    # 1-9

Example

detected

·

·# detected

  • |1

·explicit

  • detected

[ “detected\n“,

  “\n\n# detected\n“,

  ” explicit\n“,

  “\t\ndetected\n” ]

I’ve explained the first supported method for multi-line strings, so you can use it for your YAML strings. Now, I’ll talk in more detail about the second method.

2. Flow Scalar Format

The flow scalar format is a new way of writing CSS. It allows you to declare variables, nesting, and other features much more concisely. You can also benefit from modern processors using this format. This makes your code faster and more efficient.

Screenshot of a flow scalar code format in YAML
At first glance, you can immediately see the flow compared to the block shape of the code for the block scalar format.

The flow scalar format has 3 types

1. Single-Quoted

In YAML, you can use single-quoted strings to prevent the YAML parser from interpreting special characters. This can be helpful when you want to include a character like an apostrophe in your string. 

Example

‘Several lines of text,\n

  • ·containing ”single quotes.” Escapes (like \n) don’t do anything.\n
  • ·\n
  • ·Leave a blank line to add new lines.\n
  • ···Leading whitespace on lines is ignored.’\n

Result 

Several lines of text contain single quotes. Escapes (like \n) don’t do anything.\n

Leave a blank line to add new lines. Leading whitespace on lines is ignored.

2. Double-Quoted

You can use double-quoted strings to include special characters in your string. YAML will interpret the special characters in the string, regardless of whether you enclose it in single or double quotes.

Example

“Several lines of text,\n

  • ·containing \”double quotes.\” Escapes (like \\n) work.\nIn addition,\n
  • ·new lines can be esc\\n
  • ·aped to prevent them from being converted to a space.\n
  • ·\n
  • ·Leave a blank line to add new lines.\n
  • ···Leading whitespace on lines is ignored.”\n

Result 

Several lines of text containing “double quotes.” Escapes (like \n) work.\n

You can also escape new lines to prevent them from being converted to a space.\n

Leave a blank line to add new lines. Leading whitespace on lines is ignored.\n

3. Plain

Plain strings are just regular strings that don’t include any special characters. They also have limited escaping and can construct a single-line string with no new line characters. 

Example 

Several lines of text,\n

  • ·with some “quotes” of various ‘types.’\n
  • ·Escapes (like \n) don’t do anything.\n
  • ·\n
  • ·Leave a blank line to add new lines.\n
  • ···Additional leading whitespace is ignored.\n

Result

Several lines of text, with some “quotes” of various ‘types.’ Escapes (like \n) don’t do anything.\n

Leave a blank line to add new lines. Additional leading whitespace is ignored.

Now, I’m going to list what you need for the process of breaking a string. 

  • Text editor
  • Online YAML validator
  • Knowledge of basic computer programming concepts

After checking what you need, I’ll explain the 4-step process:

  1. Add a backslash (\) after the exclamation mark (!) to break the string
  2. Save your changes
  3. Open the file in an online YAML validator
  4. Click the Validate button
  5. Check if your code is valid (green Valid YAML message =valid/ red Invalid YAML= invalid)

The Bottom Line

To break a string over multiple lines in YAML, you must use Unicode line breaks (u+2026). This allows you to write long strings without them getting cut off on the screen. I’ve shown you how to do this in my examples in this article. I’ve also discussed the 2 methods for writing multi-line strings and the different types for each method. This is a handy trick to know when you need to format large blocks of text.

Have more questions about YAML strings? Check out the FAQ and Resources sections below!

Get The Latest Tech News Here

FAQ

Does YAML support multi-line strings?

Yes, YAML allows you to break a string over multiple lines. This can be helpful when the string is too wide for your screen. You can also do this using 2 methods: block scalar and flow scalar. 

How do I split a YAML string over multiple lines?

To break a string over multiple lines, use Unicode line breaks (u+2026). These breaks are invisible characters you can use to indicate where you should break a line.

How do you escape quotes in YAML?

To escape a literal double quote in single-quoted strings, you can use the backslash character (\) and follow it with another instance of it. Escaping quotes for embedded expressions is also similar to how you escape them in double-quoted text.

What is the pipe symbol in YAML?

The pipe symbol (“|”) in YAML keeps new lines intact. It signifies that you should interpret any indented text that follows as a multi-line scalar value. Learn more about this literal style here.

What is scalar in YAML?

A scalar is a YAML term for a single value. In YAML, scalars can be strings, numbers, booleans, or null. Quotes surround the strings, numbers are unquoted, booleans are “true” or “false”, and the symbol “null” represents null.

Resources

TechGenix’s Extracting Values from XML Guide

Learn how to read and extract values from XML in this handy guide.

TechGenix’s Skills that Every DevOps Engineer Needs Article

Check out the top skills every DevOps engineer needs in 2022 here.

TechGenix’s Guide on Using JSON with PowerShell 

Learn how you can use JSON with PowerShell if you’re an IT professional here.

TechGenix’s Article on CI/CD Software Delivery Pipelines 

Discover the latest in CI/CD software delivery pipelines here.

TechGenix’s Guide to Making String Evaluation Easier 

Learn how using PowerShell regular expressions can speed up your string evaluation here.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top