【问题标题】:Sublime Text open all files containing search termSublime Text 打开所有包含搜索词的文件
【发布时间】:2017-02-26 14:01:21
【问题描述】:

当我按 ctrl+shift+F 搜索当前范围内的所有文件时,我会看到一个新窗口,其中列出了包含该搜索词的所有文件。

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

【问题讨论】:

  • 您可以双击该文件列表中的文件以连续打开每个文件。但是,这不会一次打开所有文件。
  • BetterFindBuffer 太可怕了。为什么它会改变我的主题???

标签: sublimetext2 sublimetext3 sublimetext


【解决方案1】:

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

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

【讨论】:

  • 这个答案很完美。
  • 不起作用 - 至少在您遵循您所写的内容时。
  • Jeffz,你在 windows 或 mac 上试过吗?我只在 Mac 上测试过。
  • 在 Windows 中工作。崇高文本 2-
  • 这很好,我以为我有一个错误,但显然当你有一个直接路径和所有打开的文件夹时,它会显示两个相同的文件。就我而言,查找窗口中显示了 4 个文件,但只有 2 个。以防万一有人遇到类似问题。在 ST3 工作。谢谢。
【解决方案2】:

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 在与查找结果相同的窗口中打开文件。您当然可以根据需要更改默认设置。

【讨论】:

  • 谢谢。我在想我会潜入插件开发以获得解决方案。我喜欢这个例子!谢谢。
  • 我把这个是.py文件放到用户目录下?
  • 是的;只要是 .py 文件,任何文件名都可以。然后你只需要绑定一个键到命令open_all_found_files
  • 当我专注于查找文件缓冲区窗口时,我按照这些步骤并按了热键命令,但没有成功。
  • 嗯。我在发布之前对其进行了测试,一切正常。不过,我只检查了最新版本的 Sublime 3,所以早期版本(或 ST2)可能不会。控制台是否产生错误?
【解决方案3】:

你不能在 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 or --new-window 以在新窗口中打开它们。

如果您使用的是 Windows,请考虑使用 GOWCygwin

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多