Sublime Text Tips Searching with Regular Expressions & Filtering Search Scope

by RealEyes Media
← Previous Next →

For use in Sublime Text’s “Find In Files…” Dialog (⇧⌘F)

Sublime Text offers many, many options for navigating within your project’s code or searching within either a single file, or the entire codebase of the project. The “Find” Menu offers over a dozen different options for Find and Replace, and depending on which you choose, there are 3 separate “Find” Dialog Panels available.

In this post, I’m going to show a few quick, introductory tips for using the “Find in Files…” Panel. Searching from this Panel will allow you to search across all files in your project, or (as we’ll cover in the second section) select a sub-set of files for a quicker search with more relevant results.

Regex Search Strings

Regular Expressions (which I’ll often refer to as “regex”) offer some incredible capabilities for searching text of any sort. While the material covered in this post is largely beginner level regex, a full introduction to Regular Expressions is far beyond the scope of a single article – I would recommend a resource such as RegexOne.com if you aren’t familiar with Regular Expressions at all.

The actual syntax of regex can vary slightly between coding languages (PHP and JavaScript for instance), but for this article’s purposes Sublime Text uses Perl Regular Expression Syntax from the Boost library. Full documentation can be found at boost.org, which you will want to reference when creating your own search expressions, or modifying and extending the examples offered below.

It’s important to note that in order to use these strings in your searches, you will need to enable Regular Expressions in search by ensuring that the “Regular expression” button (functions as a checkbox) is selected. The appearance can vary depending on the theme you are using with Sublime Text, but generally the button displays either “*” or “.*” as an icon.

While the appearance of these Option Buttons can change depending on the theme in use, I do not recall ever seeing a Sublime Theme change the location or order of the buttons.

These are some useful templates for regex strings to use in the “Find:” field.

Search “Near”

Frequently, I end up searching for code that I have a rough memory of, but not one specific enough to quickly come up with a single, efficient thing to search for. However, I can almost always remember the general shape and structure of things well enough to come up with a pretty unique pair of things to search for that should only occur in close proximity to one another a few times throughout the project.

Variation 1 - Very Basic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Variation 1 - Full String
b[[term-1]]W+(?:w+W+){[[1]],[[10]]}?[[term-2]]b
 
// Breakdown of regex
  b                  // Opening word boundary
          [[term-1]]      // Search Term 1
  W+                 // Any NON-alphanumeric character (W) with one or more repetitions (+)
  (?:                 // Open non-marking sub-expression
    w+               // Any alphanumeric character (w) with one or more repetitions (+)
    W+               // Any NON-alphanumeric character (W) with one or more repetitions (+)
  )                   // Close non-marking sub-expression
  {                   // Open definition of min/max words between search terms
          [[1]],          // minimum quantifier
          [[10]]          // maximum quantifier
  }?                  // Close definition of min/max words between search terms
          [[term-2]]      // Search Term 2
  b                  // Closing word boundary
 
// NOTE: Be sure to remove the [[brackets]] when inserting
//        both your search terms and qualifiers

Returns all instances of [[term-1]] that are at least [[1]] but no more than [[10]] words away from [[term-2]] –

  • Just replace [[term-1]] and [[term-2]] with the strings you want to search for
  • [[1]] and [[10]] are quantifiers which define the minimum and maximum number of words allowed between the two search terms to return a match
  • Using [[1]] as the minimum would mean that any instances where the two words were adjacent would not be considered a match
  • You would need to use [[0]] instead
  • NOTE – Be sure to remove the brackets [[ ]] when inserting search terms and choosing quantifiers

Variation 2

Variation 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Variation 2 - Full string
b(?:[[term-1]]W+(?:w+W+){[[1]],[[10]]}?[[term-2]]|[[term-2]]W+(?:w+W+){[[1]],[[10]]}?[[term-1]])b
 
// Breakdown of regex
  b                  // Opening word boundary
  (?:                 // Open non-marking sub-expression
          [[term-1]]      // Search Term 1
    W+               // Any NON-alphanumeric character (W) with one or more repetitions (+)
    (?:               // Open non-marking sub-expression
      w+             // Any alphanumeric character (w) with one or more repetitions (+)
      W+             // Any NON-alphanumeric character (W) with one or more repetitions (+)
    )                 // Close non-marking sub-expression
    {                 // Open definition of min/max words between search terms
          [[1]],          // Quantifier - minimum # of words between search terms
          [[10]]          // Quantifier - maximum # of words between search terms
    }?                // Close definition of min/max words between search terms
          [[term-2]]      // Search Term 2
    |                 // OR Operator
          [[term-2]]      // Search Term 2
    W+               // Any NON-alphanumeric character (W) with one or more repetitions (+)
    (?:               // Open non-marking sub-expression
      w+             // Any alphanumeric character (w) with one or more repetitions (+)
      W+             // Any NON-alphanumeric character (W) with one or more repetitions (+)
    )                 // Close non-marking sub-expression
    {                 // Open definition of min/max words between search terms
          [[1]],          // Quantifier - minimum # of words between search terms
          [[10]]          // Quantifier - maximum # of words between search terms
    }?                // Close definition of min/max words between search terms
          [[term-1]]      // Search Term 1
  )                   // Close non-marking sub-expression
  b                  // Closing word boundary
 
