11

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.

1

4 Answers 4

22

try this

test: |+
  This is a long string


  with new lines

| after the key helps in beginning of multiline text indented and maintain the indentation for the long string.

3
1

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.

1
  • Another thing I noticed is that YAML escape strings the \n, so in Ruby you could also parse the text replacing the \\n with \n. Jun 26, 2022 at 0:49
-1

You can do this :

test: >+
  This is a long string



  with new lines
0
-4

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.