new line "\n" in yaml file

Viewed 35242

I'd like to do this

test: >
  This is a long string
  \n\n\n
  with new lines

But it prints out the "\n" instead of making new lines.

I know it's possible to do

test: "This is a long string
      \n\n\n
      with new lines"

But i'd rather not add quotes everywhere if possible. Thanks for the help!

EDIT: I'd rather not use empty lines. In other words, i'd like to use \n to show empty lines to make my yml file more readable.

4 Answers

Came across this while asking the same question. I found another solution using the \n in string. When I used eval(str) it worked as expected.

Sorry for the use of Python, but I don't code in Ruby. However, I was looking at the documentation and it appears to have the same eval function: https://www.rubydoc.info/stdlib/core/Kernel:eval

Example YAML:

text: >
    "This is the 1st row sentence.\n\n\n"
    "This is the 4th row sentence."

Example Python:

print(eval(yaml_string))

# returns:
# This is the 1st row sentence.
#
#
# This is the 4th row sentence.

Hope this is helpful to someone.

to print a new line in yaml you can do the following code I am using the degub module msg func like this

debug:
   msg:
     - 'hello'
     - 'name'
Related