About this site's lack of design: Yes, it's supposed to look this way — I'm helping create a new sandbox theme for WordPress (see it on GitHub).

Dan Rubin's SuperfluousBanter

Suffering from chronic idiocy since 1977


Regex Patterns for Single Line CSS

Update: You can now down­load the Text­mate macro file rather than record­ing your own (skip to the down­load »).

There has been plenty of dis­cus­sion about the pros and cons of single-line style sheets, and I’ve been includ­ing them as an option when teach­ing CSS man­age­ment and orga­ni­za­tion in my Web Design World pre­sen­ta­tions in Chicago, Seat­tle, and later this year in Las Vegas (at Web­Builder) and Boston.

It’s a mat­ter of preference

As a fel­low Side­bar–ian (Side­bar­bar­ian?) Steve has been try­ing to con­vince me to use the single-line approach for a while of course, and Bryan and Jon have also become fans of this for­mat­ting style for their own work. Although they are enam­ored with it, I haven’t taken to it yet, still pre­fer­ring to write my style sheets using the “nor­mal” indented for­mat­ting most of us are used to.

Now, before any­one gets their knick­ers in a twist about why they love, hate, “can’t live with­out” or “will die before they try” single-line for­mat­ting, let’s just take a step back and remem­ber one thing: it isn’t any­thing spe­cial, just an alter­nate for­mat­ting style that doesn’t affect the way the browser inter­prets the style sheet one iota. It’s a per­sonal preference—remember that before jump­ing on or off this par­tic­u­lar bandwagon.

Always keep your options open

Now that I’ve got that out of my sys­tem, let’s talk prac­ti­cal­ity: there are indeed ben­e­fits to be had when using single-line CSS formatting—for exam­ple, I find it can make a dif­fer­ence after a project has been com­pleted, at which point I’m usu­ally more inter­ested in visu­ally scan­ning a style sheet for the selec­tors first, and then for a par­tic­u­lar prop­erty I’m inter­ested in edit­ing. This is where I’ve found single-line for­mat­ting can come in handy.

But my edi­tor already does that!

This is the point where some peo­ple will start going on about how you could just use Textmate’s “fold­ings” fea­ture to get the same visual result (with­out alter­ing your file), or how CSS Edit has a handy list of all the selec­tors in a col­umn on the left side of its win­dow, or that you could always use your editor’s “find” com­mand and search for the selec­tor you want to edit. Yet while those are all per­fectly log­i­cal, sane sug­ges­tions, they don’t account for flex­i­bil­ity and choice.

Just another way of doing things

Much like Jon Hicks with his harem of web browsers, I tend to be a bit of a “text edi­tor polyg­a­mist”, bounc­ing from Text­mate to CSS Edit to Coda to BBE­dit to Transmit’s text edi­tor and a host of CLI edi­tors, mostly depend­ing on my mood (though some­times con­tex­tual if I’m at a com­puter that doesn’t have a par­tic­u­lar application—a Win­dowsXP box with noth­ing but Notepad, for instance). It’s the times when I’m using an edi­tor that doesn’t have “fold­ings” or pretty columns of selec­tors that I start to appre­ci­ate single-line CSS when mak­ing quick edits, so I’ve started con­vert­ing style sheets to a “sim­ple” single-line for­mat (with­out the left-aligned tab blocks to start each rule’s prop­er­ties) once they are ready to go live.

Pat­terns fit for a kilt

Edi­tors like Text­mate and BBE­dit have built-in com­mands for for­mat­ting (the stan­dard, multi-line approach) or com­press­ing (the entire style sheet on one line, osten­si­bly to reduce file size by strip­ping white space) CSS, but no way to con­vert to single-line for­mat­ting and Textmate’s “For­mat CSS Com­pressed” bun­dle can for­mat your stylesheet to a sin­gle line per-rule, though it’s all squished together, mak­ing it dif­fi­cult to scan due to a lack of white­space. Con­vert­ing a style sheet by hand every time would be much too time-consuming to bother with, but that’s where reg­u­lar expres­sions come to the rescue.

In Text­mate, you can record a macro using each of these regex pat­terns as a sep­a­rate step (I’m sure other edi­tors have a sim­i­lar fea­ture, so please feel free to post details in the com­ments below). This allowed me to cre­ate a “For­mat CSS Single-line” com­mand, com­plete with a key­board short­cut, which enables an easy switch between multi– and single-line formatting.

Unfor­tu­nately, as of this writ­ing Text­mate macros can­not be exported and shared For those using Text­mate, get­ting the macro is a sim­ple mat­ter of down­load­ing, expand­ing and double-clicking this file:

While sim­i­lar to “For­mat CSS Com­pressed”, this macro retains a sin­gle blank line where your orig­i­nal con­tained two or more blank lines (help­ful if you group your rules), and adds white­space that matches my stan­dard for­mat­ting pref­er­ences (which I find makes it eas­ier to scan quickly). To switch between each for­mat­ting style, just run each com­mand in turn (via the Bun­dles menu or the key­board shortcuts).

How­ever, that wouldn’t be much use to every­one who doesn’t use Text­mate, so here are the respec­tive groups of regex I used for the conversion:

[ \t]+
[ \t]*,[ \t]*

What’s miss­ing

In Text­mate and BBE­dit I can return to multi-line for­mat­ting with a sin­gle com­mand, but that might not be as sim­ple in other edi­tors. What I’d love to see is a pair of shell scripts that con­vert from multi– to single-line and back, and pos­si­bly a web-based proces­sor that does the same (paste your style sheet into a textarea, select your for­mat­ting, hit “process” and the script pro­duces the result). If any­one would like to take on those tasks, I’ll hap­pily update this post to link to the prod­ucts of your labor.

And in the end…

If you’ve never tried single-line for­mat­ting, this makes it easy to exper­i­ment with­out com­mit­ting your­self (and I do rec­om­mend giv­ing it a try—you may be sur­prised once you’ve worked with it a few times).

Ulti­mately, because I can return to multi-line with a sin­gle com­mand my pri­mary text edi­tor should I ever feel like it, automat­ing the switch from multi– to single-line is a con­ve­nient way to get the ben­e­fits of single-line for­mat­ting with­out back­ing myself or my clients into a for­mat­ting corner.

This item was posted by Dan Rubin on Friday, August 29th, 2008.


You can follow comments on this item via the RSS 2.0 feed.

Comments are closed, but you can trackback from your own site.

67 comments on “Regex Patterns for Single Line CSS

« Older Comments
« Older Comments