How to make vim sort alphabetically CSS rules within one line?

A source:

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

vim magic here, maybe something based on visual selection

Result:

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

What do you suggest for the magic part of vim?

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

Divide the string by { or ; to get each rule on its own line :sort them (omitting the first line containing the CSS definition), then concatenate them together.

+6
source

Very quick answer:

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

Another one line:

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

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

+1
source

Source: https://habr.com/ru/post/1413535/


All Articles