// NOTE: Be sure to remove the [[brackets]] when inserting
//        both your search terms and qualifiers

Same as above, but will find the terms regardless of which order they are in –

  • Variation 1 will only return instances where [[term-1]] comes before [[term-2]]
  • Variation 2 essentially just adds an OR and then repeats the search string with the [[term-]]values flip-flopped

Variation 3

Variation 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Variation 3 - Full string
b([[term-1]]|[[term-2]]|[[term-3]])(?:W+w+){[[1]],[[10]]}?W+([[term-1]]|[[term-2]]|[[term-3]])b
 
// Breakdown of regex
  b                  // Opening word boundary
  (                   // Open group of Search Terms
          [[term-1]]      // Search Term N
    |                 // OR Operator
          [[term-2]]      // Search Term N
    |                 // OR Operator
          [[term-3]]      // Search Term N
  )                   // Close group of Search Terms
  (?:                 // Open non-marking sub-expression
    w+               // Any alphanumeric character (w) with one or more repetitions (+)
    W+               // Any NON-alphanumeric character (W) with one or more repetitions (+)
  )                   // Close non-marking sub-expression
  {                   // Open definition of min/max words between search terms
          [[1]],         // Quantifier - minimum # of words between search terms
          [[10]]         // Quantifier - maximum # of words between search terms
  }                   // Close definition of min/max words between search terms
  ?W+                // Any NON-alphanumeric character (W) with one or more repetitions (+)
  (                   // Open group of Search Terms
          [[term-1]]      // Search Term N
    |                 // OR Operator
          [[term-2]]      // Search Term N
    |                 // OR Operator
          [[term-3]]      // Search Term N
  )                   // Close group of Search Terms
  b                  // Closing word boundary
 
// NOTE: Be sure to remove the [[brackets]] when inserting
//        both your search terms and qualifiers

Allows you to search for more than just 2 values –

  • Results will be returned if any 2 words in your list are found within the defined proximity of each other
  • This example only adds 1 extra word, but using the syntax illustrated, you could add as many extras as you’d like
    • I wouldn’t recommend going much higher though – for both performance and logic reasons 1
  • As compared to Variation 2 only the list of search terms gets repeated, as opposed to the entire set of search parameters, so that’s better-ish
  • It occurs to me that I’m not 100% certain what will happen if all three search terms are found within the defined range of proximity
    • My guess though, would be that it shouldn’t break anything too thoroughly, you would just end up with a set of results that included each combination listed as a separate match, even though they all pointed back to the same chunk of code 2

I’d be willing to bet that there’s a much more succinct way of accomplishing all this, but these ones work, and I’m still very much in the early stages of learning regex – That being said, if you have any suggestions for improving those strings, I’d love to hear them.

Include/Exclude Filters

These are some useful strings to use in the “Where:” field to make your search run faster, and avoid cluttering your results with, say, the entirety of jQuery in it’s minified format, or all of the libraries you’re pulling in with npm/bower/etc.

Include File Types

*.js,*.tpl.html

Say you’re looking for a style attribute that’s getting added dynamically and throwing off your selectors, because it gets added as an inline style instead of toggling a class – based off of that you know that it’s almost certainly in either a full-blown .JS file, or a script tag somewhere – Combine that with the knowledge that even though our (hypothetical) values are used pretty commonly throughout the CSS, there are probably only a handful of instances that it occurs in the JS. So you could exclude all the CSS files ( -*.css ) from your search and hopefully trim down the number of search results, keeping them more focused (and more likely to be relevant) as well as reducing the number of files being searched by a little bit. However, excluding CSS explicitly will still search every single file of any other file-type.

Exclude Minified Files

-*.min.css,-*.min.js

-OR-

-*.min.*
## Exclude Component Libraries
-*/bower_components/*,-*/npm-modules/*

Conclusion

Although we really just scratched the surface of what Sublime Text and Regular Expressions can do, I hope this quick overview of some of Sublime Text’s more advanced searching capabilities has given you some ideas of how to search more efficiently based on your own preferences, or the needs of a specific project.

Tangents and non sequiturs

  1. I’m sure it affects overall search performance, and in general, adding more possibilities to match against will result in more matches being returned – which is good, I suppose, if you’ve run a more limited search already and the code you’re hoping for is not showing up – but if you get back a huge list of questionable relevance then that defeats the entire purpose of searching in the first place.

  2. So, actually, I bet you could probably write a search with enough parameters, and a large enough range of proximity that (depending on your system specs and the size of your code base) it would in fact hang or crash Sublime Text – but I think you’d have to be trying fairly deliberately to do that.