Sublime Forum

Close tab after delete file?

#1

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 :open_mouth:

0 Likes

#2

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

0 Likes

#3

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")

0 Likes

#4

I agree, it would be nice to have ST close the tab of a deleted file.

0 Likes

#5

@skuroda cool, thx, works nice :sunglasses:

0 Likes

#6

How do I add this? Just make a new plugin folder?

0 Likes

#7

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”

0 Likes

#8

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]

0 Likes

#9

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.

0 Likes

#10

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]
0 Likes

#11

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)
0 Likes

#12

never use the on_modified_async listener for something like this, then typing lags, etc, etc

0 Likes

#13

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.

0 Likes

#14

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.

0 Likes

#15

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

0 Likes