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?

1 Answer 1


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':'='
  • 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= : Mar 3, 2015 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). Mar 3, 2015 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.