问题描述
更深入地了解我之前的问题 为什么不这个 Sublime Text 的热键配置不行吗?.现在我来谈谈 sublime 命令的实现,它确实是一种破解 sublime 命令的令人困惑的方法,因为在前一个线程中探索以找到用于打开浏览器的命令,最后我在帮助下找到了它@MattDMo.
One more depth into my former question Why doesn’t this hotkey configuration for Sublime Text work?. Now I come to the implementation of sublime command, it is really a confusing way to hack what commands sublime has, as the exploration in former thread in order to find the command which is used to open a browser, finally I found it with help of @MattDMo.
然后我发现 Packages/Default 文件夹中有一个名为 open_in_browser.py 的文件,我猜命令只是 .py 文件的文件名,但实际上我找不到对应的文件命名 find_pre.py 命令 find_prev,然后我将 open_in_browser.py 复制为 open_browsers.py,并添加 { "keys": ["ctrl+b"], "command": "open_browsers"} 升华键盘映射,但它不起作用.然后我意识到应该有一些地方将sublime命令注册到他们的实现中,那么如果有这样的机制,那是什么?我在哪里可以找到它?
Then I find there is one file named open_in_browser.py in the Packages/Default folder, I guess the commands is just the file name of .py files, but in fact I cannot find the corresponding file which could be named find_pre.py to command find_prev, then I copy open_in_browser.py as open_browsers.py, and add { "keys": ["ctrl+b"], "command": "open_browsers"} to sublime keymap, but it doesn't work. Then I realized that there should be some place which registers sublime commands to their implementation, so if there is such a mechanism, what is it? Where can I find it?
推荐答案
TL;DR
在 Packages/User
目录中创建一个任意名称的文件.使用 run
方法在文件中创建一个类,例如 MyTestCommand
.使用蛇形大小写的类名创建键盘映射,不带 Command
后缀.使用命名参数将任何内容传递给命令.
Create a file with any name in the Packages/User
directory. Create a class in the file like MyTestCommand
with a run
method. Create a keymap using the class name in snake case and without Command
suffix. Use named arguments to pass anything to the command.
完整答案
无需注册任何内容即可创建自定义命令.文件名并不重要,因为 Sublime Text 只是扫描 .py
脚本的目录并自动执行它们(注册它们).
There is no need to register anything to create custom commands. Filename doesn't matter as Sublime Text simply scans it's directories for .py
scripts and automatically executes them (registers them).
这是我使用的示例脚本:
Here is the example script I use:
import sublime
import sublime_plugin
class ChangeViewCommand(sublime_plugin.WindowCommand):
def run(self, reverse=False):
window = self.window
group, view_index = window.get_view_index(window.active_view())
if view_index >= 0:
views = window.views_in_group(group)
if reverse:
if view_index == 0:
view_index = len(views)
if reverse:
new_index = view_index - 1
else:
new_index = (view_index + 1) % len(views)
window.focus_view(views[new_index])
它的作用是 - 切换到当前组中的下一个/上一个选项卡(默认行为围绕所有选项卡组).
So what it does - switches to the next/previous tab in the current group (the default behavior circles around all tab groups).
因此我们只需将其保存为Packages/User
目录中的任意名称.
So we simply save it as any name in Packages/User
directory.
然后我们必须在我们的用户键盘映射文件中创建键绑定:
Then we must create the key bindings in our user keymap file:
{ "keys": ["ctrl+tab"], "command": "change_view" },
{ "keys": ["ctrl+shift+tab"], "command": "change_view", "args": {"reverse": true} },
如您所见,命令是类名的snake_case
,没有Command
后缀.这将使用命名参数运行类的 run
方法.
As you may see, the command is snake_case
of the class name without the Command
suffix. This will run the class's run
method with named arguments.
这是否回答了您的问题?为了在出现任何错误时进行调试 - 打开 ST 控制台(默认快捷方式是 + )
Does this answer your question? For debugging in case of any errors - open the ST console (default shortcut is + )
这篇关于我可以在 sublime 中创建自己的命令以及如何将 python 实现与该命令相关联吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!