Sublime Text: set language for each file

1.1k views Asked by At

In Sublime Text (3), I can select the dictionary to use for spell checking. However, this setting seems to be global rather than on a per-file basis. This is annoying when I'm working on multiple files that use different languages. How can I achieve that Sublime Text remembers what dictionary to use for a file?

1

There are 1 answers

3
sergioFC On BEST ANSWER

Usually it is needed to set settings based on the syntax of the file (one dictonary for javascript files, another for css files, etc.). You can achieve this goal easily using syntax specific settings. But there are also times where you need file-specific settings (files with the same syntax with different settings values). I give you example solutions for both cases.

File-specific way

In order to set view-specific settings (similar to file-specific) you can write a plugin. This simple example shows an input panel in which you can set the desired dictionary for the opened file.

import sublime, sublime_plugin

class Example(sublime_plugin.TextCommand):
    def run(self, edit):
        """Default dictionary (caption)"""
        defaultDict = 'Packages/Language - English/en_US.dic'
        if self.view.settings().get('spell_check') == True and self.view.settings().get('dictionary') != None:
            defaultDict = self.view.settings().get('dictionary')
        """Show panel to input dictionary name"""
        self.view.window().show_input_panel('Dictionary value (cancel to disable spell check)', defaultDict, self.setDictionary, None, self.disableSpellCheck)

    def setDictionary(self, dictionary):
        """Enables spell check and sets the dictionary (it is associated with the view)"""
        self.view.settings().set('spell_check', True)
        self.view.settings().set('dictionary', dictionary)

    def disableSpellCheck(self):
        self.view.settings().erase('spell_check')
        self.view.settings().erase('dictionary')

Save it as example.py inside Packages>User. Then add a key-binding and trigger it when you're focused in the desired view:

{ "keys": ["ctrl+alt+e"], "command": "example" }

Note that this is view-specific, so if you close sublime and then re-open it the setting is restored, but if you close the file tab the setting is lost, so if you open the file in the future you'll have to set the setting again. To add a real file-specific setting you need a more complex plugin that extends EventListener and reads the files names in order to set the syntax.

Syntax-specific way

In addition to default settings and user settings you can use syntax speific settings.

Lets say you want to set the dictonary for javascript files, to add the required syntax specific settings open a javascript source file, then go to menu Preferences>Settings-more>Syntax-specific-user, and in the file opened set the settings:

{
    "spell_check": true,
    "dictionary": "Packages/Language - English/en_GB.dic"
}

Finally save it and now your javascript files are using the specified dictonary. Repeat the proccess for other file types.

Note that this is not file-specific but syntax-specific, so if you really need different dictionaries for different javascript files (for example) you'll need to use the other way.