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 header | String 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
-
jjkparker. (2010, September 24). How do I break a string in YAML over multiple lines? Stack Overflow. Question and examples. ↩
-
菜鸟教程. (n.d.). YAML 入门教程. Runoob. Basic syntax and indentation (in Chinese). ↩
-
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