Search code examples
sublimetext3

Automatically loading a Sublime workspace on startup


I posted a question here: Easier access to Sublime macros

That was brilliant and worked perfectly! But there is one last thing I can't figure out. How do I set it so Sublime loads a saved workspace automatically whenever it is opened? Because I have my macros folder in the sidebar but I want it there automatically every time I start Sublime, I added the folder and saved the workspace but when I close Sublime the folder is gone and I have to go to project->open-recent->myworkspace

Is there a way to either load a workspace automatically upon starting Sublime or automatically adding a folder to the project when starting Sublime?


Solution

  • If the appropriate folder is open in the side bar and you have the hot_exit setting turned on (which it is by default), then quitting Sublime and restarting it should keep the folder there, and the same should be true for individual windows if they are referencing a project or workspace.

    It's not uncommon to run afoul of the distinction that closing the last window is not the same thing as quitting Sublime depending on the platform that you're on, which can cause Sublime to record in the session that there were no windows open when it quit.

    However based on the comments on your question it sounds more like this isn't actually your issue and you'd instead like a way to have a specific folder or folders added to newly created windows and projects instead, so that your macros folder is just always available.

    This is possible via a plugin, which can adjust the folders in a particular window as appropriate. This can be semi-mostly automated (more on that below). Here's the plugin we need for this:

    import sublime
    import sublime_plugin
    
    
    class AddAdditionalFoldersCommand(sublime_plugin.WindowCommand):
        """
        Adds all of the folders from the `additional_folders` setting in the user
        preferences to the current window, if they are not already present there.
        """
        def run(self):
            # Get the list of folders from the settings
            settings = sublime.load_settings("Preferences.sublime-settings")
            additional = settings.get("additional_folders", [])
    
            # Get the project data and associated folders from the current window,
            # with sensible defaults if the window has no project data yet
            project_data = self.window.project_data() or {}
            project_folders = project_data.get("folders", [])
    
            # Add in any additional folders that are not already present
            for folder in additional:
                if not any(o["path"] == folder for o in project_folders):
                    project_folders.append({"path": folder})
    
            # Update project data
            project_data["folders"] = project_folders
            self.window.set_project_data(project_data)
    
    
    class AdditionalFolderListener(sublime_plugin.EventListener):
        """
        Listens to see if a new window has been created, and if so add additional
        folders if desired.
        """
        def on_post_window_command(self, window, command_name, args):
            if command_name == "new_window":
                settings = sublime.load_settings("Preferences.sublime-settings")
                if settings.get("auto_add_additional_folders", False):
                    window = sublime.active_window()
                    window.run_command("add_additional_folders")
    

    To use this, you also need to add some settings to your Preferences.sublime-settings file (change the values as appropriate):

        // When true, the folders in "additional_folders" will be
        // automatically added when a new window is created.
        "auto_add_additional_folders": true,
    
        // Additional folders to add; these must be absolute paths
        "additional_folders": [
            "/home/odatnurd/.config/sublime-text-3/Packages/User/macros"
        ],
    

    This does two things:

    1. When you run the add_additional_folders command, all of the folders from the list configured in the additional_folders setting will be added to the current window so long as they are not already present (otherwise the same folder would appear more than once).

    2. When a new window is created, if the auto_add_additional_folders setting is true (it defaults to false in the code for safety), then the command will be executed automatically for the new window.

    Cycling back to the above post, the "semi-mostly automated" part of this is because in order to work, the new_window command has to be invoked for the event handler to be able to know that a new window was created.

    This will be the case if you use the menu item or key binding to create a new window, but that command isn't executed if you open an existing project or workspace. It's also possible for windows to be created without using this command, such as if you quit Sublime with no windows and it creates a new one by default, or on MacOS if there are no windows and you click the dock icon.

    Thus depending on how you use Sublime you may want to bind add_additional_folders to a key so that you can invoke it manually as needed. You can also create a file named something like AdditionalFolders.sublime-commands in your User package with the following content to add the command to the command palette. You can adjust the caption to suit; also the name of the file is not important so long as the extension is correct.

    [
        { "caption": "Add Additional Folders", "command": "add_additional_folders" },
    ]
    

    Final Notes:

    1. Despite the name of the API used here, this will work in all windows even if they don't have a project associated with them (or they just have a workspace associated with them) because all windows inherently carry project data

    2. It's been hinted that the next version of Sublime will have additional events that make it easier to detect new windows. The above covers up to build 3211; if you're reading this and there is a newer build available, the above could possibly be made smarter.