Search code examples
cssvim

Vim: reformat CSS from one-line to multi-line


I have some css code in this format:

a { color: #333; background-color: #fff; } 
a:visited { color: #aaa; background-color: #555; } 

I want to get it in this format:

a { 
    color: #333; 
    background-color: #fff; 
} 
a:visited {
    color: #aaa; 
    background-color: #555; 
} 

Is there an easy way to do that? I know I can write a macro to do that, but I was hoping there was a better/easier solution. Ideally, I'd like to be able to select the lines and do something like gq.


Solution

  • if the filetype has already been set as CSS, you can try:

    :%s/[{;}]/&\r/g|norm! =gg
    

    at least it works for your example:

    enter image description here