Skip to content

Instantly share code, notes, and snippets.

@ahawthorne
Created March 14, 2012 23:13
Show Gist options
  • Save ahawthorne/2040294 to your computer and use it in GitHub Desktop.
Save ahawthorne/2040294 to your computer and use it in GitHub Desktop.
Format horrible css in to readable css
" Add the map below to .vimrc as one long line.
" Restart vim, open horribly formatted css, press F9
" Then gg=G to fix formatting
" Win.
map <F9> :%s/\n\+\s*//g<CR>:%s/:[ ]*/:/g<CR>:%s/}/}\r\r/g<CR>:%s/;/;\r\t/g<CR>:%s/[ ]*{/ {\r\t/g<CR>:%s/\(\t\)\?\/\*\(.*\)\*\//\1\/\*\r\1\2\r\1\*\/\r\1/ge<CR>:%s/\(\t\)\?\/\*/\1\/\*\r\1/ge<CR>:%s/\(\n\+\)\?\(\t\)\?\*\//\r\2\*\/\r\2\1\2/ge<CR>:%s/^\t}/}/g<CR>:%s/\t\([^:]\+\):/\t\1: /g<CR>:%s/[ ]*!important/ !important/ge<CR>:%s/\t\n//ge<CR>:%s/\n\{3,\}/\r\r/ge<CR>:%s/\(\n^$\)\{2,\}//ge<CR>:let err=0<CR>:while err == 0<CR>try<CR> %s/^\([^,\t]\+\),\([^$]\)/\1,\r\2/g<CR>catch /^Vim\%((\a\+)\)\=:E/<CR>endtry<CR>:endwhile<CR>gg=G<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment