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

后端 未结 3 1878
别那么骄傲
别那么骄傲 2021-02-10 15:35

Source:

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

vim magic here, probably something visual selection based

R

相关标签:
3条回答
  • 2021-02-10 15:38
    :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.

    0 讨论(0)
  • 2021-02-10 15:47

    Very quick answer:

    :s/[{;] /\0\r
    vi{
    :!sort
    va{
    J
    
    0 讨论(0)
  • 2021-02-10 16:01

    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())

    0 讨论(0)
提交回复
热议问题