Sublime Forum

Multi Select Broken if Indentation is different (doesn't ignore whitespace)

#1

Take the simplest of examples

<button>hello </button>
how are you?

	<button>hello </button>
	how are you?

highlight the second block (2 lines) hit alt F3.
Only the selected block is found because its not including whitespace or tabs / indentation.

Am I missing something?
In real life / html coding you will have a for example in multiple locations but it will not always sit in the same column of code.

0 Likes

#2

The command find_all_under (bound to Alt+F3) finds all occurrences of the current selection or word. The search is exact (except with respect to case sensitivity if you have that option turned on). When you select the second set of lines, the second line has all of that extra white space preceding it which the first one doesn’t have, which is why it doesn’t match (different text). Even differences with internal white space ("how are you" and "how are you") matter in this case.

That’s working by design and is how regular find operations usually work everywhere, not just in Sublime. When working with something like HTML that’s a bit of a drag, since HTML tends to disregard white space a lot of the time.

In order to perform the kind of search you’re trying to do, you could do a Regular Expression search from the find panel and use the Find All button to select all of the matches at once.

If you’re not familiar with RegEx it’s a pretty big topic, but essentially it’s sort of a mini programming language that allows you to search for text by providing some rules for what to match instead of the exact text to match alone.

For your example above:

  1. Open the find panel with Ctrl+F

  2. Make sure that Regular Expression is turned on; it’s the left most button in the panel, which in the default theme has the text .* on it.

  3. Optional: Turn on Highlight Matches if it is not already on; it’s the button that comes right before the text field. If this is turned on, you can see what your regex is matching as you enter it so you can tell if it’s going to work the way you want or not.

  4. Enter the following in the search box: <button>hello </button>\s*how are you\?

  5. Press the Find All button

The text \s is special and means “match a white space character”, where white space is a space, tab, newline, etc. The * is an instruction that means “match 0 or more of”. Together they match a variable amount of white space (including none). In your case that allows the second line to match. In fact this would also match if the two lines where joined together into one.

The ? character is special in a regex; where * means “match 0 or more of”, ? means “match 0 or 1 of”. Since we actually want to match a question mark, we need to “quote” it with a backslash to tell Sublime that it’s not a special question mark.

You need to do similar quoting for all special characters; if you wanted to search for an actual backslash you need to use \\, if you want to search for an actual asterisk you need to use \* and so on.

With all that said, trying to search around in an HTML document selecting text as well as the enclosing tags is a pretty tricky subject overall. For example if the space following “hello” was outside of the button tag instead of inside it,the above search wouldn’t match it.

If you wanted to find a tag including the text inside it, without worrying about white space, you can extend the idea above: <button>\s*hello\s*</button> to find all instances of a button tag with the text hello inside.

2 Likes

#3

AHHH!!!
Thank you so much.
After a while I figured this had to be by design.
But then it seemed really odd given that it wasn’t ignoring whitespace.

What are the possibilities that we could see a button in the search box
“exclude whitespace”

0 Likes