I used to work with location list (:lvimgrep) to show contents of the buffer, but recently I have discovered folding as an interesting alternative. I am using foldmethod=expr
and foldexpr=getline(v:lnum)=~'.'?1:0
options. All non-empty lines (a.k.a. paragraph) are folded and separated by empty lines. Here you can see the original file and folded. The advantages of this compared to location list is it shows amount of lines and there is no need for additional buffer. However, for readability it would be better to remove blank lines between folds and I do not know how to do it. It is possible with manual folding but if we include empty lines in foldexpr
, vim will merge all this paragraphs into one fold. How to separate them? Here is how it should look like.
You can make empty lines part of the preceding paragraph like this:
:set foldexpr=strlen(getline(v:lnum))==0?'=':strlen(getline(v:lnum-1))?1:'>1'
If the current line is empty, use the fold level from the previous line (=
).
Otherwise, check the preceding line: If it is empty, this must be the start of a new paragraph. Create a new level 1 fold with >1
. Otherwise this must be part of an existing paragraph; assign it fold level 1
.