How can I show a list of all folds?

e.g., maybe a list something like:


where <start-line>-<end-line> are the starting and ending lines of the fold.

Is there a command-line command that will do this? Or a script that could do it?

I know about :set foldcolumn=12 but I'd like a list rather than visually looking through the file.

  • Ugh. vimscript support for folds sucks. – D. Ben Knoble Oct 19 '20 at 20:57

I was able to accomplish something reasonable with the following code:

function CollectFolds() abort
  if !exists('s:folds')
    let s:folds = []
  const line = line('.')
  const end = foldclosedend(line)
  if !exists('s:prev_end') " first one
    let s:prev_end = end
    call add(s:folds, [line, end])
  elseif end isnot# s:prev_end " new fold
    call add(s:folds, [line, end])
    let s:prev_end = end

command PrintFolds execute 'folddoclosed call CollectFolds()' | echo s:folds | unlet s:folds s:prev_end

You can drop it in ~/.vim/plugin/printfolds.vim (though it should really have an if exists('g:loaded_printfolds') guard for that).

Change echo s:folds if you want to touch up the formatting.

The idea is to maintain a running state of the end of the current fold; when we start a new fold (the end changes), we add the fold start and end to the list of folds.

This approach only counts closed folds, so it skips nested folds and such. See previous edits for other versions. (If you know what foldmethod is, or use foldmarker or some other smarter technique, you might do better.)

It might be possible to get more robust by tweaking my fold-detection code from AutoOrigami. Heck, at that point, you migth be better off looping through the file and tracking previous foldlevel() and such. As filbranden points out below, this comes with its own detection problems; adjacent lines with the same fold level but that belong to different folds need to be distinguished, probably based on foldclosedend (which limits you to closed folds again; I mocked this up, and it almost works, but not quite).

  • "looping through the file and tracking previous foldlevel()" This was my first thought, but it fails if you have a line closing a fold level 2 followed by another that opens a new fold level 2. Both lines will be at the same fold level, but they're separate folds. – filbranden Oct 19 '20 at 20:21
  • Ah, good thought @filbranden – D. Ben Knoble Oct 19 '20 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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