Skip to Content

What Is YAML and Can You Write Multiline Strings?

YAML is short for “yet another markup language” or “YAML ain’t markup language,” the latter recursive acronym being used to emphasize that YAML is for data and not documents. YAML is a data serialization language used to write configuration files for deployment of infrastructure. For example, if you want to deploy a Docker application to Kubernetes, you would use a YAML file to configure the instance. YAML is human-readable, so it’s easy to create a file and read instructions from it during deployments.

If you’re new to YAML, it won’t be long before you realize you need a way to break up your strings into multiple lines, one of the most frequently asked YAML questions on the web. In this article, we’ll cover the two main approaches to YAML multiline strings, as well as some other frequently asked questions.

YAML and Whitespace: What to Know

YAML files use whitespaces and carriage returns, but the content is serialized when ingested into a specific resource. Developers creating a YAML file can format text using indents and spacing to make it easier to read. YAML treats whitespaces similarly to other languages, so you can have as many as needed to format a file.

How to Break Lines for YAML Multiline Strings

When ingesting YAML files, you might occasionally want to keep the line breaks in a multiline string. Take the following pseudocode as an example:

    key:

            item1

            item2

            item3

After every line item (e.g., item1, item2, and item3), you have a carriage return. If you leave the YAML syntax as is, the string becomes the following:

item1 item2 item3

This string format creates one long string without any carriage return formatting, so you might want to preserve the line breaks. Line breaks can be preserved with the literal operator. The literal operator is the pipe (“|”) character. To preserve line breaks, your YAML syntax should be changed to the following:

    key: |

            item1

            item2

            item3

Notice the pipe character after the “key” directive. When the application ingests the YAML file, it will convert to the following string:

item1\nitem2\nitem3

The “\n” character represents a new line. Printing this string to an application will display each item with an appended carriage return to preserve the formatting in the YAML instructions.

YAML Multiline Blocks vs. Long Strings

For long strings, you need to use multiline blocks to read a YAML file. For example, if you have a 250-word paragraph in a YAML file, you need to break the sentences into multiline blocks. You wouldn’t want to preserve the carriage returns except at the end of a paragraph. You can format a YAML file to use multiple lines for long strings and serialize the string to append spaces to each item instead of a new line character. 

To use a whitespace character instead of appending a new line, use the folded (“>”) operator:

    key: >

            item1

            item2

            item3

With the folded operator, YAML adds a space when it creates the string. The string looks like the following:

item1 item2 item3\n

YAML Multiline FAQs

How do you insert a line break in YAML?

Using the literal operator (“|”), any line breaks in a YAML string will be preserved. You create a line break using the carriage return keystroke in the same way that you create a line break in a standard document. The following example preserves the carriage return in a YAML string:

    key: |

            item1

            item2

            item3

Can YAML have blank lines?

Yes, a string represented as a block of text can also contain blank lines. The keep operator (“|+”)  preserves any blank empty lines along with the new line character. The following YAML is an example of a multiline string with two empty lines at the end of the block:

    key: |+

            item1

            item2

            item3

A YAML parser will display the following string from the above syntax:

item1\nitem2\nitem3\n\n

Does YAML care about whitespace?

A tab whitespace is an illegal character in YAML, so any indents must be used with a standard single space character. Tabs can be used in quoted strings, but they cannot be used to format a YAML file. Consistency in whitespace indents is important for readability of data.

Are spaces allowed in YAML?

Yes, you can have standard spaces throughout a YAML file. Spaces should be used for indents and not the tab character. Tab characters could cause errors during the parsing of a YAML file.

06/2024
The Pure Data Storage Platform for AI
Pure Storage® accelerates and simplifies AI deployments, enhancing their value to the enterprise.
White Paper
14 pages

Browse key resources and events

PURE//ACCELERATE® 2024
Experience Pure//Accelerate

Get inspired, learn from innovators, and level up your skills for data success.

See What’s Happening
PURE//ACCELERATE ROADSHOWS
An Event Is Coming Near You

Join us for a Pure//Accelerate event and discover storage solutions for the next generation and beyond.

Register Now
RESOURCE
The Future of Storage: New Principles for the AI Age

Learn how new challenges like AI are transforming data storage needs, requiring new thinking and a modern approach to succeed.

Get the Ebook
RESOURCE
Stop Buying Storage, Embrace Platforms Instead

Explore the needs, components, and selection process for enterprise storage platforms.

Read the Report
CONTACT US
Meet with an Expert

Let’s talk. Book a 1:1 meeting with one of our experts to discuss your specific needs.

Questions, Comments?

Have a question or comment about Pure products or certifications?  We’re here to help.

Schedule a Demo

Schedule a live demo and see for yourself how Pure can help transform your data into powerful outcomes. 

Call Sales: 800-976-6494

Mediapr@purestorage.com

 

Pure Storage, Inc.

2555 Augustine Dr.

Santa Clara, CA 95054

800-379-7873 (general info)

info@purestorage.com

CLOSE
Your Browser Is No Longer Supported!

Older browsers often represent security risks. In order to deliver the best possible experience when using our site, please update to any of these latest browsers.