3

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?

1 Answer 1

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

3
  • 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 gist.github.com/198ee62a82f74c5e6bf2
    – lkraav
    Commented Jul 8, 2012 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
    Commented Jul 8, 2012 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
    Commented Jan 17, 2013 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.