[PYTHON] Insert type stamp in Sublime Text (automatically updated when saved)

I thought that Emacs came with it as standard and was convenient, but in Sublime Text, I wrote a timestamp insertion command and a plugin that automatically updates it when saving.

timestamp.py


import sublime, sublime_plugin
from datetime import datetime

TIMESTAMP_PATTERN = 'Last\sModified:\s+20[0-9][0-9]-\d+-\d+\s+\d+:\d+:\d+(\.\d+)?'

class InsertTimestampCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    timestamp = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
    replacement = 'Last Modified: %s' % timestamp
    for r in self.view.sel():
      if r.empty():
        self.view.insert (edit, r.a, replacement)
      else:
        self.view.replace (edit, r, replacement)

class UpdateTimestampListener(sublime_plugin.EventListener):
    def on_pre_save(self, view):
      if view.settings().get("insert_timestamp_on_save") == True:
        region = view.find(TIMESTAMP_PATTERN, 0)
        if region:
          view.sel().clear()
          view.sel().add(region)
          view.run_command("insert_timestamp")

Setting

Place the above script in ~ / Library / Application Support / Sublime Text 3 / Packages / User.

Preferences-> Keybindings --User,

{ "keys": ["ctrl+shift+t"], "command": "insert_timestamp" }

(Key binding is your choice) and press the key to insert the date and time Last Modified: 2015-09-15 01:37:14.

Preferences-> Settings --User,

"insert_timestamp_on_save": true

If you write, the above Last Modified: 2015-09-15 01:37:14 characters will be updated automatically when saving.

reference

Sublime Forum • View topic - Automatically updated Timestamp? Easy time stamp insertion with Sublime Text

Recommended Posts

Insert type stamp in Sublime Text (automatically updated when saved)
Automatically convert to py file when ui file is updated in PySide
GOTO in Python with Sublime Text 3
What Emacs users should know when writing python code in Sublime Text