+16

Override default associated syntax definition

guillermooo fa 14 anys updated by Phillip Koebbe fa 13 anys 7
Association between file types and syntax definitions is established in syntax definitions by means of the "filetypes" element. While normally this works fine, sometimes you might want to use an alternate syntax definition for a file type that already has a generic syntax definition associated with it, and to which Sublime gives precedence. To override the default syntax definition, it would be necessary to be able to specify a preferred syntax definition for a file type. If this preferred syntax wasn't found, Sublime would fall back to the current system.

Example:

file_types: { 
"sublime-build": { 
"syntax": "Packages/PackageDev/Support/Sublime Build.tmLanguage",
}
}
+7
You can do this via .sublime-settings files: this is how the "View/Syntax/Open all with current extension..." menu operates. By placing "extensions": ["txt"] in Python.sublime-settings, for example, .txt files would be opened with python syntax highlighting, taking precedence over what's specific in the .tmLanguage files
+5

But could this be done on a per project basis?


Is it possible (and if so, how? - if not, could it be?) to set the default syntax for certain file extensions on a per project basis?
that is, I wanna open .html files in one project using a certain tmLanguage file
but I wanna keep the default on other pojects.

+1
And how about files without an extension, like Gemfile?
@telephono in case you haven't found this yet, I created a gist which helps with setting syntax highlighting using a Python script, it also supports files without extensions: https://gist.github.com/925008
+4
How could we do this if we have, f.e. the .rb extension which is used for all ruby files, but I want files ending with _spec.rb to use the Rspec syntax?

I tried using the following in rspec.sublime-settings but it doesn't work:

{
    "extensions":
    [
        "spec.rb"
    ]
}
+5
Same goes for Cucumber Step files which end in .rb, but are styled differently. (via a Cucumber steps bundle.)

What would be nice would to have fileTypes be a regex, so you could put whatever path in there you like to identify the file.  +1 for making it also configurable at the project level.  That way, for Rails projects I could just pop in a set of defaults for how I work.
+1
@Dave Sanders,

If you haven't found it yet, you can try DetectSyntax[1], a plugin I created (based on what @Jean Mertz did) just for this use case.