Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

css

vim

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.

like image 371
Jonathan Avatar asked Apr 04 '14 14:04

Jonathan


2 Answers

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

like image 113
Kent Avatar answered Nov 11 '22 09:11

Kent


You can use cssbeautify:

:%! css-beautify --file -
like image 38
Ingo Karkat Avatar answered Nov 11 '22 09:11

Ingo Karkat