ST3:如何在宏中使用插件的命令?

1 投票
1 回答
524 浏览
提问于 2025-04-17 21:55

在Sublime Text 3中,我想创建一个宏,能够粘贴当前文件的“相对路径(从项目开始,已编码)”。这个功能是由一个很棒的插件提供的,叫做Sidebar Enhancements

为了跟踪我的实验,我开启了控制台日志记录:

  • 打开控制台(按 Ctrl-`
  • 输入 sublime.log_commands(True)

当我执行想要记录的操作时,我在控制台中看到了:

command: side_bar_copy_path_absolute_from_project_encoded
command: paste

但是当我录制这些操作时,保存后生成的json是:

[
    {
        "args": null,
        "command": "paste"
    }
]

如果我手动修改宏(根据非官方Sublime文档SidebarEnhancements / Side Bar.sublime-menu中的提示):

[
    { "command": "side_bar_copy_path_relative_from_project_encoded", "args": {"paths": []} },
    { "command": "paste", "args": null  }
]

…然后播放这个宏时,我在控制台中看到了以下内容:

Unknown macro command side_bar_copy_path_relative_from_project_encoded

(我还尝试过将 null 作为 side_bar_copy_path_relative_from_project_encoded 的参数。)

我需要在我的宏命令中引用包名或类名吗?有没有什么建议?

1 个回答

0

太好了!我搞定了 - 我觉得宏可能不支持所有的命令,所以我试了一个插件:

import sublime, sublime_plugin

'''
    Paste the current file's Relative Path From Project (Encoded).
    Requires https://github.com/titoBouzout/SideBarEnhancements to be installed

    First save this file as:
    ~/Library/Application Support/Sublime Text 3/Packages/User/paste-path.py

    More details about writing plugins:
    http://docs.sublimetext.info/en/latest/reference/plugins.html

    To trigger the command with 'Command Option Shift p',
    add the following to your Sublime Text > Preferences > Keybindings - User

    [
        { "keys": ["super+option+shift+p"], "command": "paste_path"}
    ]
'''

class PastePathCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.window.run_command("side_bar_copy_path_relative_from_project_encoded", {"paths": []})
        self.window.run_command("paste")

这里有个链接,可以查看具体内容: https://gist.github.com/9505770


补充一下:抱歉,信息不全!你需要把这个文件保存为类似这样的名字:

~/Library/Application Support/Sublime Text 3/Packages/User/paste-path.py

更多信息可以查看这里: https://docs.sublimetext.io/reference/plugins.html

撰写回答