Sublime Forum

YAML syntax not detected automatically

#1

I’m using Ansible for configuring our servers. Ansible uses YAML-formatted files nearly everywhere. But only few files have a suffix of “.yml”. Sublime (3) considers the files “Plain text” so I don’t get syntax highlighting. I even added a hint by writing “—” into the first line of each file. But Sublime still thinks it’s just plain text.

How can I tell Sublime to set the syntax to YAML for such files regardless of the file name (because it doesn’t have a “.yml” suffix)?

0 Likes

#2

Add “%YAML” to the first line, it should then be detected automatically.

Alternatively you could tweak the first line regex in the YAML syntax or use packagecontrol.io/packages/ApplySyntax, which will prevent you from receiving updates to the syntax if you modify it.

0 Likes

#3

Thanks for the reply. Unfortunately putting “%YAML” into the first line of the YAML document didn’t make it open as YAML - Sublime 3 still considers it as “Plain Text”.

I will try ApplySyntax. Still wondering why Sublime does not consider a document starting with “—” at the beginning as YAML.

0 Likes

#4

Any text file can start with —, this is not an good indicator of YAML.

If you are not willing to use .yml you can always add your extension to file with Yaml definition (on linux /opt/sublime_text/Packages/YAML.sublime-package)

<key>fileTypes</key>
        <array>
                <string>yaml</string>
                <string>yml</string>
                <string>server_config</string>
        </array>

The other way is to create plugin for that.

0 Likes

#5

[quote=“alkuzad”]
If you are not willing to use .yml you can always add your extension to file with Yaml definition (on linux /opt/sublime_text/Packages/YAML.sublime-package)[/quote]

Better way is to use the “View -> Syntax -> Open all with current extension as… -> YAML”. That doesn’t require to change the syntax def.

Something I just realized: The “%YAML” first line detection was only added to 3084 so if you are on 3083 you won’t get that.

0 Likes

#6

I will second that statement. ‘—’ is a terrible indicator. What about files that use yaml frontmatter? Should the entire file be highlighted as YAML? What about markdown which uses things like ‘—’ to denote an hr tag; I could go on. Starting a file with ‘—’ is not a good way to identify a YAML file.

Editing your file yaml syntax is a good option. Using the built-in options in sublime to always open an extension with a syntax is a good option. ApplySyntax is also a good option for handling more complex scenarios but can handle simple extension scenarios as well.

0 Likes

#7

Alright - thanks. Looks that upgrading Sublime to the version that understands %YAML is the best way.

“View -> Syntax -> Open all with current extension as… -> YAML” is not an option. It’s not a matter of “unwillingness” to name the files *.yml. It’s just that Ansible requires the files to be named in a certain way and they wouldn’t work if they ended on “.yml”. For example the host variables defined in YAML need to match the FQDN of a host. If I added “.yml” then Ansible just wouldn’t find them any more. :smile:

UPDATE: I’m now using 3084 and “%YAML” works as expected. Thanks.

0 Likes