Skip to content

Instantly share code, notes, and snippets.

@madastro
Last active September 25, 2015 07:40
Show Gist options
  • Save madastro/eec24bee45e6167ba056 to your computer and use it in GitHub Desktop.
Save madastro/eec24bee45e6167ba056 to your computer and use it in GitHub Desktop.
Sublime Text 3 plugin for simple timestamping of source code

Sublime Text 3 add timestamp on save plugin

  1. Go to Tools > New Plugin
  2. Paste content of st3_timestamp.py and save as timestamp.py
  3. Add {@timestamp} anywhere in your code to insert timestamp, subsequent saves will update the timestamp
Note:
  1. Add supported files by editing the following line or remove to support all files:
if filename.lower().endswith(('.tpl','.html','.php','.css','.js')):
  1. Time stamps are in your current local time

MIT License © Romano Fonbuena

# coding: utf8
import sublime, sublime_plugin
from datetime import datetime
class InsertTimestampCommand(sublime_plugin.TextCommand):
def run(self, edit):
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
pattern = '\{\s*@timestamp\s*\}'
insert_ts = self.view.find_all(pattern, 0)
insert_ts.reverse()
for region in insert_ts:
self.view.replace(edit, region, timestamp)
class InsertTimestamp(sublime_plugin.EventListener):
def on_pre_save(self, view):
filename = view.file_name()
if filename.lower().endswith(('.tpl','.html','.php','.css','.js')):
view.run_command("insert_timestamp")
class UpdateTimestampCommand(sublime_plugin.TextCommand):
def run(self, edit):
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
pattern = "(20[0-9][0-9]-\\d+-\\d+\\s+\\d+:\\d+:\\d+[(\\.\\d+)]?)"
update_ts = self.view.find_all(pattern, 0)
update_ts.reverse()
for region in update_ts:
replacement = '%s' % timestamp
self.view.replace(edit, region, replacement)
class UpdateTimestamp(sublime_plugin.EventListener):
def on_pre_save(self, view):
filename = view.file_name()
if filename.lower().endswith(('.tpl','.html','.php','.css','.js')):
view.run_command("update_timestamp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment