sublime3插件开发快速指南

在开发之前,你要看清楚,是开发的哪个版的插件。
这个sublime 3的指导文档
https://www.sublimetext.com/docs/3/api_reference.html#example_plugins

这个sublime 1的指导文档
http://www.sublimetext.com/docs/plugin-examples

不要搞错了。
如果没注意在开发的时候,就会出现ImportError: No module named 'sublimeplugin类似的错误
我走了弯路之后,发现这人和我一样,并写了一篇文章,指出这不好的插件文档。
https://blog.thewheatfield.org/2014/05/30/sublime-text-importerror-no-module-named-sublimeplugin-i-e-bad-plugin-documentation/

step 1

首先选择Tools -> Deveroper -> New Plugin…菜单,可以看到新建了一个插件文件,代码如下:

1
2
3
4
5
import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")

step2

选择Preferences -> Browser Packages…,打开Packages文件夹,新建一个目录Example。
然后将上面的文件保存至该文件夹,比如叫example.py。
这里叫什么,没有关系,因为sublime会监控Packages里面的文件变化,动态加载这些python文件。

step3

然后ctrl+~,会打开控制台
运行view.run_command('example')
就会在文件的第一行中插入Hello, World!

这样一个简单的插件就实现了。

step4

下面为它增加快捷键。
插件编写好以后,你肯定不愿通过运行view.run_command(‘example’)命令来调用插件,这时可以为你的插件绑定快捷键。
打开目录Example,新建文件Default (Windows).sublime-keymap,输入如下内容:

1
2
3
[
{ "keys": ["ctrl+f11"], "command": "example" }
]

这样当按ctrl+f11时,相当于执行了view.run_command('example')

参考

http://zxhfighter.github.io/blog/javascript/2013/07/30/sublime-plugin.html


sublime3插件开发快速指南
https://blog.fengcl.com/2017/06/29/sublime-text-plugin-quickstart/
作者
frank
发布于
2017年6月29日
许可协议