How to enable Sublime Text 3 only for specific file syntax?

I wrote the ST3 package (with .py plugins, .sublime-keymap and .sublime-commands in it) and I want the plugins and .sublime* configuration files to be active only for the specific file syntax (e.g. .txt or .myCustomSyntax ). How can i achieve this?

+5
source share
2 answers

Judging by the above examples, you want to use file extensions, not syntax. Active syntax can be changed using the syntax menu, while file extensions are persistent.

 #■■■■■ Establish Valid File Extensions ■■■■■■■■■■■■■■■■■■■■ valid_FileExtensions = [] valid_FileExtensions.append ( "txt" ) valid_FileExtensions.append ( "myCustomFileExtension" ) #■■■■■ Get Current File Extension ■■■■■■■■■■■■■■■■■■■■■■■■■ window = view.window() fileExtension = window.extract_variables()[ "file_extension" ] #■■■■■ Verify Current File Extension ■■■■■■■■■■■■■■■■■■■■■■ file_IsValid = False for entry in valid_FileExtensions: if fileExtension == entry: file_IsValid = True #■■■■■ Exit Routine If File Extension Is Invalid ■■■■■■■■■■ if file_IsValid == False: return 

As an alternative; if you prefer to use syntax, you can use:
fileSyntax = view.settings().get ( "syntax" )

Not necessary; you can create a valid_FileExtensions array in YourPlugin.sublime-settings so that users can determine which extensions the plugin will run on.

0
source

Here is a key binding example that I use for the Markdown Preview plugin

 { "keys": ["ctrl+b"], "command": "markdown_preview", "args": {"target": "browser"}, "context": [{ "key": "selector", "operator": "equal", "operand": "text.html.markdown" }] }, 

As you can see, I use the ctrl+b key, which is usually attached to the build command, but it is only active when my cursor is in "text.html.markdown", which occurs only in markup files. Therefore, ctrl+b is created as usual when i'm in a different type of file.

This area is usually in the form of "text.html ..." or "source.python ...". To find the current region name of your cursor, use the command "show_scope_name" with ctrl+alt+shift+p .

Related Documentation

0
source

All Articles