Sublime Forum

Open specific file type (.py) in a new window instead of new tab

#1

I often have a series of tabs in a single window that are just random notes, TODO lists, etc. that I keep open at all times on my laptop monitor. When I open any new file, the setting I have is to open in a new tab. However, I would like to specify that a specific file type, in this case Python .py files, should open in a new window - keeps things neater that way (in my mind, at least).

Is something like this possible?

0 Likes

#2

A plugin could handle a situation like that by triggering on on_load and re-opening the file in a separate window:

import sublime
import sublime_plugin


class SinglePythonEventListener(sublime_plugin.EventListener):
    def _ensure_single_window(self, view):
        """
        If the window that the view provided is currently loaded into contains
        any other files, close the file in this window and open it in a new
        one.
        """
        if len(view.window().views()) != 1:
            # Capture the filename of the view, then close it
            filename = view.file_name()
            view.close()

            # Create a new window and open the file in it.
            sublime.run_command('new_window')
            sublime.active_window().open_file(filename)

    def on_load(self, view):
        """
        If a file is loaded and it is a Python file, ensure that it's the only
        file within its window.
        """
        if view.match_selector(0, 'source.python'):
            self._ensure_single_window(view)
1 Like

#3

This worked great, thanks for the reply!

0 Likes