Can ST2 auto close tab after the file was deleted? I delete file because I donāt need it anymore, but then I must close the tab manually. Itās so annoying
Close tab after delete file?
Totally agree!
I am constantly hitting save on deleted files by accident and thereby undeleting them. often donāt realize Iāve done it for a while
You can do this with a plugin. I didnāt really test this much, so you may want to test on non critical stuff first. It does just close the view, so worst case is that you lose some existing work. That being said, Iām pretty sure it works fine.
import sublime_plugin
import os
class MyEvents(sublime_plugin.EventListener):
def on_activated(self, view):
if view.file_name():
if not os.path.exists(view.file_name()):
view.set_scratch(True)
view.window().run_command("close")
Go to āTools -> New Pluginā. Paste the content I posted above into the file. Save it into āPackages/Userā. You can choose whatever file name you want, just be sure the extension is ā.pyā
Theres a Problem with the plugin, i just found out that the Default-Settings-Files, also Default-Keymap, are not displayed anymore, they get instantly closed after opening.
So you can not view the default settings of Sublimetext or other Plugins.
I just upgraded the script a little bit. Now it checks first if the file is not a kind of āDefaultā-file.
[code]import sublime_plugin
import os
class MyEvents( sublime_plugin.EventListener ):
def on_activated( self, view ):
s = view.file_name()
if s:
if not os.path.exists( s ):
if not "Default" in s:
view.set_scratch( True )
view.window().run_command( "close_file" )
[/code]
Any chance this plugin can be updated for ST3? Doesnāt seem to work there for me.
Or maybe Iām doing something wrong? Iāve added it to packages/user as CloseDeletedTabs.py, and even went so far as to restart Sublime, but it doesnāt seem to do anything. The tab stays open after deleting the file.
So I hadnāt ever looked at plugins in ST3 before, but Iāve done some research and found that in ST3 plugins donāt run on the main thread, so you need to use sublime.set_timeout to run the close_file command on the main thread and avoid a crash.
This seems to work for me in ST3 on OSX:
[code]import sublime_plugin
import sublime
import os
class MyEvents( sublime_plugin.EventListener ):
def on_activated( self, view ):
s = view.file_name()
if s:
if not os.path.exists( s ):
if not "Default" in s:
view.set_scratch( True )
sublime.set_timeout(lambda: view.window().run_command("close_file"), 0)[/code]
The on_activated event wasnāt working properly when deleting the file. I had to change tabs and then click the deleted fileās tab for it to disappear. The method I needed was on_modified_sync.
import sublime_plugin
import sublime
import os
class MyEvents(sublime_plugin.EventListener):
def on_modified_async(self, view):
s = view.file_name()
if s:
if not os.path.exists(s):
# Without checking for this string in the path, config files seem to be automatically closed.
if "Sublime Text 3" not in s:
view.set_scratch(True)
sublime.set_timeout(lambda: view.window().run_command("close_file"), 0)
never use the on_modified_async listener for something like this, then typing lags, etc, etc
Updated the plugin again to be faster and fix a bug when creating a new file from the subl command line.
[code]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ā)[/code]
Gist: https://gist.github.com/NickWoodhams/434e185ce543ca1c8a99
Hope this helps.
Has anyone got a version of this that will only delete files without unsaved changes, please?
If the file is clean, I would like it to disappear from ST3 automatically, but if itās dirty I need to check on it.
I installed this: https://packagecontrol.io/packages/Delete%20Current%20File
It sends the file to the recycle bin and you can have a prompt for deletion in the user settings, the defaults are set up like this anyway.
If this functionality isināt there that you need then perhaps the person who wrote it may add this request here. https://github.com/yaworsw/Sublime-DeleteCurrentFile/issues