开启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的结构

  1. “keys”: An array with a key combination that this mapping will respond to. In this case, the “escape” key.
  2. “command”: A method that sublime uses internally to accomplish the task.
  3. “context”: A configuration object that tells sublime to only respond to this key combination under specific circumstances. In this case,setting.command_mode needs to be false. 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)