Sublime Forum

Is it possible to close tab of files deleted outside of sublime?

#1

say, I am having an open tab of abc.txt
I deleted it in FileExplorer, and in sublime it shows as “modified”(red dot)

Is it possible to auto close tab when I switch to Sublime?

Thank you.

0 Likes

#2

I believe the 2 closest things you can get:

  1. close_deleted_files settings which closes deleted files upon restart.
	// When reopening Sublime Text close saved files that have been deleted from
	// the filesystem (Unsaved files will not be closed). If this setting is
	// false no files will be closed, instead they will be restored as empty
	// files.
	//
	// This is useful when working from an unstable networked file system where
	// tabs would be lost if the connection wasn't active.
	"close_deleted_files": true,
  1. Using the tab context menu’s Close Tabs With Deleted Files which will close all views with deleted files when clicked.

I don’t think there is an option (or any way) to close deleted files when ST is refocused again.

0 Likes

#3

Thank you for your reply.
I tried and confirmed these 2 methods works.

Wonder if there are any shortcut for “Close Tabs With Deleted Files”?
(Went through

Default (Windows).sublime-keymap
and Menu but found nothing relevant.

Thank you.

0 Likes

#4

The command isn’t in the main menu, just in the tab context menu as @UltraInstinct05 mentioned above. The command requires that you tell it in which group of files you want to close the deleted files (i.e. it doesn’t work globally for all deleted files that might appear in the window):

	{ "command": "close_deleted_files", "args": { "group": -1 }, "caption": "Close Tabs With Deleted Files" },

The command requires that you tell it the group of files to deal with (0, 1, 2, etc), so binding it to a key would require a simple plugin that does it for all of the available groups in the current window. If you never take advantage of split window layouts, you could bind a key that just hardcodes the group to 0 though.

0 Likes

#5

Thank you for the explantation
Will try to make a script for this command

0 Likes

#6

An example of this (worked on in this week’s live stream) would be:

import sublime
import sublime_plugin

# Related reading:
#     https://forum.sublimetext.com/t/is-it-possible-to-close-tab-of-files-deleted-outside-of-sublime/62674?

class CloseAllDeletedFilesCommand(sublime_plugin.WindowCommand):
    """
    Simple wrapper around the built in command that will close tabs for deleted
    files; it normally exists only in the Tab context menu and can't be bound
    to a key directly because it requires you to give it a group number.

    If current is True, this will only operate over the current file group;
    otherwise it will close all deleted file tabs in all groups in the current
    window.
    """
    def run(self, current=False):
        count = self.window.num_groups()
        active = self.window.active_group()
        groups = range(count) if not current else range(active, active + 1)

        for group in groups:
            self.window.run_command('close_deleted_files', {'group': group})

This implements a command that can either close all deleted file tabs across all groups in the current window or just the ones in the current group, depending on your needs.

1 Like

#7

Thank you!
Will try to import it into my Sublime

0 Likes