The code outlined above ensures that, whenever the plugin is loaded, the active window (the one with the focus) has its console toggled. That would happen either at startup OR when the plugin loads (but the latter would only happen if it was part of a packge that was ignored and then unignored, or if you manually re-save the file).
If you wanted to ensure that all windows have the console open when the plugin loads, and not just the active one, the last part of the plugin_loaded() should be this instead (not that you asked, but just for the sake of completeness):
for w in sublime.windows():
w.run_command("show_panel", {"panel": "console", "toggle": True})
Assuming you still want ST2 compatability (I sure hope not) you would still need the other parts of that version to verify that it’s safe to make the call.
To have this trigger when a new window is created after this, you would want something like the following (presented here as a single plugin but if you’re combining, just add the class to the plugin and you’re good to go):
import sublime
import sublime_plugin
class OpenConsoleInNewWindowListener(sublime_plugin.EventListener):
"""
In builds of Sublime Text >= 4050, listen for the on_new_window event to
know when a new window is created, and toggle the panel in it.
"""
def on_new_window(self, window):
window.run_command("show_panel", {"panel": "console", "toggle": True})
Note however that this requires Sublime Text 4; the related event here does not exist in builds prior to this one. You can work around this in ST3 (and maybe ST2 but it has been years since I’ve used its API so I’m not 100% certain the below would work for it).
In ST3 where this event does not exist, you need to resort to trying to catch the command that causes a new window to be created; you can do that by adding this method to the listener above after on_new_window:
def on_post_window_command(self, window, command, args):
"""
Prior to build 4050, the on_new_window event does not exist; we can
bodge it in by listening for the command that creates a new window and
manually triggering the event.
on_post_window_command triggers after the command happens, at which
point the newly created window is the active one.
"""
if command == "new_window" and int(sublime.version() < 4050):
self.on_new_window(sublime.active_window())
What this does is listen for the command that creates a new window to be issued and, once it is finished executing, if the version of Sublime is prior to the version that supports the event natively, it will just invoke it with the new window.
This is not 100% foolproof; it has been a while, but as I recall there are ways to create a new window that don’t go through the command system and thus cannot catch this (hence part of the reason the event was added). The only one that comes to mind is running Sublime on MacOS, where clicking rhe dock icon while Sublime is running causes (I beleive) a new window to appear.
Since that is an outside party telling the core to create the window, the command doesn’t trigger, and the plugin would not work.
If THAT matters, then you would need to get into something like constantly polling the available window list to see if there is a new one now that was not there a few ms prior, but that is kind of invasive, resource intensive, and probably better solved by using a more recent build of Sublime (or manually using the keyboard shortcut to create the new window)…