English 中文(简体)
What s your favorite folding method (or secret techique) in Vim for HTML, Javascript and CSS? [closed]
原标题:
  • 时间:2010-01-31 09:29:44
  •  标签:
  • vim
  • fold

Closed. This question is opinion-based. It is not currently accepting answers.


Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 2 years ago.

I use something like this: 1,40 fo but I think is not the most efficient way.

What s yours?

最佳回答

I use foldmethod=marker and have mappings to enter <!-- {{{ --> and <!-- }}} --> where I want the fold to start and end. I put the start marker on the line with the opening block tag like:

<div id="topmenu"> <!-- {{{ -->

so when it s folded I immediately see what the fold contains without the need to add extra comment.

For CSS it s even easier, I just use foldmarker={,} and all definitions are automagically folded showing me just a very clear list of all classes, tags and ids which I can open just when I need them. Actually all my CSS files have this line at the very end:

/* vim: set fdm=marker fmr={,}: */

You can also visually select the region you want to fold and press zf if you prefer.

问题回答

I flip between indent and marker with this in my vimrc..

let g:FoldMethod = 0
map <leader>ff :call ToggleFold()<cr>
fun! ToggleFold()
    if g:FoldMethod == 0
        exe  set foldmethod=indent 
        let g:FoldMethod = 1
    else
        exe  set foldmethod=marker 
        let g:FoldMethod = 0
    endif
endfun

Indent works ok for most beautified html but I use marker for large declarative table of contents style folding of documents. Depending on who wrote the file, one will work better than the other so you need quick access to both.

Best folding method for vim for html: use haml instead. Best option for css: use sass instead.

I m actually serious. They make it much more compact.

I have used foldmethod=ignore almost exclusively. However, my desire to have ignored lines default to the higher fold level of the above or below lines, instead of the lower, inspired the following:

" Default foldmethod
" Similar to fdm=indent, but lets blank and comment lines default high.
set fen fdm=expr fdi=
set foldexpr=EswaldFoldLevel(v:lnum)

function! EswaldFoldLevel(lnum)
  let ignored =  ^s*([#/*]|$) 
  if getline(a:lnum) !~ ignored
    " In the general case, just use the indent level.
    " It would be nice if it didn t skip several levels...
    return indent(a:lnum) / &sw
  endif

  let previndent = 0
  let prevnum = a:lnum - 1
  while prevnum > 0
    if getline(prevnum) =~ ignored
      let prevnum = prevnum - 1
    else
      let previndent = indent(prevnum) / &sw
      break
    endif
  endwhile

  let nextindent = 0
  let maxline = line( $ )
  let nextnum = a:lnum + 1
  while nextnum <= maxline
    if getline(nextnum) =~ ignored
      let nextnum = nextnum + 1
    else
      let nextindent = indent(nextnum) / &sw
      break
    endif
  endwhile

  return max([previndent, nextindent])
endfunction

(Sorry about the syntax highlighting...)

I use that as a custom plugin, letting individual filetypes override it. Python, for example, doesn t want to look at previous lines, just following ones.





相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签