Skip to content

Instantly share code, notes, and snippets.

@michaelkonecny
Created October 10, 2017 15:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save michaelkonecny/bb5a0d1cf43698c0ebe8673f92324ea3 to your computer and use it in GitHub Desktop.
Save michaelkonecny/bb5a0d1cf43698c0ebe8673f92324ea3 to your computer and use it in GitHub Desktop.
Sublime Text plugin - close tabs containing deleted files on refocus
"""
When a view is focused, goes through all open tabs and closes those, whose files don't exist anymore.
Tested in Sublime Text 3.0
"""
import sublime_plugin
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")
Copy link

ghost commented Aug 11, 2018

Thank you for this! Works perfectly. Good riddance deleted files!

@twome
Copy link

twome commented Jan 30, 2019

Insta-crashes Sublime Text 3 with no error message when I switch views to one with "orphan" tabs on macOS 10.14.2.

Update: the alternate focus_view + close_file works great :) Thanks for making this; been my #1 pet peeve with Sublime

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