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

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

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?

回答1:

: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.



回答2:

Very quick answer:

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


回答3:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!