前言
還記得剛實習(xí)的時候,那時候經(jīng)常使用sql,然后需要查找一堆特定的id返回來的數(shù)據(jù),
id存在Excel表格的一列里面,我們需要將id復(fù)制出來兩邊加上雙引號,然后用逗號分隔放在sql的in()里面.
單純的我將數(shù)據(jù)復(fù)制到文本編輯器里面协饲,然后一個一個加雙引號加逗號,然后再變成一行.
你們能想象在數(shù)據(jù)多一些的時候,那酸爽的程度...
好在隔壁的大佬解救了我,向我介紹了Sublime Text 和里面的批量編輯功能

這一神奇又裝逼的效果成功的吸引了我,從此過上了吸Sublime Text 的生活...
對上圖操作分析我們可以發(fā)現(xiàn)做了下面步驟
ctrl + a = 全選
ctrl + shift + l = 選中行批量編輯
ctrl + home = 跳到行首
插入字符串 = "
ctrl + end = 跳到行尾
插入字符串 = ",
delte = 右刪除
ctrl + end = 跳到行尾
backspace = 左刪除
上面的操作大大縮短了我前面提到的場景花費的時間,在不短的一段時間里,經(jīng)常敲這一段代碼,甚至閉著眼也能打出上面一套連招.但是對于喜歡懶到極致的想法:我可不可以更快,甚至只按一個鍵就實現(xiàn)上面的操作呢?
設(shè)置過自定義快捷鍵的小伙伴們往核,就會知道每一個keys對應(yīng)的其實是一個Sublime封裝好的command,如:
{ "keys": ["ctrl+a"], "command": "select_all" },
{ "keys": ["ctrl+shift+l"], "command": "split_selection_into_lines" },
可以看到我們的 "ctrl+a" 其實對應(yīng)的就是 "select_all" 命令, "ctrl+shift+l" 對應(yīng)的就是 "split_selection_into_lines" ,我們可以從字面意思判斷該快捷鍵的功能,同時想知道某個功能的快捷鍵對應(yīng)的是什么的時候,可以在keys binding里面搜索關(guān)鍵詞找到對應(yīng)的keys.
既然一個"keys"對應(yīng)的是一個命令,那么可不可以一個"keys"對應(yīng)多個命令呢?這就是本篇文章要講的核心內(nèi)容.
正文
sublime.log_commands(True)
在sublime里面輸入 ctrl + ` 打開console面板,輸入 sublime.log_commands(True) 開啟log模式. 之后的每一次操作,都會在console里面顯示出來.

創(chuàng)建一個 command
import sublime
import sublime_plugin
class XxxCommand(sublime_plugin.WindowCommand):
def run(self, reverse=False):
window = self.window
window.run_command("select_all")
window.run_command("split_selection_into_lines")
用上面的模板創(chuàng)建一個xxx.py(文件名可以隨意,建議對應(yīng)class name)存到/User目錄下面,當保存的時候,Sublime會自動重新加載User下面修改/新增的py文件.
此處可以不需要安裝python環(huán)境,因為Sublime有自帶Python,只需要保存,Sublime Text就會重新加載.
根據(jù)這個特性可以用print()來輸出一些log,在初學(xué)python的時候很方便的測試.

如果你使用ctrl+b去編譯的話,可能會報下列錯誤,因為sublime_plugin包并不在python的libs里面,需要自行導(dǎo)入sublime的包到libs下.
ModuleNotFoundError: No module named 'sublime_api'
sublime會根據(jù)class名生成對應(yīng)的command,注意class name 要使用駝峰式結(jié)構(gòu),且一定為Command結(jié)尾
XxxCommand = xxx
AaaBbbCommand = aaa_bbb
AaaBbbCccCommand = aaa_bbb_ccc
新建一個keys Mapping
當我們保存上面的文件xxx.py 到/User目錄下面后,使用ctrl+`可以看到sublime 已經(jīng)重新加載了xxx.py文件,那么此時在sublime 里面其實已經(jīng)有了xxx命令了,我們在到Key Bindings里面添加我們的Keys Mapping
{ "keys": ["f5"], "command": "xxx" },
此時我們使用f5,即相當于執(zhí)行了xxx命令,然后就會執(zhí)行XxxCommand里面的run方法,而依次執(zhí)行 select_all, split_selection_into_lines 命令

window.run_commad()
Runs the named WindowCommand with the (optional) given args. This method is able to run any sort of command, dispatching the command via input focus.
run_command(string, <args>)
參考官方文檔API 可以看到run_command有兩個參數(shù),
第一個參數(shù)是命令的名字,第二個參數(shù)是命令的入?yún)?如果有一些命令有入?yún)⒌脑?可以填參數(shù),下面是使用的例子
import sublime
import sublime_plugin
class xxxCommand(sublime_plugin.WindowCommand):
def run(self, reverse=False):
window = self.window
window.run_command("select_all")
window.run_command("split_selection_into_lines")
window.run_command("move_to",{"extend":False,"to":"bol"})
window.run_command("insert", {"characters": "\""})
window.run_command("move_to",{"extend":False,"to":"bol"})
window.run_command("move_to",{"extend":False,"to":"eol"})
window.run_command("insert", {"characters": "\","})
window.run_command("right_delete")
window.run_command("move_to",{"extend":False,"to":"eol"})
window.run_command("left_delete")
效果

相關(guān)命令的參數(shù)可以先打開Log,然后輸入對應(yīng)命令的鍵,再根據(jù)輸出的log就確定需要哪些參數(shù)了.
先到這先,希望對你有幫助.