关于sublime自动生成头部注释

1.在tool->new snippet…创建一个新的snippet 
sublime text2 用snippet 创建文件头部信息

Snippets are smart templates that will insert text for you and adapt it to their context. Snippet 是插入到文本中的智能模板并使这段文本适当当前代码环境. 程序员总是会不断的重写一些简单的代码片段, 这种工作乏味/无聊, 而Snippet的出现会让Code更加高效.

2.新建一个snippet文件 
这里写图片描述 
简要介绍一下snippet四个组成部分:
  • 1.content:其中必须包含<![CDATA[…]]>,否则无法工作, Type your snippet here用来写你自己的代码片段
  • 2.tabTrigger:用来引发代码片段的字符或者字符串, 比如在以上例子上, 在编辑窗口输入hello然后按下tab就会在编辑器输出Type your snippet here这段代码片段
  • 3.scope: 表示你的代码片段会在那种语言环境下激活, 比如上面代码定义了source.python, 意思是这段代码片段会在python语言环境下激活.
  • 4.description :展示代码片段的描述, 如果不写的话, 默认使用代码片段的文件名作为描述

Snippet可以存储在任何的文件夹中, 并且以.sublime-snippet为文件扩展名, 默认是存储在.sublime-snippet文件夹下。

3.在content标签里面编辑要在文件头部显示的信息,在tabTrigger标签中间编辑触发的单词–意思就是在文件头部输入单词,然后按tab键,会将content标签中间的信息显示出来;请看我的例子 
这里写图片描述

sublime 用snippet 创建文件头部信息 

4.其中有一个date time变量,snippet是不能自动创建时间的,需要你再创建一个插件,用来创建当前时间,步骤其实和创建snippet差不多,只不过需要选择的是new plugin,,然后将下面的代码粘贴到新的plugin文件里面:

复制代码
import datetime, getpass
import sublime, sublime_plugin
class AddDateTimeStampCommand(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") } )
class AddDateStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.datetime.now().strftime("%Y-%m-%d") } )
class AddTimeStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.datetime.now().strftime("%H:%M:%S") } )
复制代码

文件名字可以自己定义,保存的文件后缀是.py 

5.虽然创建了plugin,但是还需要在sublime编辑器 用户按键–key bindings user文件里面编辑触发插件的快捷键代码:

[
    {"keys": ["ctrl+alt+shift+d"], "command": "add_date_time_stamp" },
    {"keys": ["ctrl+alt+d"], "command": "add_date_stamp" },
    {"keys": ["ctrl+alt+t"], "command": "add_time_stamp" }
]

6.经过以上操作和编辑就可以了

原文地址:https://www.cnblogs.com/aten/p/8052659.html