Sublime 插件开发

阅读更多

开发sublime插件非常简单,以

右键添加“Copy File Name"为例:

 

Step1:

Preferences => Browse Packages... 打开文件夹中新建你的 插件文件夹,本例新建文件夹”FileUtil"

 

Step2:

文件夹中新建两个文件Context.sublime-menu和FileUtil.py

 

Step3: 编辑FileUtil.py

import sublime, sublime_plugin, os

class FilenameToClipboardCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      sublime.set_clipboard(os.path.basename(self.view.file_name()))

class PathToClipboardCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      sublime.set_clipboard(self.view.file_name())

 具体函数列表可以参考官方API: http://www.sublimetext.com/docs/2/api_reference.html

 

Step4: 编辑Context.sublime-menu

 

[

    { "command": "filename_to_clipboard", "caption": "Copy File Name" },

]

 

注意:

Context.sublime-menu中的command "filename_to_clipboard"对应FileUtil.py中相应的 command,

注意对应的规则: 下划线,大小写等等。。。

新添加例子:右键添加“Close"选项,关闭当前文件

Step1: Preferences => Key Binding-Default查到关闭文件的command为"close_file".

Step2:

编辑Context.sublime-menu

[
    { "command": "filename_to_clipboard", "caption": "Copy File Name" },
    { "command": "get_encoding", "caption": "Get Encoding" },
    { "command": "close_file", "caption": "Close" },
]

 

 

另外一个 例子:右键添加“Get Encoding"选项,弹出Msg Box显示文件编码

 

编辑FileUtil.py

class GetEncodingCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      sublime.message_dialog(self.view.encoding())

 

编辑Context.sublime-menu

[

    { "command": "filename_to_clipboard", "caption": "Copy File Name" },
    { "command": "get_encoding", "caption": "Get Encoding" }

]

你可能感兴趣的:(sublime,右键,编码,关闭,复制文件名)