35

I know how to search for a single string in several files at once with Sublime 3 (explained here).

What I need to do is to search for multiple strings in several files. I've tried going to Find in files and setting:

Field:  string1 \& string2
Where: /path_to_folder_containing_the_files_I_want_to_ search/

(where string1 and string2 are the strings I want to search for) but that doesn't seem to work.

Can this be done at all?

3
  • Gabriel, I think your question would be better on superuser.com
    – samy
    Commented Sep 5, 2014 at 15:26
  • @samy why? There are thousands of questions regarding Sublime here, just click on the tags to see.
    – Gabriel
    Commented Sep 5, 2014 at 15:27
  • 2
    Yeah I agree; this looked like a question about general computing software to me, not programming - so I was thinking that the sublime tag on superuser was most logical: superuser.com/questions/tagged/sublime-text-3
    – samy
    Commented Sep 5, 2014 at 15:30

3 Answers 3

53

Just came across this old question and thought I'd add my solution, which may be useful for somebody in the future.

Sublime supports searching in all open folders and can use regex. So utilizing both, you can open or add all folders you want to search in to the project and use regex to search for multiple keywords. In your case it would be the following (make sure to check the regex box .* icon):

Find: (string1|string2)
Where: <open folders>
3
  • When using a regex in the place of 'string1', 'string2' on 4 strings or more, regex crashes with the error: "The complexity of matching the regular expression exceeded predefined bounds. Try refactoring the regular expression to make each choice made by the state machine unambiguous." Is this relate to this type of search?
    – john
    Commented Dec 8, 2016 at 16:17
  • 1
    @john: can you post your sample regex? Just tried with simple 4 options, and it works just fine on mine.
    – Sherzod
    Commented Dec 8, 2016 at 18:32
  • 4
    apparently I was regex'ing each individual string, rather than encapsulating the entire set as a whole, and that caused the crash. E.g. I wanted this: ^.*(?:string1|string2|string3).*$
    – john
    Commented Dec 10, 2016 at 4:44
6

I use this:

checked Regular expression

Find: (string1.*string2)

Where: *.php
3
  • 1
    This will only work if a file has both strings, and in the order given. (Also, the dot does not match newlines in some regex scenarios...not sure about Sublime's implementation) Better to use | (or operator): (string1|string2) Commented Sep 17, 2018 at 14:27
  • But if i want to find both strings my solution will work.
    – koshin
    Commented Sep 19, 2018 at 5:35
  • 1
    If you want to find them in that order, yes. But if a file has them reversed: string2...string1, this solution will not match that. Commented Sep 19, 2018 at 18:56
0

I tried this on Sublime Text2, so should work on Sublime Text3 as well.

Field: string1 string2 string3 string4 Where: /path_to_folder_containing_the_files_I_want_to_ search/

Note: uncheck '.*' which means Regular Expression and check "" which means look for the whole word.

This will search for the pattern "string1 string2 string3 string4" in all the files in the mentioned folder.

Not the answer you're looking for? Browse other questions tagged or ask your own question.