您好,欢迎访问一九零五行业门户网

教你怎么在Sublime3中设置自己的代码片段

下面由sublime教程栏目给大家介绍怎么在sublime3中设置自己的代码片段,希望对需要的朋友有所帮助!
在 sublime text 3 中设置自己的代码片段
写代码的时候,经常会在注释里写一下作者,创建时间等等,这样子也算留下了自己的印记,今天就教大家如何构建自己的注释代码块(snippets)。
sublime snippets(代码片段)
sublime text 3 snippets是你需要反复输入相同片段的文本、代码时需要的重要功能。
snippets可以储存在任何一个包的文件夹下,但是为了简单,现在建议先保存在packages/user目录下
snippets的文件格式是.sublime-snippet,通常snippet的结构如下
<snippet> <content><![cdata[type your snippet here]]></content> <!-- optional: tab trigger to activate the snippet --> <tabtrigger>xyzzy</tabtrigger> <!-- optional: scope the tab trigger will be active in --> <scope>source.python</scope> <!-- optional: description to show in the menu --> <description>my fancy snippet</description></snippet>
我们只要把cdata中的内容替换成自己的,就可以完成一个最简单的snippets的编写。
创建自己的snippets
接下来我们就以自己的代码注释为例,写一个snippet。
首先,在sublime菜单栏中选择tools | developer | new snippets…,然后输入
<snippet> <content><![cdata[/** @author: maniau* @createtime: ${1:time}* @description: ${2:description}*/]]></content> <!-- optional: set a tabtrigger to define how to trigger the snippet --> <tabtrigger>comm</tabtrigger> <!-- optional: set a scope to limit where the snippet will trigger --> <scope>source.js</scope></snippet>
其中content为snippet的内容,tabtrigger是你输入什么内容时可以识别为snippet,scope的表示生效的文件形式,content中 ${}为你输入完之后,tab键可以选中的内容,${1:}为你输入完之后直接选中,${2:}为按一次tab选中的内容,依此类推。
随后保存为comment.sublime-snippet,接下来随便在一个js文件中,输入comm,按下tab键盘,你的snippet就出现了。
时间输入插件
snippet虽然生成了,但是时间还是没有搞定,接下来就创建自己的插件,在sublime菜单栏中选择tools | developer | new plugin…,输入以下内容
import sublime, sublime_pluginfrom time import localtime, strftimeclass insertdatetimecommand(sublime_plugin.textcommand): def run(self, edit): sel = self.view.sel(); for s in sel: self.view.replace(edit, s, strftime("%y-%m-%d, %h:%m:%s gmt%z", localtime()))
保存为insert_datetime.py,然后在preference | key bindings中加上
{ "keys": ["super+ctrl+t"], "command": "insert_datetime"}
这表示你按下⌘+control+t,就可以插入时间了,配合上面的snippet,插入注释后,加上时间和描述,就可以方便地生成自己的注释,如下
/** @author: maniau* @createtime: 2017-03-14, 22:33:00 gmt+0800* @description: this is a test!*/
后记
当然,snippets的用处不仅如此,你可以在你的环境下配置各种各样的片段,可以大大提升的工作效率,大家一起去探索吧!
以上就是教你怎么在sublime3中设置自己的代码片段的详细内容。
其它类似信息

推荐信息