Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Updated for Sublime Text 3. Automatically add (if you have the correct code in your template) and update timestamps to mark the date a file was created and date it was last modified.
Automatically add and update time stamps in your files.
On file open, will replace [timeStamp] with the current date and time.
Currently looks for two instances of this (example below).
To use, place the text [timeStamp] in your template file where you want it.
// CREATED: timeStamp
// MODIFIED: timeStamp
When you open this file for the first time, both [timeStamp] will change to
the current date and time. Every save, the MODIFIED line will update.
NOTE: After installing, don't open this file in Sublime Text because it will change
[timeStamp] in the code to the current time. Oops =]
import datetime
import sublime
import sublime_plugin
TIMESTAMP_CODE = 'timeStamp'
TIMESTAMP_PATTERN = 'MODIFIED:\\s*20[0-9][0-9]-\\d+-\\d+\\s+\\d+:\\d+:\\d+(\\.\\d+)?'
class InsertTimestampListener (sublime_plugin.EventListener):
When opening file, search for TIMESTAMP_CODE and replace it with current time
def on_load (self, view):
region = view.find (TIMESTAMP_CODE, 1)
if region :
replacement ='%Y-%m-%d %H:%M:%S')
with Edit(view) as edit:
edit.replace (region, replacement)
region = view.find (TIMESTAMP_CODE, 1)
if region :
replacement ='%Y-%m-%d %H:%M:%S')
with Edit(view) as edit:
edit.replace (region, replacement)
class UpdateTimestampListener (sublime_plugin.EventListener):
On save, search for the MODIFIED line, and replace timestamp with current time
def on_pre_save (self, view):
region = view.find (TIMESTAMP_PATTERN, 1)
if region :
timestamp ='%Y-%m-%d %H:%M:%S')
replacement = 'MODIFIED: %s' % timestamp
with Edit(view) as edit:
edit.replace (region, replacement)
# buffer editing for both ST2 and ST3 that "just works"
# cortesy of "" ST2/3 Edit Abstraction. See:
import sublime
import sublime_plugin
from collections import defaultdict
except AttributeError:
sublime.edit_storage = {}
class EditStep:
def __init__(self, cmd, *args):
self.cmd = cmd
self.args = args
def run(self, view, edit):
if self.cmd == 'callback':
return self.args[0](view, edit)
funcs = {
'insert': view.insert,
'erase': view.erase,
'replace': view.replace,
func = funcs.get(self.cmd)
if func:
func(edit, *self.args)
class Edit:
defer = defaultdict(dict)
def __init__(self, view):
self.view = view
self.steps = []
def step(self, cmd, *args):
step = EditStep(cmd, *args)
def insert(self, point, string):
self.step('insert', point, string)
def erase(self, region):
self.step('erase', region)
def replace(self, region, string):
self.step('replace', region, string)
def callback(self, func):
self.step('callback', func)
def run(self, view, edit):
for step in self.steps:, edit)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
view = self.view
if sublime.version().startswith('2'):
edit = view.begin_edit()
key = str(hash(tuple(self.steps)))
sublime.edit_storage[key] =
view.run_command('apply_edit', {'key': key})
class apply_edit(sublime_plugin.TextCommand):
def run(self, edit, key):
sublime.edit_storage.pop(key)(self.view, edit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.