How do I split a YAML string across lines without adding a newline?

June 16, 2025

Suppose your app reads a notification from a YAML configuration file. You want to wrap the message in your editor and still send it as one line. This is the problem behind a Stack Overflow question about splitting a long string.1 Use >- after the setting name and indent the message underneath it. The leading spaces tell the YAML parser which lines belong to the message setting.2

message: >-
  Your order is ready for pickup.
  Bring your receipt.

The app receives Your order is ready for pickup. Bring your receipt. The > selects folded style. It turns the line break between these two equally indented lines into a space. The - strips the final line break after the period. You can type the message directly, including any quotation marks you want the reader to see.3

What if the receipt reminder should appear on its own line? Change just the first line to message: |-. The vertical bar selects literal style, which keeps the line break inside the message. Here are the four results for the same text, assuming you pressed Enter after the final period. Each \n represents one newline character.3

YAML headerString the app receives
message: >-Your order is ready for pickup. Bring your receipt.
message: >Your order is ready for pickup. Bring your receipt.\n
message: |-Your order is ready for pickup.\nBring your receipt.
message: |Your order is ready for pickup.\nBring your receipt.\n

Where you wrap matters with >-. If you split the word pickup across two lines, it becomes pick up. And if you leave a blank line between the two sentences, YAML keeps a newline there. To send one continuous sentence, wrap at a space between words and keep the lines at the same indentation.3

Footnotes

  1. jjkparker. (2010, September 24). How do I break a string in YAML over multiple lines? Stack Overflow. Question and examples. ↩

  2. 菜鸟教程. (n.d.). YAML 入门教程. Runoob. Basic syntax and indentation (in Chinese). ↩

  3. YAML Language Development Team. (2021, October 1). YAML Ain’t Markup Language (YAML™), revision 1.2.2. YAML.org. §8.1, Block Scalar Styles. ↩ ↩2 ↩3

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-06-16). How do I split a YAML string across lines without adding a newline?. https://caiborui.com/blog/2025-06-16-en-yaml-multiline-strings

@misc{borui2025-ae91c964,
  author = {borui},
  title = {{How do I split a YAML string across lines without adding a newline?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-06-16-en-yaml-multiline-strings}
}