如何根据自己的实际需求开发属于自己的sublime text插件

简介: 如何根据自己的实际需求开发属于自己的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.


image.pngThen 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”.


image.png2) Tools->Command Palette, then click “Preferences: Key Bindings – User”:image.pngit 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.


相关文章
|
2天前
|
前端开发 JavaScript Linux
Sublime Text 3配置Go语言开发环境
【4月更文挑战第13天】本篇文章 Huazie 向大家介绍使用 Sublime Text 3搭建Go语言开发环境,并演示编译运行 Go语言代码
33 8
Sublime Text 3配置Go语言开发环境
|
1月前
|
前端开发
Sublime Text 3 快捷键总结(拿走)
Sublime Text 3 快捷键总结(拿走)
14 0
|
1月前
|
数据安全/隐私保护
sublime 如何安装ftp插件
sublime 如何安装ftp插件
13 1
sublime 如何安装ftp插件
|
3月前
Sublime Text 4 设置中文、汉化
Sublime Text 4 设置中文、汉化
142 0
|
4月前
|
程序员 开发工具 开发者
猿如意开发工具|Sublime Text(4126)
猿如意开发工具|Sublime Text(4126)
|
4月前
|
JavaScript Linux
Sublime Text 3 在 Linux 中配置
Sublime Text 3 在 Linux 中配置
|
5月前
sublime text 远程开发
sublime text 远程开发
|
5月前
|
Web App开发
解决Sublime Text用浏览器打开没反应
解决Sublime Text用浏览器打开没反应
|
11月前
|
前端开发
Sublime Text 3 快捷键总结(拿走)
以下是个人总结不完全的快捷键总汇,祝愿各位顺利解放自己的鼠标。
128 0
|
Ubuntu Linux
Ubuntu中安装Sublime Text 3.2并创建桌面快捷方式
Ubuntu中安装Sublime Text 3.2并创建桌面快捷方式
220 0