Sublime 自定义快捷键

sublime插件丰富,当我们安装了诸多插件之后,发现要使用其中的功能还得用鼠标去点,这岂不是太麻烦了,让我们来自定义快捷键吧!

写在前面

在写这篇文章之前,我也为自定义快捷键感到无从下手,而在网上搜索千篇一律的都是打开Preferences->key bindings user,添加一些东西进去,比如等等,这个command是什么玩意?

比如下面

{ "keys": ["ctrl+alt+down"], "command":"duplicate_line" },

等等,这个command是什么?我要去哪儿找?我安装了一个新的插件,能使用快捷键吗?等等一系列的疑问,从未有人解答过,可能一些文章都是抄袭拼凑吧。

如何查找Commands

比如我安装了一个php code sniffer,想要使用一个快捷键来对代码进行检测,然而这个command是什么?看来只能去查看官方文档。

经过一番查找,看到了一个commands的文档,链接如下:

http://sublime-text-unofficial-documentation.readthedocs.io/en/latest/reference/commands.html

其中有有一段

Discovering Commands

There are several ways to discover a command’s name in order to use it as a key binding, in a macro, as a menu entry or in a plugin.

Browsing the default key bindings at Preferences | Key Bindings – Default. If you know the key binding whose command you want to inspect you can just search for it using the search panel. This, of course, also works in the opposite direction.

``sublime.log_commands(True)``

Running the above in the console will tell Sublime Text to print the command’s name in the console whenever a command is run. You can practically just enter this, do whatever is needed to run the command you want to inspect and then look at the console. It will also print the passed arguments so you can basically get all the information you need from it. When you are done, just run the function again with False as parameter.

Inspecting .sublime-menu files. If your command is run by a menu item, browse the default menu file at Packages/Default/Main.sublime-menu. You will find them quick enough once you take a look at it, or see the menu documentation.

Similar to menus you can do exactly the same with .sublime-command files. See Completionsfor some documentation on completion files.

OK,问题解决了,打开控制台(打开控制台的方法是Ctrl+Shift+`),输入

sublime.log_commands(True)

操作会在控制台中显示相应的命令,之前安装的php code sniffer发现sniff this file的命令是phpcs_sniff_this_file

知道了命令之后,不需要再记录的则可以在控制台输入

sublime.log_commands(False)

快捷键配置

最简单的快捷键就是在key bindings修改json配置文件了(你需要知道json格式),比如下面

[
 {
     "command": "phpcs_sniff_this_file",
     "keys": ["f1"]
 }
]

然而这其中还有一些参数

keys
快捷键数组. 组合键可以使用+,比如Alt+s. 可以绑定多个快捷键(比如, [“ctrl+k”,”ctrl+j”]),当一个不可用时候,会选择另外一个
command

需要执行的命令

args
传给命令的参数
context
决定命令的上下文数组. 所有的条件必须为真,使上下文处于活跃状态. 查看 Structure of a Context 了解更多.

这是一个简单的例子:

{ "keys": ["shift+enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
   [
      { "key": "setting.auto_indent", "operator": "equal", "operand": true },
      { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
      { "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
      { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
   ]
}

感觉过于复杂,暂时不写了。一些简单的快捷键够用了

赞赏

微信赞赏支付宝赞赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注