寫下簡單的sublime text插件

sublime text 是一個跨平臺的文本編輯工具,用了下還是比較方便。
安裝后,除了默認的語法支持外,如果想找下額外的功能,可以通過安裝插件的方法來做。
快捷鍵 Ctrl+Shift+p 敲入install 可以看到已發布的插件列表。根據自己情況選擇需要的插件。
但是有的時候,找不到自己想要的插件,那么只有自己編寫了。
自己編寫插件必備條件:
1、會基本python腳本語言
2、了解sublime text插件的api,這個通過官方文檔查看
http://www.sublimetext.com/docs/3/
3、了解創建插件的過程,可以通過這個地址了解:
https://code.tutsplus.com/tutorials/how-to-create-a-sublime-text-2-plugin--net-22685

我寫了個一個簡單的sftp上傳的插件:

pscp.exe 可以到網上下一個:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

目錄結構:
example/example.py
example/bin/pscp.exe

example.py代碼如下

import sublime
import sublime_plugin
import os
import sys
import re

bin_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bin')
has_bin = os.path.exists(bin_folder)
pscp_exe = os.path.join(bin_folder, 'pscp.exe')
has_psftp = os.path.exists(pscp_exe)
remote_ip = '1.1.1.1' # replace your ip
remote_ssh_password = 'you_password' # replace your you_password
pscp_exe_cmd = "\"" + pscp_exe + "\" -pw \"" + remote_ssh_password + "\" \"%s\" root@" + remote_ip + ":\"%s\" "
base_path = "D:\\svn\\project1\\" # repalce you base  path
remote_path = "/data/project1/"   # repalce you remote path

def get_relative_path(full_path,base_path):
    full_path = re.sub(r"\\", "/", full_path)
    base_path = re.sub(r"\\", "/", base_path)
    if (re.match(base_path, full_path, re.I)):
        relative_path = re.sub(base_path, "", full_path, re.I)
        return relative_path
    else:
        return ""

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if os.name == 'nt' and (not has_bin or not has_psftp):
            sublime.error_message("sftp plugin is invalid")
        else:
            file_path = self.view.file_name()
            relative_path = get_relative_path(file_path,base_path)
            remote_file_path = remote_path + relative_path
            if (relative_path == ""):
                sublime.error_message("can not sftp to server, due to file not in base_path")
            else:
                cmd = pscp_exe_cmd % (file_path,remote_file_path)
                ret = os.popen(cmd)
                output = ret.read()
                if re.search(r'100%',output):
                    msg = relative_path+" upload success!"
                else:
                    msg = relative_path+" upload failed"
                sublime.message_dialog(msg)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容