Skip to content

Instantly share code, notes, and snippets.

@NickWoodhams
Last active November 24, 2019 17:00
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save NickWoodhams/434e185ce543ca1c8a99 to your computer and use it in GitHub Desktop.
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
Copy link

guntiss commented Jul 30, 2015

Does't work correctly. Closes last tab instead

@michaelkonecny
Copy link

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
Copy link

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