9

I use the folding option of vim quite a lot, and have usually set foldmethod to syntax. Recently I discovered that it is possible to add custom folds, such that I can put whole blocks in /*{{{*/ and /*}}}*/ which is very useful for grouping large sections of a source file together. However to use that feature I need to set foldmethod to marker, and I loose the syntax folding.

Is it possible to have two active foldmethods at the same time in vim? set foldmethod=syntax,marker does not work.

2 Answers 2

4

No, strictly speaking it is not possible to have multiple foldmethods active in Vim at the same time.

You can emulate marker based folding if you are willing to learn how to write syntax highlighting rules and hack the highlighting file for the language you want to add marker folding emulation to. Doing so has a couple of drawbacks:

  1. The commands to create/remove a fold ("zf", "zd" and so on) which work with marker and manual folding methods will not work when the foldmethod is set to "syntax".

  2. Emulating markers that "force" the fold level (e.g. "{{{1"/"}}}1") as opposed to increasing/decreasing the fold level by one (e.g. "{{{"/"}}}") is problematic; either you have to ignore the fold level numbers entirely, or take the chance that you will interfere with—and possibly break—the rest of the syntax based folding rules.

I suggest that when you find you need marker based folding, you use that method entirely. It can take some extra time and effort, but if you truly need the flexibility it may be worth it, especially considering the difficulties of trying to combine the two.

2

I was looking for the same thing. Since I don't want to hack the syntax file, instead I have a mapping to toggle between foldmethods. Right now I only switch between syntax and marker:

nmap <Leader>ff :call <SID>ToggleFold()<CR>
function! s:ToggleFold()
    if &foldmethod == 'marker'
        let &l:foldmethod = 'syntax'
    else
        let &l:foldmethod = 'marker'
    endif
    echo 'foldmethod is now ' . &l:foldmethod
endfunction

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .