Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Close Deleted File - Sublime 3 Plugin
import sublime_plugin
import sublime
import time
import os
class MyEvents(sublime_plugin.EventListener):
def on_deactivated_async(self, view):
s = view.file_name()
if s:
time.sleep(0.1) # Give the file time to be removed from the filesystem
if not os.path.exists(s):
print("Closing view", s)
view.set_scratch(True)
view.window().run_command("close_file")

guntiss commented Jul 30, 2015

Does't work correctly. Closes last tab instead

Here's my version that seems to work fine in Sublime 3.0:

import sublime
import time
import os


class MyEvents(sublime_plugin.EventListener):
    def on_activated(self, view):
        window = view.window()

        open_views = window.views()

        for v in open_views:
            s = v.file_name()
            if s:
                if not os.path.exists(s):
                    print("Closing view", s)
                    v.set_scratch(True)
                    v.close() # undocumented, but works. if not, one can probably use below:

                    # window.focus_view(v)
                    # window.run_command("close_file")

anviar commented Oct 29, 2017

Hi, Michael!
Here is improved version:

import sublime_plugin
import os


class WatcherEvents(sublime_plugin.EventListener):
    def on_activated(self, view):
        for view in view.window().views():
            s_file = view.file_name()
            if s_file and not os.path.exists(s_file) and not s_file.endswith('-settings'):
                print("File was disappeared " + s_file)
                view.set_scratch(True)
                view.close()

It resolved conflict with settings editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment