Sublime Forum

Programmatically moving folders in the folders panel


Is there a way to programmatically move the folders in the folders panel via the API ?



do you mean visually sorting or just a normal file system move operation?



Yes. Visually sorting the folders up and down in the folders panel.



There’s not an API for interacting with the side bar directly (yet at least), so in the general sense it’s not possible to modify what the side bar is displaying short of modifying settings that control what appears in there (like excluding files). So changing the sort order of general content in the side bar is not possible.

What IS possible is changing the order of the top level folders in the side bar (i.e. the folders that are open in the current window), so if that’s what you’re after that is indeed possible.

The list of folders that are open in the window are stored in the window’s project data and that data can be manipulated. This is true for all windows; both those that have an associated sublime-project or sublime-workspace file and those that don’t. In the latter case this is generally referred to as anonymous project data, and saving a project/workspace in this case is just storing to disk the project data that’s already present in the window.

The format of the project data mimics the content of a sublime-project file, and is available via the window.project_data() API endpoint. For example, the result of that could be something like this (reformatted here so the line isn’t so long):

>>> window.project_data()
  'folders': [
    {'path': '/home/tmartin/deleteme/folder_a'}, 
    {'path': '/home/tmartin/deleteme/folder_b'}

The folders are stored in the folders key, and the order of them in the list is the order that they appear in the side bar. Using window.set_project_data() you can modify the project data in the window, so you could use that to alter the order that they appear in any manner that you wish:

data = window.project_data()
data["folders"] = list(reversed(data["folders"]))

If a window has an associated sublime-project file, this will modify the content of the file to match what you set here so that the change is persisted.

Something else to note is that some (but not all) changes to the folder data in the window cause Sublime to fold up all of the folders in the side bar as a result. Changing the order of the items in the list is a change that will cause this to happen.



Thank you @OdatNurd for the detailed explanation.