new line "\n" in yaml file
Asked Answered
I

5

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.

Injector answered 20/7, 2012 at 15:59 Comment(1)
Duplicate. #10983206Scribe
D
23

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.

Demo answered 20/7, 2012 at 16:45 Comment(3)
i'd like to use the \n notation instead of using empty lines (just for readability). Is there another way using the \n char?Injector
then you have to use the qoutes...other solution i don't have any ideaDemo
more you can get here yaml.org/YAML_for_ruby.html#three_trailing_newlines_in_literalsDemo
C
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.

Cassatt answered 26/6, 2022 at 0:39 Comment(2)
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.Cassatt
not working for meThrower
T
0

If none of the above solutions work with you, try adding white-space: pre-line on your HTML element, and setting your translation value like this :

key: "Line1\nLine2"
Thrower answered 28/8, 2024 at 16:43 Comment(0)
P
-1

You can do this :

test: >+
  This is a long string



  with new lines
Perrone answered 20/7, 2012 at 16:8 Comment(0)
G
-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'
Gala answered 7/1, 2021 at 15:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.