In TextMate there is this awesome option to "Format CSS" and "Format CSS Compressed". This option doesn't seem to exist in vim. Perhaps it's in a plugin I'm not aware of? Or maybe I need to integrate CSS Tidy somehow?
Asked
Active
Viewed 3,702 times
1 Answers
14
You can integrate CSS Tidy fairly easily. Since you're coming from TextMate I'll assume you're on a Mac. If you don't already have CSS tidy installed, I'd recommend installing it via a package manager, like the excellent Homebrew.
Once it's installed, you can allow Vim to use CSS tidy for the =
command, which automatically formats code, by adding the following to your ~/.vimrc
:
autocmd filetype css setlocal equalprg=csstidy\ -\ --silent=true
Now whenever you have a CSS file open, it will consult CSS Tidy to format it when you invoke the =
command. Try it on an open CSS file with gg=G
to format the whole document.
Consult the CSS Tidy usage guide to add any other options you like, and don't forget to escape spaces you add to the above ~/.vimrc
command with \
.

Glorfindel
- 21,988
- 13
- 81
- 109

michaelmichael
- 13,755
- 7
- 54
- 60
-
Awesome. I noticed csstidy doesn't seem to cuddle opening curly brace in High readability template, but then it doesn't indent properties in Default template. So I corrected the default template a bit to indent properties, available at https://gist.github.com/198ee62a82f74c5e6bf2 – lkraav Jul 08 '12 at 22:11
-
Also noticing dos line feeds appearing from filtering through csstidy. So my autocmd is now `autocmd filetype css setlocal equalprg=csstidy\ -\ --silent=true\ --template=mydefault.tpl\ \|\ dos2unix` – lkraav Jul 08 '12 at 22:33
-
The problem with `equalprg=csstidy` is that it returns an empty result, if you call it with just the inner contents of a selector (since it is not valid CSS by itself). I would like to have a function, which calls csstidy and falls back to the internal indenting (equalprg=), if the result is empty. – blueyed Jan 17 '13 at 18:54