通过保存升级自动更新日期?

时间:2015-01-19 20:12:29

标签: sublimetext sublimetext3

当我编辑并保存文件时,有一种方式是sublime文本自动更新文件注释块中的日期吗?

像:

/**
 * @author yada yada
 * @date 2015-01-08
 */

为:

/**
 * @author yada yada
 * @date 2015-01-19
 */

2 个答案:

答案 0 :(得分:4)

好的,明白了。

的混合物

偏好设置>密钥绑定 - 用户

[
    {"keys": ["ctrl+s"], "command": "date_and_save" }
]

add_date.py

'''
Autodate header
@date <>
'''
from datetime import datetime
import sublime, sublime_plugin

class AddDateCommand(sublime_plugin.TextCommand):
    def run(self, args):
        content = self.view.substr(sublime.Region(0, self.view.size()))
        begin = content.find('@date <',0,100)
        if begin == -1:
            return
        end = content.find("\n", begin)
        target_region = sublime.Region(begin, end)
        self.view.sel().clear()
        self.view.sel().add(target_region)

        self.view.run_command(
        "insert_snippet",
        { "contents": "@date <%s>" % datetime.now().strftime("%Y-%m-%d %H:%M:%S") } )

class DateAndSaveCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.window.run_command("add_date")
        self.window.run_command("save")

\ o /

答案 1 :(得分:0)

很棒的东西!上述解决方案的一小部分:

而不是

end = begin + 19

这一行会使它变得更具动态性,因为它会消除所有内容直到行尾。

end = content.find("\n", begin)

帮助进行版本控制,例如,行长度可能会有所不同。

相关问题