Once there’s a filename associated with a tab (there won’t be before you save a brand new file, for example), the state of the tab tracks that file. So as you’ve seen if you remove the file in any way, the response from Sublime is to tell you that the file has been edited and putting it back will make it think it hasn’t changed.
In some weird loose technical way, that’s somewhat true; the file changed from existing to not existing and vice versa, though that’s not what you’d expect the modified state of the buffer to tell you.
On the other hand, the plugin API has an on_modified
event which doesn’t trigger when a file is removed, even though it’s marking the file as being modified for the purposes of the tab bar and such, so that seems like a bit of a disconnect.
This is controlled by the always_prompt_for_file_reload
setting:
// Always prompt before reloading a file, even if the file hasn't been
// modified. The default behavior is to automatically reload a file if it
// hasn't been edited. If a file has unsaved changes, a prompt will always
// be shown.
"always_prompt_for_file_reload": false,
The default value is false
, so if the underlying file changes and you haven’t modified it, it will silently reload without asking you. You can flip the state of that if you want to know when this happens. This can be semi-problematic in cases where you’re for example using source control and check out an old version while you have files open, which may give you an excess of prompts to respond to, though.
In regards to having views close when the underlying file is missing, there’s not a setting for that but something like the plugin below may be of help (see this video if you are unfamiliar with using plugins).
When the plugin is loaded (whenever you modify it or when Sublime starts) it will check every open window to see if there are any views that have a filename associated with them but for which the file on disk is no longer present and forcibly close them. For our case here, that would be files that had active modifications to them at the point where Sublime was quit, since Sublime will close unmodified tabs for missing files on it’s own.
As defined here, the tab will be closed without prompting to save any changes, which has the potential for data loss in cases where you might actually want to have kept such a buffer around, so this is sort of a Danger Will Robinson
kind of operation.
import sublime
import sublime_plugin
import os
def plugin_loaded():
"""
At plugin load time, check every view in every window looking for views
represented by files which are no longer on disk and forcibly clobber
them.
"""
for window in sublime.windows():
for view in window.views():
if view_file_missing(view):
clobber_view(view)
def view_file_missing(view):
"""
Return an indication on whether the view provided represents a file that
is missing on disk. Files with no name assigned and files opened by
"View Package Files" and similar commands will report False here.
"""
name = view.file_name()
if name is None or view.is_read_only():
return False
return not os.path.isfile(name)
def clobber_view(view):
"""
Unconditionally close the provided view, discarding all changes present
in it without asking for confirmation.
"""
view.set_scratch(True)
view.close()