Ah yes, I was going to mention that and forgot (dang work interruptions).
In the general case, any time there’s a time delay involved in the solution to a problem, it’s a “not great” solution because that sort of thing is bound to be variable. In this context the only time delay that would be considered safe is 0
, but that doesn’t work here.
In any case, my recommendation in general (biased though it is) would be to replace the entire plugin and any associated key bindings you might have created as outlined by posts above with this:
import sublime
import sublime_plugin
import time
class UpdateLastEditedDateCommand(sublime_plugin.TextCommand):
"""
Check the current file for the first line that contains the provided
header, and if found update it to include the current date and time
as formatted by the given date format.
"""
def run(self, edit, hdr="Last Edited: ", fmt="%d %b %Y %I:%M%p"):
span = self.view.find(f'{hdr}.*$', 0)
if span is not None:
self.view.replace(edit, span, f"{hdr}{time.strftime(fmt)}")
class UpdateLastSavedEvent(sublime_plugin.EventListener):
"""
Update the last edited time in a file every time it's saved.
"""
def on_pre_save(self, view):
view.run_command('update_last_edited_date')
This creates a command named update_last_edited_date
that will find the first instance of the date header and replace it all in one shot in code that is much less verbose than the original (largely because the original was using the wrong kind of command to do this in the first place). The header to look for and the date format are arguments to the command, so the defaults will be used but you can easily change it while executing the command if desired (even having multiple keys bound that do it in different ways, etc)
The event listener will run the command every time the file is saving, and change the content of the file prior to it being saved to disk.
Thus taken together you don’t need any special key bindings, and the natural save
(or even if you save_all
using the menu item or a bound key) will cause all saved files to update automagically.
Should you wish for this to be bound to a separate save action key so that you can trigger it as you want rather than automatically, comment out or remove the event listener class and add a key binding like this (changing the key as you like):
{ "keys": ["super+t"], "command": "chain",
"args": {
"commands": [
{"command": "update_last_edited_date"},
{"command": "save"},
]
}
},
The built in chain
command will execute all the commands you give it one after the other, so here it will update the file date and then save the file.