Is there a function to remove all white space/compress my code? I seem to be missing this feature and figure I am just overlooking it somewhere.
Command to remove all white space?
It may not satisfy your needs, but to remove all white space you could press CTRL+H to bring up the Find & Replace panel, then make sure that regular expression button is toggled (it’s labelled .*), then have the following in the input fields:
Find what: **\s**
Replace with:
That is, an empty input replacing it. It performs a bit slowly on my machine, mind you.
Perfect! Thanks so much. Wish I could just make that a button, but this serves the purpose. It’s a little slow on the current file, but its 1400 lines, which won’t be the usual.
is there anyway to control this removal process so that markup such as the following:
becomes:
instead of:
- SubVertItem1
- SubVertItem2
- SubVertItem3
[quote=“capnhud”]is there anyway to control this removal process so that markup such as the following:
becomes:
instead of:
- SubVertItem1
- SubVertItem2
- SubVertItem3
notice the difference in the [/quote]
remove new lines only \n
just one more question concerning removing white space. I can remove newlines which is good, but how would you trim the whitespace that is between subsequent text and tags.
ex:
becomes:
I tried that, but that has not affect on the extra spaces that are present once you remove the new lines.
Find what: \s*(<>])\s*|\s*\n\s+
Replace with: \1
(or) Replace with: $1
This removes all whitespace surrounding the < and > characters, as well as any line breaks and leading and trailing whitespace on lines, and as far as I understand it is exactly what you wanted.
Edit: actually, you should use \s*(<>])\s* as the pattern. It should work the same but if you happen to have a tag that spans multiple lines, it will not ruin it. For maximum effect, you can use two find&replaces: with one replace \s*(<>])\s* by $1, and with the other replace \s+ by a single space.
I understand what you are saying on the first part of this, but you kinda lost me when you say at the end of the edit
Are you indicate do a
Find what: \s(+space)
Replace with: $1
for maximum effect?
What I meant was that in the second search you should use ‘\s+’, without the quotation marks, as the pattern, and the replacement should be just a single space character that you use between words. That way every run of consecutive whitespace characters (spaces, line breaks, tabs and whatnot) will be merged into a single space, which is what Web browsers do anyway.
On the other hand, I have just realized that if you have something like<a href="#">two</a> <a href="#">examples</a>
then the first search (the \s*(<>])\s* one) will remove the space between the two anchor elements. I am afraid there is no easy way to avoid this, unless you are willing to write a long regular expression listing all block-level and inline-level HTML elements and treating them differently, and even then it would probably possible to trick it into leaving an unneeded space or deleting a needed one.
[quote]On the other hand, I have just realized that if you have something like
<a href="#">two</a> <a href="#">examples</a>
then the first search (the \s*(<>])\s* one) will remove the space between the two anchor elements. [/quote]
This solution does exactly what I was looking for.
Also check the following Sublime setting:
"trim_trailing_white_space_on_save": true
find:
(>)\n?\s*(\w\s]*)\s(<)
replace with:
\1\2\3
becomes:
hopefully it’s what you want.