Sublime Forum

Bind file extensions to layout groups

#1

Hello!
Is it possible to split files by type and put a specific type in a layout group?
I mean the following functionality, when I open the *.css file so that it always opens in the same group (group 1) of the layout, and when I open the *.html file so that it always opens in a different group (group 2).
I need to bind file extensions to layout groups.
Is it possible to do this?

1 Like

#2

With plugins, you can. Here is an example one that I came up with quickly (Bit dirty, needs work but it works).

import sublime
import sublime_plugin

def plugin_loaded():
	global of_settings
	of_settings = sublime.load_settings("OrganizeFiles.sublime-settings")

class ExampleListener(sublime_plugin.EventListener):

	def on_load_async(self, view):
		extension_to_group = of_settings.get("extension_to_group")
		for key, value in extension_to_group.items():	
			if view.file_name().endswith(key):
				view.window().run_command("move_to_group", {
					"group": int(value)
				})

and the corresponding settings file for it (Just a bit of convenience for us)

{
	// A setting to map the extension to the group you want.
	"extension_to_group": {
		".css": 1,
		".html": 2
	}
}

Save the python file by any name you want (e.g. bob.py) in the User directory & the settings file as OrganizeFiles.sublime-settings in User.
Now, when you have a second group open (group 1), any css files will open in group 1.
When you have a third group open (group 2), any html files will open in group 2.
Note that this will not create the group if groups aren’t present.

You can probably improve this version a lot, which is left as an exercise to the readers.

1 Like