Using vi, how can I make CSS rules into one liners?

后端 未结 7 911
眼角桃花
眼角桃花 2020-12-05 05:55

Example bad:

#main {
      padding:0;
      margin: 10px auto;
}

Example good:

#main {padding:0;margin:10px auto;}
<         


        
相关标签:
7条回答
  • 2020-12-05 06:56

    If you want to change the file, go for rampion's solution.

    If you don't want (or can't) change the file, you can play with a custom folding as it permits to choose what and how to display the folded text. For instance:

    " {rtp}/fold/css-fold.vim
    " [-- local settings --]               {{{1
    setlocal foldexpr=CssFold(v:lnum)
    setlocal foldtext=CssFoldText()
    
    let b:width1 = 20
    let b:width2 = 15
    
    nnoremap <buffer> + :let b:width2+=1<cr><c-l>
    nnoremap <buffer> - :let b:width2-=1<cr><c-l>
    
    " [-- global definitions --]           {{{1
    if exists('*CssFold')
      setlocal foldmethod=expr
      " finish
    endif
    
    function! CssFold(lnum)
      let cline = getline(a:lnum)
      if     cline =~ '{\s*$'
          return 'a1'
      elseif cline =~ '}\s*$'
          return 's1'
      else
          return '='
      endif
    endfunction
    
    function! s:Complete(txt, width)
      let length = strlen(a:txt)
      if length > a:width
          return a:txt
      endif
      return a:txt . repeat(' ', a:width - length)
    endfunction
    
    function! CssFoldText()
      let lnum = v:foldstart
      let txt = s:Complete(getline(lnum), b:width1)
      let lnum += 1
      while lnum < v:foldend
          let add = s:Complete(substitute(getline(lnum), '^\s*\(\S\+\)\s*:\s*\(.\{-}\)\s*;\s*$', '\1: \2;', ''), b:width2)
          if add !~ '^\s*$'
              let txt .= ' ' . add
          endif
    
          let lnum += 1
      endwhile
      return txt. '}'
    endfunction
    

    I leave the sorting of the fields as exercise. Hint: get all the lines between v:foldstart+1 and v:voldend in a List, sort the list, build the string, and that's all.

    0 讨论(0)
提交回复
热议问题