Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text open all files containing search term

When I pres ctrl+shift+F to search across all the files in the current scope, I get a new window listing all the files that contain that search term.

How can I quickly open all of these files?

like image 309
Scorb Avatar asked Oct 17 '16 14:10

Scorb


People also ask

How do I search all files open in Sublime Text?

Use the Search all shortcut: Ctrl + Shift + F , then select the folder in the "Where:" box below. (And for Mac, it's ⌘ + Shift + F ). If the root directory for the project is proj, with subdirectories src and aux and you want to search in all subfolders, use the proj folder.

How do I open multiple files in Sublime Text?

To do so, you need to press Ctrl+K, then Ctrl+Shift+Up. You can repeat as many times as you like. Press Ctrl+K, then Ctrl+Shift+Up to open a new sub-window.

How do I search all text in Sublime Text?

Type your search string into the “Find” text input and make sure the “Where” input says “” and press the “Find” button. It should then search all files in your open project for your search string.

How do you select all occurrences of a word in Sublime Text?

To add all occurrences of the current word to the selection, use Find All: Alt+F3 on Windows and Linux, or Ctrl+Command+G on OS X.


2 Answers

Hold down the F4 key from the Search Results screen, and it will "Navigate to next match" - which causes it to open each file listed in the results.

Just a small note, if you are getting 10+ matches per file this method starts to fail because it gets slow.

like image 93
Vigrant Avatar answered Oct 01 '22 00:10

Vigrant


Sublime doesn't have the ability to do this out of the box; however the plugin API gives you the power to create a plugin to do something like this fairly simply (depending on how you ultimately want it to work).

I assume there are plugins available for something like this, but for reference purposes here is a simple example:

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")

This provides a command named open_all_found_files which could be bound to a key, added to a menu, added to the command palette, etc.

Using the notion that sublime has a custom syntax for the find results with a scope dedicated to the matching filenames, this collects all such regions and then opens the associated files.

The optional command argument new_window can be passed and set to true to open the files in a new window; leaving it off or setting it to false opens the files in the same window as the find results. You can of course change the default as you see fit.

like image 26
OdatNurd Avatar answered Oct 01 '22 01:10

OdatNurd