396

In a config file, I have a key to which I wish to assign a URL. The problem is that YAML interprets : and - characters as either creating mappings or lists, so it has a problem with the line

url: http://www.some-site.example/

(both because of the colon following HTTP and the hyphen in the middle)

Is there an explicit way to escape : and -? Or would it work to just put the whole thing in single quotes and call it a day?

1

8 Answers 8

367

Quotes:

"url: http://www.some-site.example/"

To clarify, I meant “quote the value” and originally thought the entire thing was the value. If http://www.some-site.example/ is the value, just quote it like so:

url: "http://www.some-site.example/"
8
  • 51
    It depends on the parser, apparently. This didn't work with Jekyll YAML.
    – ptomato
    Sep 30, 2013 at 3:55
  • 1
    YAMLDotNet also renders quotes.
    – Dr1Ku
    Nov 19, 2013 at 13:54
  • 2
    Nothing better? Because then quotes need to be escaped themselves, which does not solve the problem but simply moves it forward ... Jan 27, 2015 at 14:47
  • 3
    Well, it would be much cooler to have an error-proof document, just like markdown, so the non-tech guys of the team can edit it (Eg. locale files in Rails) without any risk of breaking it! Jan 28, 2015 at 11:00
  • 1
    @ivan_pozdeev: The quotes go around the entire string. - 'PS4="+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }"'
    – Ry-
    Dec 19, 2017 at 5:39
301

What also works and is even nicer for long, multiline texts, is putting your text indented on the next line, after a pipe or greater-than sign:

text: >
    Op dit plein stond het hoofdkantoor van de NIROM: Nederlands Indische 
    Radio Omroep

A pipe preserves newlines, a gt-sign turns all the following lines into one long string.

7
  • 9
    ...and a newline is added at the end, which is usually not what you want.
    – equaeghe
    Mar 23, 2015 at 15:55
  • 83
    @equaeghe: You can use >- or |- in order to prevent this.
    – dtoux
    Mar 30, 2015 at 18:23
  • 8
    This is a wonderful solution. Completely avoids having to escape other characters in your text. +++90000 points
    – bennlich
    Jun 25, 2015 at 16:48
  • is it possible to have a list of multilines somehow? I've tried - > but the items after the first are ignored.
    – ffghfgh
    Jul 1, 2015 at 17:05
  • 3
    @ffghfgh - urg! I can't figure out how to format the code properly in the comment and now I can't edit the original comment. Basically, I used a list item with a pipe, like this: - | and then on a new line I indented the list item text so that the first character lined up with the pipe. I hope that helps, it worked for me in a Rails 4.2 locale file.
    – Spakman
    May 25, 2016 at 11:27
107

According to the YAML spec, neither the : nor the - should be a problem. : is only a key separator with a space after it, and - is only an array indicator at the start of a line with a space after it.

But if your YAML implementation has a problem with it, you potentially have lots of options:

- url: 'http://www.some-site.example/'
- url: "http://www.some-site.example/"
- url:
    http://www.some-site.example/
- url: >-
    http://www.some-site.example/
- url: |-
    http://www.some-site.example/

There is explicitly no form of escaping possible in "plain style", however.

4
  • 1
    The linter of travisCI complains about colons in unusual - lint.travis-ci.org
    – koppor
    Jan 23, 2016 at 15:12
  • For Travis CI, it seems colons inside single quotes are OK.
    – Malvineous
    Apr 25, 2016 at 9:29
  • Be caution when pasting a json in a yaml file. It's quite common to naively add a space after :. Mar 25, 2022 at 15:10
  • Agreed. You should not have to quote it. If you do, the library has a bug.
    – Kenmore
    Aug 31, 2022 at 22:05
36

Quotes, but I prefer them on the just the value:

url: "http://www.some-site.example/"

Putting them across the whole line looks like it might cause problems.

25

Another way that works with the YAML parser used in Jekyll:

title: My Life: A Memoir

Colons not followed by spaces don't seem to bother Jekyll's YAML parser, on the other hand. Neither do dashes.

2
  • … are character entities part of YAML? And is what Jekyll uses actually YAML?
    – Ry-
    Sep 30, 2013 at 4:02
  • Jekyll claims to use it: jekyllrb.com/docs/frontmatter I can't find anything about character entities in the YAML spec, so I suspect Jekyll is aberrant, but I think this answer serves well for people Googling "yaml escape colon" like me ;-)
    – ptomato
    Sep 30, 2013 at 4:26
8

If you're using @ConfigurationProperties with Spring Boot 2 to inject maps with keys that contain colons then you need an additional level of escaping using square brackets inside the quotes because spring only allows alphanumeric and '-' characters, stripping out the rest. Your new key would look like this:

"[8.11.32.120:8000]": GoogleMapsKeyforThisDomain

See this github issue for reference.

2
  • 1
    This is the most helpful in my current use case. Thanks
    – anand
    Feb 4, 2020 at 20:52
  • Also helped in my case
    – vikifor
    Mar 27 at 7:50
8

I came here trying to get my Azure DevOps Command Line task working. The thing that worked for me was using the pipe (|) character. Using > did not work.

Example:

steps:
- task: CmdLine@2
  inputs:
    script: |
      echo "Selecting Mono version..."
      /bin/bash -c "sudo $AGENT_HOMEDIRECTORY/scripts/select-xamarin-sdk.sh 5_18_1"
      echo "Selecting Xcode version..."
      /bin/bash -c "echo '##vso[task.setvariable variable=MD_APPLE_SDK_ROOT;]'/Applications/Xcode_10.2.1.app;sudo xcode-select --switch /Applications/Xcode_10.2.1.app/Contents/Developer"
4
  • 3
    I used same thing in .gitlab-ci.yml, but until I needed pipe '|' in script - it fails silently on it :(
    – Dalibor
    Feb 25, 2020 at 10:58
  • This appears to have nothing to do with the question. The question is about colons and dashes in YAML. This looks like a solution to an entirely different problem. Jun 23, 2022 at 9:45
  • @StephenOstermiller I don't agree that this is related to an entirely different problem. The answer adds additional information about a specific use case and based on the upvotes it has helped some people. When I Google for something and a related question shows up in the results, I find it very useful to share my solution so that when others search for the same thing, they can find the solution as well. Jun 27, 2022 at 6:10
  • 2
    You need the pipe, because the pipe preserves newlines within the block while ">" converts the whole block into a single line.
    – Nicktar
    Mar 1 at 13:07
5

GitHub actions complain about

curl -L -H "Authorization: token ${{ secrets.TOKEN }}"  https://example.com/try.txt

but it's fine when there is no space after colon, like

curl -L -H "Authorization:token ${{ secrets.TOKEN }}"  https://example.com/try.txt
2
  • 2
    Use multi-line commands, either by starting with > or | and then write you command on the next line.
    – AJenbo
    Feb 17, 2022 at 16:51
  • 1
    This appears to have nothing to do with the question. The question is about colons in YAML. This looks like a solution to an entirely different problem. Jun 23, 2022 at 9:44

Your Answer

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

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