Sublime Forum

Do not prompt to save deleted files

#1

Sublime asks me if I want to save a tab that I am trying to close of a deleted file. I don’t wish to have to say “yes” to each file. If it is deleted, I want Sublime to close it automatically. I do have "close_deleted_files": true, but it doesn’t make any difference.

0 Likes

#2

The setting’s comment explicitly states to handle only unmodified views, when ST is restoring a session.

ST asks for confirmation, when closing a single view by design to avoid accidental data loss. This behavior is not configurable.

It however provides a “Close Deleted Files” command.

Whether “Save as…” dialog is displayed, depends on “scratch” state of a view.

Plugins can view.set_scratch(True) before closing to suppress it.

The following plugin implements a close_immediately command, which suppresses confirmation,

  1. if another clone of a view is open in current window
  2. if file no longer exists

If file is dirty it is saved and than immediately closed.

import sublime_plugin

DEBUG = False


class CloseImmediatelyCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.active_view()
        if view:
            view_id = view.id()
            buffer_id = view.buffer_id()

            # don't ask if another view into same buffer exists
            if any(buffer_id == v.buffer_id() and view_id != v.id() for v in view.window().views()):
                if DEBUG:
                    print("Close clone")
                view.set_scratch(True)

            else:
                fname = view.file_name()
                if not fname or not os.path.exists(fname):
                    # close immediatly, if file no longer exists
                    view.set_scratch(True)
                    if DEBUG:
                        print("Close deleted file")

                elif view.is_dirty():
                    # save file, before closing
                    if DEBUG:
                        print("Save and close file")
                    view.run_command("save")

            view.close()
            return

        sheet = self.window.active_sheet()
        if sheet:
            sheet.close()
1 Like

#3

Here’s an updated plugin which restores scratch state of cloned views.

from __future__ import annotations

import os
import sublime
import sublime_plugin

DEBUG = False

def find_clone(view: sublime.View) -> sublime.View | None:
    w = view.window()
    if w is not None:
        bid = view.buffer_id()
        vid = view.id()

        for v in w.views():
            if bid == v.buffer_id() and vid != v.id():
                return v

    return None


class CloseImmediatelyCommand(sublime_plugin.WindowCommand):
    def run(self, save=True):
        view = self.window.active_view()
        if view:
            is_scratch_clone = False
            clone = find_clone(view)
            if clone:
                if DEBUG:
                    print("Close clone")
                is_scratch_clone = clone.is_scratch()
                view.set_scratch(True)

            else:
                fname = view.file_name()
                if not save or not fname or not os.path.exists(fname):
                    # close immediatly, if file no longer exists
                    view.set_scratch(True)
                    if DEBUG:
                        print("Close deleted file")

                elif view.is_dirty():
                    # save file, before closing
                    if DEBUG:
                        print("Save and close file")
                    view.run_command("save")

            view.close()

            if clone:
                # restore scratch state of clones
                view.set_scratch(is_scratch_clone)

            return

        sheet = self.window.active_sheet()
        if sheet:
            sheet.close()


class CloseAllInGroup(sublime_plugin.WindowCommand):
    def run(self, save=True):
        group = self.window.active_group()
        if not group:
            return
        for view in self.window.views_in_group(group):
            self.window.run_command("close_immediately", {"save": save})
0 Likes