Sublime Text 打开所有包含搜索词的文件

Sublime Text open all files containing search term

当我按 ctrl+shift+F 搜索当前范围内的所有文件时,我得到一个新的 window 列表包含该搜索词的所有文件。

如何快速打开所有这些文件?

您不能在 Sublime Text 中执行此操作。

如果您使用 Linux/UNIX/OSX,您可以在命令行上使用 grepxargs 的组合来打开所有包含特定字符串或匹配正则表达式的文件命令如下:

grep -rlZ "search_str_or_regex" /path/to/search/* | xargs -0 subl

// Command line options (may vary between OSes):
//
// grep -r     Recurse directories
// grep -l     Output only the filenames of the files which contain the search pattern
// grep -Z     Output null terminated filenames
// xargs -0    Input filenames are null terminated
// xargs subl  Sublime Text executable
//
// The combination of -Z and -0 allows filenames containing spaces to be handled

文件将在最近使用的 Sublime Text 中打开 window。在 subl 之后添加 -n or --new-window 以在新的 window.

中打开它们

如果您正在使用 Windows,请考虑使用 GOW or Cygwin

Sublime 没有开箱即用的能力;然而,插件 API 使您能够创建一个插件来相当简单地执行此类操作(取决于您最终希望它如何工作)。

我假设有类似这样的插件可用,但这里有一个简单的例子供参考:

import sublime
import sublime_plugin

class OpenAllFoundFilesCommand(sublime_plugin.TextCommand):
    def run(self, edit, new_window=False):
        # Collect all found filenames
        positions = self.view.find_by_selector ("entity.name.filename.find-in-files")
        if len(positions) > 0:
            # Set up the window to open the files in
            if new_window:
                sublime.run_command ("new_window")
                window = sublime.active_window ()
            else:
                window = self.view.window ()

            # Open each file in the new window
            for position in positions:
                window.run_command ('open_file', {'file': self.view.substr (position)})
        else:
            self.view.window ().status_message ("No find results")

这提供了一个名为 open_all_found_files 的命令,它可以绑定到一个键、添加到菜单、添加到命令选项板等。

使用 sublime 具有用于匹配文件名的范围的查找结果的自定义语法的概念,这会收集所有此类区域,然后打开关联的文件。

可以传递可选的命令参数 new_window 并将其设置为 true 以在新 window 中打开文件;将其关闭或将其设置为 false 将在与查找结果相同的 window 中打开文件。您当然可以根据需要更改默认值。

在“搜索结果”屏幕中按住 F4 键,它将 "Navigate to next match" - 这会导致它打开结果中列出的每个文件。

请注意,如果每个文件的匹配项超过 10 个,则此方法开始失败,因为它变慢了。

我不得不更改 OdatNurd 的代码。我在文件名末尾的“:”和“”有问题...在 Mac OS Big Sur...

import sublime
import sublime_plugin

class OpenAllFoundFilesCommand(sublime_plugin.TextCommand):
    """
    Collect the names of all files from a Find in Files result and open them
    all at once, optionally in a new window.
    """
    def run(self, edit, new_window=False):
        # Collect all found filenames
        positions = self.view.find_by_selector("entity.name.filename.find-in-files")
        if len(positions) > 0:
            # Set up the window to open the files in
            if new_window:
                sublime.run_command("new_window")
                window = sublime.active_window()
            else:
                window = self.view.window()

            # Open each file in the new window
            for position in positions:
                file = self.view.substr (position)
                #print(file)
                file = file.replace(":","")
                window.run_command('open_file', {'file': file.strip()})
        else:
            self.view.window().status_message("No find results")

    def is_enabled(self):
        return self.view.match_selector(0, "text.find-in-files")