如何根据自己的实际需求开发属于自己的sublime text插件
In my spare time I would like to use Sublime Text to write some small pieces of HTML5 code. Every time after I finish the edit, I have to manually open the html page via Chrome manually, which is very inconvenient. Thanks to the great flexibility of Sublime Text, we can create our own plugin with minor effort to make things done automatically:
Edit html page in Sublime Text -> click some short key defined by ourselves -> html page is opened by Chrome
(1) Open Sublime Text, menu Tools->New Plugins, then a python file is automatically created for us as below.
Then paste the following simple python source code:
import sublime, sublime_plugin
import webbrowser
class OpenBrowserCommand(sublime_plugin.TextCommand):
def run(self,edit):
window = sublime.active_window()
window.run_command('save')
url = 'file://' + self.view.file_name()
webbrowser.open_new(url)
The logic is quite easy: first save the currently edited file, get its path and open it via the default browser installed in your laptop. Save it into /Packages/User. Rename the file as you wish, for example “open_browser.py”.
(2) Tools->Command Palette, then click “Preferences: Key Bindings – User”:
paste the following source code:
[{ “keys”: [“ctrl+shift+b”], “command”: “open_browser” }]
it means you tell Sublime Text to execute your plugin “open_browser” when the combination key “ctrl+shift+b” is pressed. Now it is done. After you press the keys you configured, the default browser will be opened by your plugin.
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法