lkraav
lkraav

Reputation: 2827

How to make vim alphabetically sort CSS rules within a single line?

Source:

.foo { line-height: 150px; font-size: 24px; clear: both; }

vim magic here, probably something visual selection based

Result:

.foo { clear: both; font-size: 24px; line-height: 150px; }

What do you suggest for the vim magic part?

Upvotes: 4

Views: 681

Answers (3)

Luc Hermitte
Luc Hermitte

Reputation: 32956

Another one-liner:

s/{\s*\zs.\{-}\ze\s*}/\=join(sort(split(submatch(0), '\s*;\s*')), '; ').';'

This time we use sub-replace-\=, and list manipulation functions (sort(), split(), and join())

Upvotes: 1

romainl
romainl

Reputation: 196576

Very quick answer:

:s/[{;] /\0\r
vi{
:!sort
va{
J

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172600

:s/\([{;]\)\s*/\1\r/g | '[+1,']sort | '[,']join

Split the line on { or ; to get each rule into a separate line, :sort them (omitting the first line containing the CSS definition), then join them back together.

Upvotes: 6

Related Questions