我只是通过一个简单的插件和元数据文件(.tmPreference 文件)来实现这个功能(在sublime3上),但我不知道这是否有效。有办法,
1. 创建一个 .tmPreference 文件,将一些你想使用的变量放在片段中。
有一个例子,你可以把内容保存在Packages /User/Default.tmPreference
<plist version="1.0">
<dict>
<key>name</key>
<string>Global</string>
<key>scope</key>
<string />
<key>settings</key>
<dict>
<key>shellVariables</key>
<array>
<dict>
<key>name</key>
<string>TM_YEAR</string>
<key>value</key>
<string>2019</string>
</dict>
<dict>
<key>name</key>
<string>TM_DATE</string>
<key>value</key>
<string>2019-06-15</string>
</dict>
<dict>
<key>name</key>
<string>TM_TIME</string>
<key>value</key>
<string>22:51:16</string>
</dict>
</array>
</dict>
</dict>
</plist>
2. 创建一个插件,插件加载时会更新.tmPreference 文件中的shell 变量。
import sublime, sublime_plugin
import time
from xml.etree import ElementTree as ET
# everytime when plugin loaded, it will update the .tmPreferences file.
def plugin_loaded():
# res = sublime.load_resource('Packages/User/Default.tmPreferences')
# root = ET.fromstring(res)
meta_info = sublime.packages_path() + '\\User\\Default.tmPreferences'
tree = ET.parse(meta_info)
eles = tree.getroot().find('dict').find('dict').find('array').findall('dict')
y = time.strftime("%Y", time.localtime())
d = time.strftime("%Y-%m-%d", time.localtime())
t = time.strftime("%H:%M:%S", time.localtime())
for ele in eles:
kvs = ele.getchildren()
if kvs[1].text == 'TM_YEAR':
if kvs[3].text != y:
kvs[3].text = y
continue
elif kvs[1].text == 'TM_DATE':
if kvs[3].text != d:
kvs[3].text = d
continue
elif kvs[1].text == 'TM_TIME':
if kvs[3].text != t:
kvs[3].text = t
continue
tree.write(meta_info)
3. 使用您在 .tmPreference 文件中定义的 shell 变量。
<snippet>
<content><![CDATA[
/**
******************************************************************************
* \brief ${1:}
* \file $TM_FILENAME
* \date $TM_DATE
* \details
******************************************************************************
*/
${0:}
/****************************** Copy right $TM_YEAR *******************************/
]]></content>
<!-- Optional: Tab trigger to activate the snippet -->
<tabTrigger>cfc</tabTrigger>
<!-- Optional: Scope the tab trigger will be active in -->
<scope>source.c, source.c++</scope>
<!-- Optional: Description to show in the menu -->
<description>c file comment</description>
</snippet>