Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
Last active August 29, 2015 13:58
Show Gist options
  • Save PeterRincker/9977119 to your computer and use it in GitHub Desktop.
Save PeterRincker/9977119 to your computer and use it in GitHub Desktop.
Fold multi-line CSS into one line CSS style. Use `>s` and `<s` to increase or decrease the width of the default selector field area. Save as ~/.vim/ftplugin/css_fold.vim
" Adapation form http://stackoverflow.com/a/1423394/438329
if exists('b:loaded_cssfold')
finish
endif
let b:loaded_cssfold = 1
let b:width = 25
nnoremap <silent> <buffer> >s :<c-u>let b:width+=v:count1<cr><c-l>
nnoremap <silent> <buffer> <s :<c-u>let b:width-=v:count1<cr><c-l>
if !exists('*s:CssFoldText')
function! s:CssFoldText()
let line = printf("% *s {", -1*b:width, substitute(getline(v:foldstart), "{\s*$", "", ""))
let nnum = nextnonblank(v:foldstart + 1)
let lst = []
while nnum <= v:foldend
let line = line . " " . substitute(getline(nnum), "^\s*", "", "")
let nnum += 1
endwhile
return line
endfunction
map <SID>xx <SID>xx
let s:sid = substitute(maparg("<SID>xx"),'xx$','', '')
unmap <SID>xx
endif
exe "setlocal foldtext=" . s:sid . "CssFoldText()"
setlocal foldmethod=marker
setlocal foldmarker={,}
if !exists('*s:ExpandCSS')
function! s:ExpandCSS() range
let search = @/
let gdefault = &gdefault
set nogdefault
exe a:firstline . "," . a:lastline . 'sm/{\_.\{-}}/\=substitute(submatch(0), "[{;]\\%(\\n\\)\\@!", "\\0\\r", "g")/'
exe "normal! '[V']="
let @/ = search
let &gdefault = gdefault
endfunction
endif
command! -buffer -range=% ExpandCSS <line1>,<line2>call s:ExpandCSS()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment