Sublime Forum

How to configure Sublime Text 3 editor settings for specific file extensions?

#1

I want to change some editor settings that’s only applicable to certain file extensions. File extensions could be something I just made up (example: .mrjayviper file extension)

I cannot find a guide/page on what file name to use for different file types/extensions. Like in the main/default settings file, an example was given for Python files which is to use Packages/User/Python.sublime-settings.

Thanks!

0 Likes

#2

Configuration settings are set globally, per syntax, per project and per open file. However settings are based on the type of the file, not the extension that they have.

For example, there’s a Python.sublime-syntax file that knows how to syntax highlight Python, and it includes information that .py is the extension for Python files. Sublime uses the extension to know that the file is a Python file to apply the syntax, and then the settings associated with the file type come from Python.sublime-settings because that file shares the same name as the syntax.

So, generally speaking you can’t apply specific settings to something.bob just because it has an extension of .bob unless that’s a known file type of some sort without some extra work being involved.

One way would be to create a syntax definition (even if it contains no useful rules) to make the association, or another would be a plugin that applies settings for you on file load.

I’m not aware of anything that does that already, though. Most questions of this nature are about how to get Sublime to recognize various file extensions as the appropriate type.

2 Likes

#3

I have a related question: I’m trying to configure keybindings and typewriter scroll differently with Python files types as opposed to Plain Text or Markdown. In Sublime 4, I didn’t find a Python.sublime-settings file, only the syntax file. Where is the right place to make these changes? In the Typewriter package preferences, I didn’t see an option to configure it to work only with certain file types. Also, Ideally, editing one config file per file type would be helpful rather than having to change assorted settings files.

0 Likes

#4

I’m not familiar with the Typerwriter package, so I can’t help with that other than to say that if it doesn’t have the ability to set setting on a per file type basis and it has its own settings file, the package would most likely need to be updated to provide that kind of support.

For key bindings and makonig a key binding specific to a particular kind of file you use the concept of a context on the key binding. For example this binding from the defaults:

	// HTML, XML close tag
	{ "keys": ["/"], "command": "close_tag", "args": { "insert_slash": true }, "context":
		[
			{ "key": "selector", "operator": "equal", "operand": "(text.html, text.xml) - string - comment", "match_all": true },
			{ "key": "preceding_text", "operator": "regex_match", "operand": ".*<$", "match_all": true },
			{ "key": "setting.auto_close_tags" }
		]
	}

Here there are three contexts that must all be true for this binding to do anything; this needs to be an HTML or Text file that’s not inside of a string or comment, the text that comes before this must have some sort of open tag, and the auto_close_tags setting has to be turned on.

So to make a binding specific to say Python, you would use selector context here using the scope for Python. You can determine the value to use by using Tools > Developer > Show Scope Name while a particular file has the input focus. Whatever this displays, the first line of the scope is the one to use in the binding (unless you want to be really specific about where exactly in a particular type of file the binding should apply).

More information on contexts and how they work, including some demos, can be found in this video:

0 Likes