開啟vi mode后,可以使用很多的VI快捷方式,所以我的sublime已經不是單純的st了,st的VI模式不完全支持所有的快捷鍵。我們來看一段官網的key bindings示例:
{
"keys": ["j", "j"], "command": "exit_insert_mode",
"context":
[
{ "key": "setting.command_mode", "operand": false },
{ "key": "setting.is_widget", "operand": false }
]
}
這里使用jj來退出VI的insert模式,比Escape好,手指不用伸老長了。而且我感覺jj也很人性化,用上一段時間也感覺可以。於是,我在想了,這個東西怎么自己設置。要設置key bindings,需要知道它有一些規定的參數。滿足復雜的需求,但是我今天只是寫個簡單的。
一個key bindings的結構
- “keys”: An array with a key combination that this mapping will respond to. In this case, the “escape” key.
- “command”: A method that sublime uses internally to accomplish the task.
- “context”: A configuration object that tells sublime to only respond to this key combination under specific circumstances. In this case,
setting.command_modeneeds to befalse. Meaning you’re in insert mode. This makes sense, you can’t exit insert mode, if you’re not in it.
首先,我們要知道command從哪里來,keys這個簡單,一看就知道它就是快捷鍵。當我們ctrl+`就可以調出控制台。輸入:
sublime.log_commands(True)
這時,你所有的操作都會顯示出來。
比如我現在想加入一個讓它在Command模式下跳到行首的快捷鍵,如果是vi的話,我們是可以用ctrl+^。這樣的話,手指又要做奇怪的1+1。當我們用了key bindings,我們的手指就不用離開原來的位置了。
這時,我們按下HOME鍵,下面就顯示:command: set_motion {"motion": "vi_move_to_first_non_white_space_character", "motion_args": {"extend": true}}
不要傻眼了,照搬回去就行了。
{
"keys":["g","h"], "command": "set_motion",
"args": {"motion": "vi_move_to_first_non_white_space_character", "motion_args": {"extend": true}}
}
這樣就完成一個了,如果有什么錯誤歡迎指出,這也是我第一個寫的Key binding。
*不要忘記了 sublime.log_commands(False)
