使用片段可能不可能,但是我编写了一个插件来完成您想要的。在 Sublime 中,单击Tools > New Plugin。将示例代码替换为以下内容。将其命名为“add_license_stamp.py”并将其保存在您的Packages文件夹中(而不是Packages/User中)。此外,在您的键映射文件中添加键绑定。要运行该命令,请将光标放在您想要的位置并按下键绑定:
键绑定:
{ "keys": ["ctrl+shift+9"], "command": "add_license_stamp" }
插入:
import sublime, sublime_plugin
import os
import datetime
import random
import getpass
''' Add license stamp
/**
* @version $Id: ${1:current_file_name.extension} ${2:random_4_digit_number} ${3:YYYY-MM-DD} ${4:time_in_UTC_24} ${5:current_logged-in_user} $
* @author Company http://example.com
* @copyright Copyright (C) 2007 - ${6:current_year} Company
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
*/
'''
class AddLicenseStampCommand(sublime_plugin.TextCommand):
def run(self, edit):
company_name = "BobCo"
company_site = "http://bobco.com"
file_path = self.view.file_name()
file_name = os.path.basename(file_path)
year = datetime.datetime.utcnow().strftime("%Y")
date_time = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
random_number = str(random.randrange(0000, 9999)).zfill(4)
user = getpass.getuser()
license = "/**\n"
license += "* @version $Id: " + file_name + " " + random_number + " " + date_time + " " + user + " $\n"
license += "* @author " + company_name + " " + company_site + "\n"
license += "* @copyright Copyright (C) 2007 - " + year + " " + company_name + "\n"
license += "* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only\n"
license += "*/\n"
self.view.replace(edit, self.view.sel()[0], license)
(注意:python 要求代码后有一个空行)
Replace "BobCo" with your company name. I'm not sure of the best way to get the current user name, I used this question: Is there a portable way to get the current username in Python? . They say it is compatible with the major OSes. If not do something similar to how I did the company name. And manually set it per user. Also, I don't know what UTC 24-hour format is. But I just used the time in 24 hour format.
Edit
I changed now() to utcnow() to get the utc date/time. I added date/time formatting. I added zfill(4) to the random number to pad it with zeros if under 4 digits. You can highlight the current stamp and hit the key binding to update it. You could also get fancy and automatically replace on save, but beyond the current scope. You would have to use a regex to find the current stamp. Then activate the script upon save instead of run.