Sublime Forum

Command to remove all white space?

#1

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.

1 Like

#2

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.

1 Like

#3

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.

0 Likes

#4

You can try the RemoveWhitespace plugin

0 Likes

#5

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

0 Likes

#6

[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

0 Likes

#7

That works perfectly. Thanks EJ12N

0 Likes

#8

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:

0 Likes

#9

Try Ctrl+J (join lines)

1 Like

#10

I tried that, but that has not affect on the extra spaces that are present once you remove the new lines.

0 Likes

#11

Find what: \s*(<>])\s*|\s*\n\s+ Replace with: \1 (or) Replace with: $1This 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.

0 Likes

#12

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?

0 Likes

#13

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.

0 Likes

#14

[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.

0 Likes

#15

Also check the following Sublime setting:

"trim_trailing_white_space_on_save": true
0 Likes

#16

find:
(>)\n?\s*(\w\s]*)\s(<)
replace with:
\1\2\3

becomes:

hopefully it’s what you want.

0 Likes