Sublime Forum

Change color scheme based on language type

#1

Hi,

Is there a way to apply a different color scheme to notes based on the type of extension they have? For example, if I am working on a Markdown file and like Monokai, but then switch to a .py python file, is there a way for that .py file to have a different theme, say Mariana?

Also, is there a way to specify this at the project level? That is, to make these theme-file type settings for different projects?

0 Likes

#2

As far as I know, there’s no way to have different color scheme per view or per window. However, you could write a plugin that changes your color scheme depending on the syntax of the active view.

0 Likes

#3

The color_scheme setting is a per-view setting, so it’s possible to have it be different for different file types. although the theme setting is application specific since it applies to the presentation as a whole.

From within a Python file, select Preferences > Settings - Syntax Specific and add a color_scheme setting to the file, which in your example would look like:

// These settings override both User and Default settings for the Python syntax
{
    "color_scheme": "Packages/Color Scheme - Default/Mariana.sublime-color-scheme",
}

If you truly want/need to change the color scheme based on the extension of the file and the syntax specific way isn’t cutting it, this is still possible but you would need to create a plugin with an EventListener to catch when files are being loaded and saved so you can examine the filename and apply settings to the view as appropriate.

In order to make this project specific, you need to add a settings key to your project file by selecting Project > Edit Project from the menu. An example of that would look like this:

{
	"folders":
	[
		{
			"path": "."
		}
	],
    "settings": {
        "color_scheme": "Packages/Color Scheme - Default/Mariana.sublime-color-scheme",
    }
}
5 Likes

#4

In case you want to use a plugin

import sublime
import sublime_plugin

lang_color_scheme = [('c++', 'Mariana.sublime-color-scheme'),
                     ('rust', 'Sixteen.sublime-color-scheme'),
                    ]
default_color_scheme = 'Monokai.sublime-color-scheme'

class LanguageColorSchemeEventListener(sublime_plugin.ViewEventListener):
    def on_activated_async(self):
        self.view.settings().set('color_scheme', default_color_scheme)
        syntax = self.view.settings().get('syntax').lower()

        for (lang, scheme) in lang_color_scheme :
            if lang in syntax :
                self.view.settings().set('color_scheme', scheme)
                break

you can add your list of language and scheme to lang_color_scheme or change it the way you want.

0 Likes

#5

I don’t know Python or how Sublime Plugins really work. Could you explain where to put this code? Or maybe you can make a sublime package out of this? :slight_smile:

0 Likes

#6

If you want to go the plugin route, this video shows how you can apply one. However the easiest method is to use the instructions in the post above to just modify the syntax specific settings for whatever file types you want to have custom schemes; a plugin is not technically needed at all (unless you’re editing unrecognized files).

1 Like