Sublime Forum

Make ST3 exit if the last tab is closed

#1

Hi. I have a quick question, and it’s the main reason why I decided to register to this forum after just starting to try out Sublime Text a little.

Is there a way Sublime Text 3 exit completely as soon as you close the last tab currently opened?

Right now, when I close the last tab, ST3 remains opened like this:

I don’t quite like that personally though. I want the program to exit if there’s no tabs currently opened. Is that possible?
At the same time, I noticed that when I open the program, a tab is not made unless I start to type something in. Is there a way to make Sublime Text create a tab as soon as I open the program as is, or in other words, when I open it directly from its .exe file?

Thanks in advance for reading!

0 Likes

#2

You can turn on this setting; by default it’s only turned on for MacOS, where it’s common for applications to remain running in the background when they have no windows open (though on Windows and Linux, closing the last window closes Sublime as well):

	// Set to true to close windows as soon as the last file is closed, unless
	// there's a folder open within the window.
	// On Mac, this value is overridden in the platform specific settings, so
	// you'll need to place this line in your user settings to override it.
	"close_windows_when_empty": false,

There’s not a setting to create a tab when a new window is created, presumably because as soon as any content is entered a tab is created for the new file, and any opened file will also similarly get a tab. So on the whole there’s not a lot of point in it.

However, this plugin will do that whenever a new window is created and at startup for any windows that restore from the previous session with no tabs in them (which can’t happen with the setting above turned on, but better safe than surprised later):

import sublime
import sublime_plugin


def plugin_loaded():
    """
    When the plugin loads (such as on startup), create an empty tab in any
    window that's empty.
    """
    for window in sublime.windows():
        if not window.views():
            sublime.set_timeout(lambda: window.run_command("new_file"))


class NewWindowListener(sublime_plugin.EventListener):
    """
    When a new window is opened, create an empty tab in it.
    """
    def on_post_window_command(self, winddow, command, args):
        if command == "new_window":
            sublime.active_window().run_command("new_file")

This video provides instructions on how to use plugins in Sublime, if you’re not sure how to do that:

1 Like

#3

I tried out the close_windows_when_empty setting and it’s working perfectly for me, thanks!
I’ll try out the plugin right away.

EDIT: Yep, the plugin is working wonderfully as well. It takes a second, but a tab is quickly created after opening Sublime Text normally through its .exe file. Thank you very much!

1 Like