sublime snippet 设置当前日期

在使用 sublime snippet 过程中, 输入注释之后,想使用快捷键快速输入当前时间,而不是手动去写,那样太不极客了…… 经过查资料及多方验证, 大致流程如下:

1.创建一个新的snippet

依次打开tool->new snippet ,并保存为author.sublime-snippet(最好在该目录(User)下再创建一个MySnippet目录),
其内容:

<snippet>
    <content><![CDATA[/**
  * ${2:undocumented description}
  *
  * @Author     : ${PHPDOC_AUTHOR:$TM_FULLNAME}
  * @DateTime   : ${1:alt+t}
  * @Version    : ${TM_VERSION}
  *
  */
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>author</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope></scope>
    <!-- Optional: Description to show in the menu -->
    <description>My description Snippet</description>
</snippet>

注:如果所有语言都想用这个snippet, scope设置为空即可,否则设定为固定的语言即可,比如:只想PHP使用这个snippet,则 scope 标签的值为 source.php

2.创建时间插件

依次打开 Tools → New Plugin, 新建插件,保存在User目录,命名为addCurrentTime.py,
其内容为:

import sublime, sublime_plugin
import datetime
class AddCurrentTimeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", 
            {
                "contents": "%s" % datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 
            }
        )
3.绑定快捷键

依次打开 Preference → Key Bindings - User
内容设置为:

[
    {
        "command": "add_current_time",
        "keys": [
            "alt+t"
        ]
    }
]

注:这里很重要, command 值一定是 python 插件名 的 驼峰写法 改为 下划线 写法!即: addCurrentTime -> add_current_time 。 其他值不起作用!

4.验证

在sublime Text3 中新建一个文件,在文中输入 author,然后按Tab键,便可出现信息头,但是时间没有显示,别急,这时候按时间绑定快捷键(alt+t),当前时间是不是出来了~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值