Sublime Forum

Code Colouring - Language Aware?

#1

Hello,

Newbie trying our Sublime for the last few days … I’m wondering if its possible to have code colouring that is language aware? Say for example if I have a file with JS, CSS, HTML and PHP code the ability to set code colouring options for each language individually ?

Thanks …

0 Likes

#2

Sublimetext detects the language by the file extension. It uses automatically the right syntax highlighting for each language.

To manually change the behaviour, open the file in question then select View / Syntax / open all with current extension as…

and choose the syntax definition you like.

0 Likes

#3

So the answer is NO? If it detects language by file extension you can’t have specific syntax highlighting for different languages in a single file?

0 Likes

#4

You can, but only in very specific circumstances. For example, JavaScript inside of HTML:

This works because the rules for highlighting HTML know how to detect and highlight JavaScript as well. So it’s possible, but not in a way where you can arbitrarily choose whatever highlighting you want for whatever file content you want.

0 Likes

#5

just to check our understanding, do you want for example, all CSS to use one color scheme, all HTML to use another color scheme and all JS to use a third color scheme?

0 Likes

#6

@kingkeith he probably referes to syntax highlighting. You can have all four scripting languages being used in a single source file, for example for a PHP application:

image

and one syntax definition to rule them all.

0 Likes

#7

well ofc its possible, not sure why anyone would want to do such a thing though - normally the whole point is that different syntaxes color similar constructs the same way for easy identification

0 Likes

#8

Hi. @mcfc4heatons

I think it would be a good idea, if I’ve understood correctly, because different languages do constructs slightly differently. I’ve been programming in a learning language jack (a bit like java) the colour scheme that works for that looks hideous (well confusing) when i go back to C (haven’t tried C++) so I’ve got to change to some other colour scheme.
Package Dev makes things like color schemes easy to edit, is the forum sure that one can’t do something with a scope operator at the top of the file ? Perhaps I’m grapsing at straws.
It would be useful to have this process automated (and I’d wager HTML is different structurally to C hence needs different colour highlighting)

@mcfc4heatons I agree.
@kingkeith See above C vs HTML ? ps If it’s possible, how is it done pls ? (if you know)

Lozminda

0 Likes

#9

@usig
Hmmm the Haskell syntax on a C file is kind of working. The only thing is I’ve got to go through all the languages to find out something I like (it’s a fix of sorts). It would be nice if somehow this process was a bit less trial and error (maybe I’m being overdemanding, very possible) Time time time.
Also, say I select Haskell for this C file, is now every C file gonna be read as Haskell, just this one, or will I have to redo this everytime I start an ST3 session ?
Cheers, Lozminda

0 Likes

#10

like mentioned above:

you can make this change persistent for all files with the same file extension. You can open all C source files with Haskell syntax definition.

All language scopes, like CSS, HTML, JS, etc, are defined by a single syntax definition.

If you are using Jack, have you had a look into the Jack syntax highlighting package for sublimetext?

It would be preferred over Haskell…

0 Likes

#11

Yes already use https://github.com/swarn/sublime-jack , ta.

So I use a modded Penitant Contrast for Jack and a Modded Monokai for C. Possible to automate the swap ?

0 Likes

#12

Monokai is a colour scheme, not a syntax definition.

Likely. Please elaborate your desired outcome.

Out of curiosity, could you share a screenshot of your syntax highlighting problems?
Perhabs, a reference to the syntax of Jack, I couldn’t find any :smile:

0 Likes

#13

With a file opened, you can choose Preferences > Settings - Syntax Specific and then set "color_scheme" to your preferred scheme only for that particular language. This only works for separate files though, and not for e.g. embedded CSS or JavaScript in a HTML file.

If only few color schemes look well for that language, perhaps its syntax package doesn’t follow the recommended scope naming conventions.

0 Likes

#14

As I recall from previous questions about this package from @Lozminda, the Jack syntax is indeed incredibly bare bones.

0 Likes

#15

I managed to get on the right path by downloading an existing theme and by trial and test using tools > developer > show scope name to add rules - I have managed to get somewhere near what I wanted… Thanks for all the replies.

0 Likes

#16
0 Likes

#17

@jwortmann and @mcfc4heatons “seem to have the answers” as it were. So the answer to the OP and my joining remarks is, I guess “no”. What I want to do, it would seem, can be done file specifically, but that can’t be turned into a generalisation.
I’ve worked around it by heavily editing monokai, adding more colors etc. So i’ll use that for the C’s and then I’m using a Rainbow Penitant Contrast (RPC) for Jack.
(Ideally I’d like to use something different for the JSON files coz they look really hideous in both !)

One would think it’s be an easy thing to achieve, when a Jack file is interacted with, the color scheme switches to RPC, and when a C++ file is inspected (etc) the color scheme switches to Monokai. After all many other ST3 functions work on scope…

Anyroad of got another really annoying question, that there doesn’t seem to be an answer to … (Bold, Italics and Underline)
Thanks all for your replies

0 Likes

#18

I found this

which could be handy. Ok it’s a colour scheme toggle, so maybe a partial solution…

0 Likes