5

I want to fold sections in a document. Each new section should define a top-level fold instead of a nested fold. I want to use a single delimiter to define a fold. Consider an example document:

>>> first fold
also in first fold
>>> second fold
still in second fold

I want to fold like this:

+----2 lines: first fold
+----2 lines: second fold

Using foldnestmax=0 does not affect foldmethod=marker, it only works on indent and syntax fold methods. Using foldmarker=>>>,>>> does not work, it creates nested folds. The only option I have found is to use fold numbers, e.g. >>>1 first fold, but I'd like to avoid that. Is that possible?

John Freeman
  • 227
  • 1
  • 6

1 Answers1

6

You can do that with a fold expression. Cp. :help fold-expr. When the current line starts with your fold marker >>>, start a new fold (of level 1); else, continue with the same level as before:

setlocal foldmethod=expr foldexpr=getline(v:lnum)=~#'^>>>'?'>1':'='
Ingo Karkat
  • 17,819
  • 1
  • 45
  • 61
  • I removed the hash (not sure what it's for), and I want to match anywhere in the line, so I removed the caret, and I had to escape special characters to use it in my modeline, but it works: " vim:set foldmethod=expr foldexpr=getline(v\:lnum)=~\'>>>\'?\'>1\'\:\'=\' foldlevel=0 foldclose= : – John Freeman Mar 03 '15 at 15:32
  • 1
    Great! The =~# is for case-sensitive matching; it doesn't matter here (but I like to be explicit). You could define a :function in your vimrc and reference that in the modeline; would save you from the escaping (and repetition). – Ingo Karkat Mar 03 '15 at 15:38