Sublime Forum

How to disable "Do you want to save the changes you made to New file?"

#1

Hello,

I open a lot of temporary tabs while I’m working. At the end of the day when I go to clean up, I want to close most of these tabs. However I find it very tedious to have to answer this same question over and over again.

Ideally I could just press a hotkey such as alt+D so I don’t have to click my mouse, but it seems like only way to answer the prompt is with mouse click =-/

Is there any way to disable this warning? Thank you

0 Likes

#2

The trick is to mark a view as “scratch” before closing.

I wouldn’t recommend to close view’s with unsaved content automatically without prompting as this is likely to cause accidental data loss.

The safest solution is probably to create dedicated views for note taking, which are not intended to be saved later.

If really required a plugin can also provide a command to suppress promt and directly close files.

Default.sublime-commands

[
	{ "caption": "File: New Scratch", "command": "new_scratch" },
	{ "caption": "File: Close Without Saving", "command": "close_without_saving" },
]

Packages/User/file_management.py

import sublime_plugin


class NewScratchCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.new_file()
        if view:
            view.set_scratch(True)


class CloseWithoutSavingCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.active_view()
        if view:
            view.set_scratch(True)
            view.close()
            return
        sheet = self.window.active_sheet()
        if sheet:
            sheet.close()
1 Like

#3

The general UI for GUI applications (not just Sublime) is that there is a default accelerator key for pressing buttons in dialogs. In Sublime (on Windows), that would be Alt+Y for Yes and Alt+N for No (see the underlined letter in the button key; that’s the mnemonic, just like in a menu).

The same applies to Linux, and probably MacOS too (though I don’t use MacOS so I don’t know if it works quite the same way or not).

0 Likes

#4

Hello thank you for your replies.
I am using MacOS and I don’t think there is an accelerator. Command - D which works in other applications doesn’t work for Sublime, neither does Tab / enter keys.

I will try to figure out how to set up a “scratch” view. Thank you

0 Likes

#5

⌘D to select “Don’t Save” in the unsaved changes dialog does work in ST in macOS. I use it all the time.

1 Like

#6

See here for keyboard shortcuts in macOS dialogs: https://superuser.com/questions/24159/osx-keyboard-shortcuts-in-dialogs

0 Likes

#7

Thank you so much bschaaf! Mac changed the hotkey from cmd +d to cmd +delete.

Thank you deathaxe too for the plugin suggestion. I’m going to try it out

1 Like

#8

I learned a lot by reading the posts here in chat. Yet I wanted to have an even smoother solution. In a few click with Claude, we constructed this code:

class SmartCloseCommand(sublime_plugin.WindowCommand):
    """
    Override the close command to handle empty non-existent files gracefully.

    This command solves the annoying problem where Sublime Text prompts to save
    when closing a non-existent file that was opened but never edited.

    Example scenario:
    - User types: subl myfile.txt (but file doesn't exist)
    - User realizes it's the wrong file and wants to close it
    - Normally: Sublime prompts "Do you want to save?"
    - With this: Closes silently if nothing was typed
    """

    def run(self):
        # Get the currently active view (tab) in the window
        view = self.window.active_view()

        # Make sure we have a valid view to work with
        if view:
            # Get the file path associated with this view
            # This will be None for untitled files, or a path for named files
            file_path = view.file_name()

            # Check if this view has a file path assigned to it
            if file_path:
                # Check if the file actually exists on disk
                # This is False for files opened with 'subl nonexistent.txt'
                file_exists = os.path.exists(file_path)

                if not file_exists:
                    # The file doesn't exist on disk, so check if it's worth saving

                    # Get the size of the buffer (number of characters)
                    buffer_size = view.size()

                    # Check if the buffer has been modified
                    # This is False if user hasn't typed anything
                    is_modified = view.is_dirty()

                    # If the buffer is empty (size 0) AND hasn't been modified
                    if buffer_size == 0 and not is_modified:
                        # This is an empty, non-existent file that user never touched
                        # Mark it as scratch so Sublime won't prompt to save
                        view.set_scratch(True)

                        # Now close it - this won't prompt because it's scratch
                        view.close()

                        # Exit early - we've handled the close ourselves
                        return

        # If we get here, it means one of these:
        # - No active view
        # - File exists on disk
        # - File doesn't exist but has content
        # - File doesn't exist but was modified
        # In all these cases, use Sublime's default close behavior
        self.window.run_command("close")

Sorry for the verbose comments. There were initially no comments. Then, I asked Claude to add nice comments, and this is what happens when you ask a diligent AI to put comments. They don’t bother me, so I decided to leave them. It makes it easier for everybody to read the flow.

0 Likes

#9

This code only handles the very special case of closing unmodified empty buffers of non-saved views. As ST already closes such views without confirmation dialog, provided command is orthogonal.

I am currently going with https://github.com/deathaxe/sublime-default-extended/blob/3b8eca0699a032a72648051af0c3e94fbe187c14/files.py#L226-L262, but be warned, it closes unsaved scratch views without asking to save them. Be prepared to potentially loose data on accidentally closing views.

0 Likes