打开当前打开文件的文件夹左侧面板的快捷方式

Shortcut to open the currently opened file's folder left panel

我知道 Sublime 中的 “文件 > 打开文件夹...” 对话框。

问题在于:

如何在 current Sublime window 的左侧“文件夹视图”中打开当前文件的文件夹而不弹出任何窗口?(我想为此绑定一个键盘快捷键)。注:我还在用Sublime 2.

用@OdatNurd 的想法解决了 ST2:

class OpenthisfolderCommand(sublime_plugin.TextCommand): 
    def run(self, edit):
        current_dir = os.path.dirname(self.view.file_name())
        subprocess.Popen('"%s" -a "%s"' % ("c:\path\to\sublime_text.exe", current_dir))

添加键绑定,例如:

{ "keys": ["ctrl+shift+o"], "command": "openthisfolder"}

菜单项 Project > Add Folder to project... 将提示您输入文件夹名称,然后将其添加到当前 window 而不是创建新文件夹。与名称相反,即使您没有明确直接使用 sublime-project 文件,这也始终有效。

要在没有任何提示的情况下执行此操作,需要一个插件来调整当前在 window 中打开的文件夹列表。

Sublime Text 3及以上API支持直接修改window中打开的文件夹列表,而Sublime Text 2只有API用于查询文件夹列表。

所有版本的 Sublime 都有一个命令行助手,可用于与 Sublime 的 运行 副本(通常称为 subl)进行交互,以及它可以做的事情之一通过添加一个额外的文件夹来扩充 window 中的文件夹列表。在 Sublime Text 2 中,subl 帮助程序本身就是主要的 Sublime Text 可执行文件。

下面是一个可以在Sublime Text 2及以上版本中使用的插件,它会执行相应的操作来获取当前文件的路径,以便在侧边栏中打开。如果您不确定如何使用插件,请参阅 this video on how to install them

import sublime
import sublime_plugin

import os


# This needs to point to the "sublime_text" executable for your platform; if
# you have the location for this in your PATH, this can just be the name of the
# executable; otherwise it needs to be a fully qualified path to the
# executable.
_subl_path = "/home/tmartin/local/sublime_text_2_2221/sublime_text"

def run_subl(path):
    """
    Run the configured Sublime Text executable, asking it to add the path that
    is provided to the side bar of the current window.

    This is only needed for Sublime Text 2; newer versions of Sublime Text have
    an enhanced API that can adjust the project contents directly.
    """
    import subprocess

    # Hide the console window on Windows; otherwise it will flash a window
    # while the task runs.
    startupinfo = None
    if os.name == "nt":
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

    subprocess.Popen([_subl_path, "-a", path], startupinfo=startupinfo)


class AddFileFolderToSideBarCommand(sublime_plugin.WindowCommand):
    """
    This command will add the path of the currently focused file in the window
    to the side bar; the command will disable itself if the current file does
    not have a name on disk, or if it's path is already open in the side bar.
    """
    def run(self):
        # Get the path to the current file and add it to the window
        self.add_path(self.get_current_path())

    def is_enabled(self):
        """
        The command should only be enabled if the current file has a filename
        on disk, and the path to that file isn't already in the list of folders
        that are open in this window.
        """
        path = self.get_current_path()
        return path is not None and path not in self.window.folders()

    def get_current_path(self):
        """
        Gather the path of the file that is currently focused in the window;
        will return None if the current file doesn't have a name on disk yet.
        """
        if self.window.active_view().file_name() is not None:
            return os.path.dirname(self.window.active_view().file_name())

        return None

    def add_path(self, path):
        """
        Add the provided path to the side bar of this window; if this is a
        version of Sublime Text 3 or beyond, this will directly adjust the
        contents of the project data to include the path. On Sublime Text 2 it
        is required to execute the Sublime executable to ask it to adjust the
        window's folder list.
        """
        if int(sublime.version()) >= 3000:
            # Get the project data out of the window, and then the list of
            # folders out of the project data; either could be missing if this
            # is the first project data/folders in this window.
            project_data = self.window.project_data() or {}
            folders = project_data.get("folders", [])

            # Add in a folder entry for the current file path and update the
            # project information in the window; this will also update the
            # project file on disk, if there is one.
            folders.append({"path": path})
            project_data["folders"] = folders
            self.window.set_project_data(project_data)
        else:
            # Run the Sublime executable and ask it to add this file.
            run_subl(path)

这将定义一个名为add_file_folder_to_side_bar的命令,它将当前文件的路径添加到侧边栏;如果当前文件在磁盘上没有名称,或者如果该路径已在边栏中打开,则该命令将自行禁用。

如果您使用的是 Sublime Text 2,请注意您需要调整顶部的变量以指向您的 Sublime 副本的安装位置(包括程序本身的名称,如示例代码所示), 因为插件需要能够调用它来调整侧边栏。

为了触发命令,您可以使用键绑定,例如:

{ "keys": ["ctrl+alt+a"], "command": "add_file_folder_to_side_bar"},

您还可以在您的 User 包中创建一个名为 Context.sublime-menu 的文件(与您放置插件的位置相同),其中包含以下内容,也可以为此创建一个上下文菜单项:

[
    { "caption": "-", "id": "file" },
    { "command": "add_file_folder_to_side_bar", "caption": "Add Folder to Side Bar",}
]