Emacs には標準で付いてて便利だなと思っていたのですが、Sublime Text で、タイムスタンプ挿入コマンドとそれを保存時に自動更新するプラグインを書きました。
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")
設定
上のスクリプトを、~/Library/Application Support/Sublime Text 3/Packages/User
に置きます。
Preferences -> Keybindings - User に、
{ "keys": ["ctrl+shift+t"], "command": "insert_timestamp" }
と書き(キーバインドはお好みで)、キーを押すと Last Modified: 2015-09-15 01:37:14
という日時が挿入されます。
Preferences -> Settings - User に、
"insert_timestamp_on_save": true
と書くと、上の Last Modified: 2015-09-15 01:37:14
の文字が保存時に自動で更新されます。
参考
Sublime Forum • View topic - Automatically updated Timestamp?
SublimeTextでかんたんにタイムスタンプを挿入させる