sublimetext3 - Sublime Text 3 片段,以日期和时间作为自定义变量

标签 sublimetext3 code-snippets

我正在尝试使用自定义环境变量在 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/

相关文章:

sublimetext3 - 更改 Sublime Text 设置后如何更改所有文件中的制表符大小

scala - 为 Scala 配置 Sublime Text 构建系统?

sublimetext - ST3 片段 : trigger HTML snippet but not on PHP scope

autocomplete - 如何在 Sublime Text 2 中删除某些特定的自动完成功能?

sublimetext3 - 在包控件 : Install Package 中找不到某个包

sublimetext3 - Sublime Text 3 和 Filter Lines 插件 - 代码折叠需要键盘快捷键

macos - 在 Sublime Text 3 中通过多跳 ssh 编辑文件

c# - Visual Studio 2015 的 MVC 片段?

visual-studio-code - VS Code 中存储的 C++ 默认代码片段在哪里?

javascript - 原子片段 : Multiple line snippets in atom editor