Sublime Forum

Noob question: Nested search?

#1

Hi, I don’t know the proper terminology for this, but say I am searching a .css file for a navbar’s font-size, is there an easy way to make this search specific? If I’m looking through the .css file, say, of Twitter Bootstrap, there are a lot of styles for the navbar, making it difficult to just browse, but if I search for ‘font-size,’ I will get everything outside of the navbar as well. I know I can select a given section and search, but even this can be cumbersome. Ideally, I could do some kind of ‘nested search’ for lack of a better term. Like ‘.navbar { font-size’

0 Likes

#2

sure, you just need the proper regex.

If you are familiar with using something like .* to represent a “wildcard” in a search, regex’s, or regular expressions, vastly expands on that idea. Basically, instead of searching for your literal text, you can use a combination of symbols to describe how you want it to search. It’s perfect for the kind of situation you describe. Most modern programming languages include some sort of regex type, for doing string manipulation. And any decent text editor will employ them to some degree in their find-and-replace functionality. Sublime Text uses on of the best regular expression libraries on the planet, Oniguruma for powering it’s syntax highlighting and some of its other capabilities. I am not sure, and hopefully someone who knows will chime in, but I would imagine that is also what is behind its Find and Replace tool also.

In Sublime Text, when you bring up the Find bar, there will be a section on the left for choosing some options and one of those is Regex.

If you select it, Then you can type a regex in there instead of just characters to search for, and it will apply your regex. So, for the example you give. You should be able to get close with something like:

(?<=navbar\s.)(\s.*\s)*\s(font-size)

This won’t actually select just your word, but it will get you in the ballpark. Your particular use case turns out to actually be kind of tricky. At least for me. I’m sure someone better at regex than I am can tell you exactly what you want though.

0 Likes

#3

Likewise, in the regex approach, the following will get you to a similar place:

\.navbar.*\{(.|\n)*font

ie. selects the text beginning .navbar and ending in font. Escape (optionally followed by End) gets you editing in the right place.

The alternative is a compound of two searches; search “.navbar” then search “font-size”.

I really wish Sublime’s macros would work with everything (including S&R for example) instead of just text edits/cursor movements. It’s one of Sublime’s biggest editing limitations presently imo. In my old editor I had a whole bunch of such macros saved which I could run from a “command” mode or bind to a keystroke. In the absence of this if you want this kind of thing often, it’s best to write a simple plugin.

:smile:

0 Likes

#4

Adding to my earlier answer a little:

(?<=navbar\s.)(?<!\})(\s.*\s)*\s(font-size)

This ensures that a navbar followed by a font-size in another set of brackets further down won’t trigger it.

0 Likes