Sublime Forum

Syntax highlighting woes

#1

HI Folks

As much as I love ST, I’m tearing my hair out here. One might think setting a file’s syntax highlighting would be dead simple. Wow, I was so wrong.

Ok lets say there is a file called .snippets. We want it to be of type Shell Script

Now here is the issue, if you open command palette and type “Bash” then enter, it works! Voila. You can even quit ST3 and when you come back to the workspace and it’s still set.

Ok now this is where it goes bad. Simply close the file, (to declutter your open files). Open it again later. And BAMM! no more highlighting.

This means every time you open that same file you have to set highlighting. Wow thats insane.

Ok well anyway, after googling around, I found a convoluted ‘solution’. Install PackageResourceManager, browse to a tmLanguage file and manually add it. Are you kidding me?!

Even then, because it’s cached, it doesn’t work immediately. I’m not sure what to do, delete the cache?

I have tens of little files all over the place that need custom syntax highlighting.

Please tell me there is a better way …

0 Likes

#2

Have you tried View -> Syntax -> Open all with current extension as…?

0 Likes

#3

This is exactly the solution. Basically, you can override file associations with the “extensions” settings of syntax-specific settings (Preferences > Settings - More > Syntax specific - User), which is what the above command does.

0 Likes

#4

There are plugins that allow you to include a text snippet inside the file to trigger filetype recognition, which is especially useful for files without an extension.

For example:

github.com/SublimeText/Modelines
github.com/kvs/STEmacsModelines
github.com/eukaryote/SublimeFiletypeConf

The first two above support lots of additional functionality other than filetype recognition. The last is a very simple ST3 plugin I made that just does filetype recognition based on a snippet like ‘sublimeconf: filetype=python’ in a comment inside the file.

I just found the following plugin though, which looks like a really nice approach:

github.com/facelessuser/ApplySyntax
github.com/facelessuser/ApplySyntax/tree/ST3

It allows for the creation of rules to automatically detect filetype and apply syntax based on criteria like file extension, the first line of the file, and regular expressions matching file content. My original desire that motivated the plugin I made was a really simple way of specifying the syntax in the file for files without extensions that have a first line like “#!/usr/bin/env python”, and I didn’t want to have to specify a full path to a language file like Modelines above requires. ApplySyntax recognizes a script with a first line like above as a python file automatically without requiring a modeline or a special comment, and you can easily add additional rules for custom types that it doesn’t know about already using the ‘binary’ rule, where binary=‘foo’ would mean that the shebang line of a script is something like “#!/usr/bin/env foo”.

0 Likes