用过一次VIM,感觉其直接插入头文件很方便。但是在sublime插件中没找到这类型的插件(应该有,只是我没找到),就决定自己写一个插件,百度之后找到了一个关于这类型的文章,便照着写了。sublime的插件开发是基于Python的,写起来很方便。有点python基础的应该都能看懂。
1. 在菜单栏的工具下有新插件,点击后就会给出一个可以往编辑器插入hello world的小插件。
2.将下面的代码复制进去,具体内容参考sublime的API。
import sublime, sublime_plugin, datetime class insertSignatureCommand(sublime_plugin.TextCommand): def run(self, edit): date = datetime.datetime.now() dateStr = date.strftime("%Y-%m-%d %X") text_encode = """#-*- encoding: utf-8 -*-\n/********************************************************\n""" text_author = """\n* File Name:\n* author: NCZkevin\n* Mail: z1376871863@163.com\n* Homepage: http://nczkevin.com\n******************************************************/\n""" text_include = """\n#include "vector"\n#include "set"\n#include "deque"\n#include "queue"\n#include "algorithm"\n#include "functional"\n#include "iostream"\n#include "cstdio"\n#include "cmath"\n#include "cstdlib"\n#include "string"\n#include "cstring"\n#include "string.h"\n#include "map"\n#include "cctype"\n#include "list"\n#include "stack"\n\nusing namespace std;\n#define INF 0x3f3f3f3f\n""" text = text_encode + 'Created on ' + dateStr + text_author + text_include #for region in the selection #一个region是一个选择块,一次可以选择多个块 for r in self.view.sel(): str_r = self.view.substr(r)#str_r是所选择块的文本内容 if 'Created on ' in str_r: if 'Updated on ' in str_r: text = str_r[0:str_r.find('Updated on ')] + 'Updated on ' + dateStr + text_author + text_include else: text = str_r.replace(text_author, '\nUpdated on ' + dateStr + text_author) self.view.erase(edit, r) self.view.insert(edit, r.begin(), text)
3.保存文件时,默认会让选择保存在“…\Sublime Text 3\Date\Packages\User\” 文件夹下,取好文件名insert_signature.py。
按下快捷键Ctrl + ‘ 打开SublimeText的控制台,执行如下命令就可以运行刚刚写的小插件:
view.run_command('insert_signature')
[ { "keys": ["f1"], "command": "insert_signature", "args": {} } ]
![](file:///C:\Users\lxdn\AppData\Roaming\Tencent\Users\1376871863\QQ\WinTemp\RichOle\F}FCG`@O5E2PLUPAFG}A4`R.png)
[![F}FCG`@O5E2PLUPAFG}A4`R](http://www.nczkevin.com/wp-content/uploads/2015/04/FFCG@O5E2PLUPAFGA4R.png)](http://www.nczkevin.com/wp-content/uploads/2015/04/FFCG@O5E2PLUPAFGA4R.png)
将插入的全部选中再按一次快捷键就会有updated 时间。具体插入可以自己根据需要修改。![](file:///C:\Users\lxdn\AppData\Roaming\Tencent\Users\1376871863\QQ\WinTemp\RichOle\F}FCG`@O5E2PLUPAFG}A4`R.png)