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

I have 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 this? I know I can write a macro for this, but I was hoping there is a better / simpler solution. Ideally, I would like to select the rows and do something like gq.

+5
source share
4 answers

if the file type is already set as CSS, you can try:

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

at least it works for your example:

enter image description here

+11
source

You can use cssbeautify :

:%! css-beautify --file -
+3
source

, , . vim, ( ), . (, PEP-8 python ..). , : http://www.vim.org/scripts/script.php?script_id=2981

TotalOpinion - (!) - !

0

. css , , - :

  1. , ,
  2. '<,'>s/[{;}]/&\r/g|norm! v'<= '<,'>s/[{;}]/&\r/g|norm! v'<=

after you have finished with the reformatted css block, select only one css block in visual mode and press J (capital letter j), then it will become a good one-line.

PS. Sorry for my bad english

0
source

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


All Articles