我正在尝试使用自定义环境变量在 Sublime Text 3 中制作自定义片段。
我本质上想在我的代码片段中包含今天的日期,但没有对此环境变量的官方支持。
我尝试了很多堆栈溢出的解决方案,但无法完成。其中大多数已经过时了。
请帮助我。
最佳答案
你不能用 Sublime Text 片段来做到这一点。
我建议您看一下 InsertDate plugin 文档中非正式地称为 Snippet Macro
的内容。关于包控制。
InsertDate 插件的作者 FichteFoll 建议创建一个宏来插入片段,然后自动移动片段字段以插入日期。下面是来自插件页面的 FichteFoll 的示例宏。您可以复制并粘贴它并将其保存在您的 User
文件夹中,名称为 DateSnippet.sublime-macro
。
[
{ "command": "insert_snippet", "args": {"contents": "Date: $1\nTime: $2\nSomething else: $0"} },
{ "command": "insert_date", "args": {"format": "%x"} },
{ "command": "next_field" },
{ "command": "insert_date", "args": {"format": "%X"} },
{ "command": "next_field" }
]
insert_date
命令显然需要安装 InsertDate 插件。使用Package Control安装它,或者使用像下面我出于类似原因编写的插件一样的轻量级插件,可以使用 insert_todays_date
命令调用(请参阅如何设置或查找 Sublime Text 插件的命令名称 here )。
import sublime, sublime_plugin, time
class InsertTodaysDateCommand(sublime_plugin.TextCommand):
def run(self, edit):
# The date/time field codes for strftime() are at this url:
# https://docs.python.org/3/library/time.html#time.strftime
date_today = time.strftime("%Y-%m-%d")
for sel in self.view.sel():
self.view.insert(edit, sel.begin(), date_today)
要运行宏,您可以从 Sublime Text 菜单工具 --> 宏
调用它,为其创建一个命令面板条目,或者只是添加一个键绑定(bind)像这样的东西:
{ "keys": ["ctrl+shift+y"], "command": "run_macro_file", "args": {"file": "res://Packages/User/DateSnippet.sublime-macro"} },
关于sublimetext3 - Sublime Text 3 片段,以日期和时间作为自定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64630364/