Sublime Forum

Accessing installed package default settings


How can I access installed packages default settings via the API ?



To do this you would create a sublime-settings file for your package and then use the sublime.load_settings() API to load that settings file and obtain a settings object from it.

The general rule of thumb is that the be named for your package because if the user modifies any settings, they’re going to end up with a copy of that file in their User package, and having the name track to the package makes it easier to know where the settings are coming from.

Inside of your sublime-settings file you can include any settings you like.



I should have made the description clearer. What I wanted was to access the default settings of an already installed package. For example, If I have Terminus installed, I wanted to do something like window.run_command("open_file", { "file": "path_to_defaut_terminus_settings" }) i.e. to open a new tab with the default Terminus.sublime-settings already populated. But for that I don’t know where installed package default settings files are stored. Is it possible ?



Probably the easiest and least error prone way to do something like that would be to use sublime.find_resources(). If you call sublime.find_resources("*.sublime-settings"), the result is a list of every sublime-settings file that exists in every package that’s currently installed and not in the list of ignored_packages in the settings.

The entries here look like 'Packages/Terminus/Terminus.sublime-settings', which indicates that in the package Terminus there’s a settings file named Terminus.sublime-settings in the root of the package.

This will give you every settings file, so you need to prune away any that you’re not interested in; for example there will also be a Packages/User/Terminus.sublime-settings in the list as well, but that’s probably not the one you want. So in your case you want to select only those settings files that report as being in the package you’re interested in.

After determining what settings file you want to open, the simplest way to open it would be to replace the Packages text with the ${packages} variable that the open_file command expands out to be the full packages path:

path = 'Packages/Terminus/Terminus.sublime-settings'
window.run_command("open_file", {"file": x.replace('Packages', '${packages}')})

The contents of sublime-package files and the contents of the Packages folder are treated as one transparent file space, so it’s transparent to you whether the file you’re opening is coming from a sublime-package file or from a file that exists inside of the Packages folder.

As a side note, no matter what platform you’re on (Windows, MacOS or Linux) package resources always use the POSIX-style paths seen here; Windows will happily open a file using forward slashes as path separators, so the above code will work everywhere.

1 Like