sublimetext3sublimetextsublime-text-pluginautosave

Enable Autosave files plugin on Sublime Startup


How can the AutoSave plugin for Sublime Text be automatically enabled on Sublime Text 3 startup?

link to auto-save is here


Solution

  • This particular package doesn't naively support the idea of enabling itself automatically when Sublime starts, so in order to accomplish this a small plugin is needed to make this happen, which I've posted below.

    In order to use this plugin, select Tools > Developer > New Plugin... from the menu, then replace the stub plugin code that is presented to you with the code below and save the file in the location that Sublime will default to (your User package) as a Python file, for example auto_save_on_startup.py

    NOTE: See the edit below; this plugin requires that you add a setting in order to control whether it's enabled or not.

    import sublime
    import sublime_plugin
    
    # Sublime executes this every time it loads the plugin, which includes when
    # it first starts, as well as whenever the this file changes on disk.
    def plugin_loaded():
        settings = sublime.load_settings("auto_save.sublime-settings")
        # See if auto_save_toggle_at_startup is turned on; default it to
        # not being turned on if the setting is missing, because that is
        # how the package would behave if you didn't add the setting.
        if settings.get("auto_save_toggle_at_startup", False):
            sublime.set_timeout(lambda: sublime.run_command("auto_save"), 1000)
    

    Edit: The original version of this plugin called run_command directly; however Sublime invokes the plugin_loaded endpoint before it has fully added all of the command classes provided by plugins, so it was possible for this to try to run the command before it was available.

    The code above has been modified so that there is a delay imposed before the command triggers to give the commands time to become available.


    As the comment suggests, every time Sublime loads a plugin file, it will execute the plugin_loaded() function inside of that plugin file, if it happens to exist.

    Here the code checks the auto save package settings to see if you have set the value auto_save_toggle_at_startup to true, and if you have it will invoke the command from the auto save package that turns it on.

    As such, you also need to select Preferences > Package Settings > Auto-save > Settings - User from the menu and add the appropriate setting. Preferences is under Sublime Text in the menu if you're using MacOS.

    If that brings up an empty file (because you're using the default settings), then you should enter the following into the file and save it. Otherwise you can just add the setting itself to the existing settings.

    {
        // Toggle auto save at startup (from User/auto_save_on_startup.py)
        "auto_save_toggle_at_startup": true
    }
    

    Here the comment is a reminder that this setting is being provided by something outside of the package itself, in case you forget.

    Since the plugin command will only toggle the state of the auto save when it's loaded, you either have to add the setting before you add the plugin, save the plugin file to get Sublime to reload it once the setting is in place, or restart Sublime.

    Alternatively you can replace the plugin_loaded() function to contain only the run_command line to unconditionally always toggle the state at startup, but you may want to temporarily stop it from doing that at some point in the future, which you could easily do by just toggling the setting.