Loading

sublime text 自定义插件,自动插入署名,自定义插入日期,自动生成头部注释

自动插入署名

菜单下面的

一、工具(tool)>新代码段(new snippet…)

看到以下代码

<snippet>
    <content><![CDATA[
Hello, ${1:this} is a ${2:snippet}.
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <!-- <tabTrigger>hello</tabTrigger> -->
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

1.snippet介绍

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

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

二、将以下代码粘贴到文件中

<snippet>
    <content><![CDATA[
/**
 * date: ${1:ctrl+alt+shift+d}
 * author: ${2:}
 * descride:${3:}
 */
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>headmark</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

保存的文件后缀是.sublime-snippet 

在文件里面输入headmark,按tab就可以出来了

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

快速插入日期

菜单下面的

一、工具(tool)>新代码段(new plugin…)

看到以下代码

import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, "Hello, World!")

将以下代码粘贴进去

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 

二、自定义快捷键

preferences>键绑定-用户

[
    {"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" }
]

保存就可以了使用了

posted @ 2018-04-17 11:00  王召波  阅读(676)  评论(0编辑  收藏  举报