avatar

Andres Jaimes

One-liner to minimize your CSS files

By Andres Jaimes

The following one-liner will help you remove comments, spaces, and more from your CSS files. Suggestions are always welcomed.

Important: Before running this command, create a copy of your original file. Try the resulting CSS in a test environment before releasing it.

cat style.css | sed -e '
s/^[ t]*//g;         # leading spaces
s/[ t]*$//g;         # trailing spaces
s/([:{;,]) /1/g;   # spaces after a colon, brace, semicolon, or comma
s/ {/{/g;             # spaces before a brace
/^$/d                 # blank lines
' | sed -e :a -e '$!N; s/n(.)/1/; ta # newlines
' | sed -e 's//*[^*]**///g;'         # comments