当我按下ctrl+shift+F以在当前范围内搜索所有文件时,会出现一个新窗口,列出包含该搜索词的所有文件。
我如何快速打开所有这些文件?
当我按下ctrl+shift+F以在当前范围内搜索所有文件时,会出现一个新窗口,列出包含该搜索词的所有文件。
我如何快速打开所有这些文件?
从搜索结果界面按住F4键,它将“导航到下一个匹配项”,这会导致打开结果列表中列出的每个文件。
顺便提一下,如果每个文件有10个以上的匹配项,此方法开始变得缓慢且无效。
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
会在与查找结果相同的窗口中打开文件。当然,您可以根据需要更改默认设置。你不能在Sublime Text内部完成这个操作。
如果你使用的是Linux/UNIX/OSX系统,可以通过在命令行中使用grep
和xargs
组合来打开包含特定字符串或匹配正则表达式的所有文件,例如:
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
subl
后添加-n或--new-window
,以便在新窗口中打开它们。我不得不修改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")