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

7

当我按下ctrl+shift+F以在当前范围内搜索所有文件时,会出现一个新窗口,列出包含该搜索词的所有文件。

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


https://packagecontrol.io/packages/BetterFindBuffer - Keith Hall
1
您可以在文件列表中双击文件以逐行打开每个文件。但是这不会同时打开所有文件。 - Pᴇʜ
BetterFindBuffer很糟糕。它为什么会改变我的主题? - Scorb
4个回答

26

从搜索结果界面按住F4键,它将“导航到下一个匹配项”,这会导致打开结果列表中列出的每个文件。

顺便提一下,如果每个文件有10个以上的匹配项,此方法开始变得缓慢且无效。


3
这个回答非常完美。 - kloddant
无效 - 至少当你遵循你所写的内容时。 - Jeffz
1
Jeffz,你是在Windows还是Mac上尝试了吗?我只在Mac上测试过。 - Vigrant
2
在Windows上工作。Sublime Text 2- - Tomas Gonzalez
这太棒了,我以为我的代码有 bug,但显然当你有一个直接路径和所有打开的文件夹时,它会显示两个相同的文件。在我的情况下,在查找窗口中显示了 4 个文件,但实际上只有 2 个。以防万一有人遇到类似的问题。我是在 ST3 中工作的。谢谢。 - Ste

6

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,以在新窗口中打开文件;省略它或将其设置为false会在与查找结果相同的窗口中打开文件。当然,您可以根据需要更改默认设置。

谢谢。我在考虑深入插件开发以获得解决方案。我喜欢这个例子!谢谢。 - Scorb
我把这个放在用户目录下的 .py 文件里? - Scorb
1
没错,只要是.py文件就可以使用任何文件名。然后你只需要将一个键绑定到命令“open_all_found_files”即可。 - OdatNurd
我按照那些步骤,在聚焦于查找文件缓冲区窗口时按下了快捷键命令,但没有成功。 - Scorb
嗯。在我发布这篇文章之前,我已经测试过了,一切都没问题。不过我只检查了最新版本的Sublime 3,所以早期版本(或ST2)可能会有问题。控制台中是否生成了任何错误? - OdatNurd
太棒了!👍 - OdatNurd

1

你不能在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窗口中打开。在subl后添加-n或--new-window,以便在新窗口中打开它们。
如果您使用的是Windows,请考虑使用GOWCygwin

0

我不得不修改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")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接