Sublime Forum

Auto-set Syntax for specific filenames?

#1

Hey,
Thanks a lot for the surprisingly quick answers I’ve had to other questions on here by the way, not only is Sublime a fantastic app - the support is brilliant.

I have another query please if anyone has the time. I’d like to automatically map the following file names and extensions to syntaxes when I open files of these types.

*.jake -> “JavaScript”
jsTestDriver.conf -> “YAML”
COMMIT_EDITMSG -> “Shell Script (Bash)”

jsTestDriver.conf for example would open as “Apache Conf” as the extension matches, but Google’s jsTestDriver.conf is written in YAML, so I’d like to override this if possible.

Thanks again.

1 Like

#2

Take a look at DetectSyntax [1]. It should do what you want. If not, let me know and I’ll see if I can make it do so.

[1] github.com/phillipkoebbe/DetectSyntax

0 Likes

#3

Thanks Phil, I got that working - The code is below incase any arrives here with the same problem.

{ "new_file_syntax": "JavaScript", "default_syntaxes": { "name": "YAML/YAML", "rules": { "file_name": ".*jsTestDriver\\.conf.*" }] }, { "name": "JavaScript/JavaScript", "rules": { "file_name": ".*\\.jake$" }] }, { "name": "ShellScript/Shell-Unix-Generic", "rules": { "file_name": ".*COMMIT_EDITMSG.*" }] }] }

One thing that caught me out was for exact filename matches, eg “jsTestDriver\.conf”, “jsTestDriver.conf” or “^jsTestDriver\.conf$” all didn’t work - so I needed to add the .* on each side.

0 Likes

#4

Did you create a new DetectSyntax.sublime-settings in your User folder? If so, you should rename “default_syntaxes” to just “syntaxes” or you’ll overwrite all of the default syntaxes (unless that’s what you want). *

Regarding the regular expression, it is matched against the full path and file name in the buffer, so just

jsTestDriver\.conf

wouldn’t work. However,

.*jsTestDriver\.conf$

should, unless of course the file name doesn’t end in .conf.*

0 Likes

#5

ahh, I did that create that file yes, but I’d just copied and edited what was in the default file - hence that “default_syntaxes”.
Thanks for that I’ll rename it now.

0 Likes