Search code examples
sublimetext3sublimetext

Is it possible to share SublimeText preferences *and* override one specific pref?


I share Sublime prefs between two machines (home, work) using Git.

My monitor at work has a higher DPI than my monitor at home, so in one of the two machines I'd like to override font_size.

I was hoping for:

  • The possibility to override using a command-line flag (to do something like subl --override-pref font_size=15), like kitty --override does.
  • Another level of prefs cascading below User.

, but I don't think any of this exists in Sublime. Language-specific config is not what I'm looking for, I want something global, but only in one of the two machines.

Ideas? Workarounds? Thanks.


Solution

  • The general mechanism at play for settings files is that when Sublime loads a sublime-settings file by name, it pulls all of the similarly named files across all of the known packages and combines them together (the same also happens for many other resource files) with content from later files overriding anything that appears in an earlier file.

    The order that's imposed here is lexically by package, with Default always being first and User always being last. That's why the default settings are in the Default package and your custom settings are in the User package. Additionally syntax specific settings also apply (as do settings specific to projects).

    Apart from this mechanism there's no direct way to override settings without some sort of manual intervention on your part. Potential solutions for this sort of problem include the following examples:

    Don't sync the Preferences.sublime-settings file

    If the file isn't synced across multiple machines, then this problem becomes moot because each machine can easily have it's own unique settings. The downside to that is that each machine then has it's own unique settings, which is a pain in the butt if you often move from machine to machine and things don't quite work the same way.

    Use separate git branches

    An alternative here if you're using git such as you are is to try and keep separate branches per host or per host type (like hi_dpi and reg_dpi or some such). Then on each machine check out the appropriate branch.

    The obvious downsides here are having to try and cross-sync desired settings changes (for both User as well as any packages you might install) between branches, which is less than ideal unless you really love git.

    Use extra Preferences.sublime-settings files

    Here the idea is that you don't include the font_size setting in your User/Preferences.sublime-settings file at all. Instead, you use Browse Packages from the command palette to open the Packages folder, then create a new folder there with some arbitrary name. Inside of that folder include a Preferences.sublime-settings file that contains only the font_size setting.

    Doing this on multiple machines, you can sync the settings in your User folder across machines without also syncing the preference that contains the font_size. As a note, if you create the file while Sublime is already running, you may need to quit and restart to get it to notice that the settings file exists; this only applies the one time, though.

    Use a plugin

    Looking at the link provided above, the ultimate trump card for any setting is a setting that's been applied directly to a view. Given that, you can use a plugin that selectively always applies a specific font size to any newly created or opened file:

    import sublime
    import sublime_plugin
    
    import socket
    
    
    class CustomFontListener(sublime_plugin.EventListener):
        hostname = socket.gethostname()
    
        def on_new(self, view):
            if self.hostname in ("host1", "host1.example.com", "host2"):
                view.settings().set("font_size", 20)
    
        on_load = on_new
    
    

    Now any time you open a file or create a new buffer, if the current hostname is in the list you've configured the view will immediately get an appropriate font_size applied; otherwise the one from the preferences would be used instead.

    You could also extend this to be configurable via it's own settings file, apply a different font size depending on the host name, etc.

    Settings in views are persisted in the sublime-session file and also in the workspace files associated with sublime-project files, so these settings will remain in place even across restarts.


    Something to keep in mind is that the internal commands for changing the font size (via Preferences > Font or via the mouse wheel keyboard shortcuts) work by writing a new font_size to your user preferences.

    If you're using separate preference files, then doing this will add font_size to your User settings and you will need to manually remove it and modify the other settings file.

    If you're using the plugin, then these shortcuts won't seem to do anything because it applies a font_size that overrides the User preference, but in fact as outlined above your preferences file end up being changed and you may not notice right away.

    So whichever way you go, if you tend to use those you may need to make manual adjustments to settings files in the aftermath. It's also possible to create smarter versions of those commands as well, if this is the sort of thing that happens often.