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.
Asked
Active
Viewed 206 times
1
-
How am I supposed to copy/paste text from an image? – melpomene May 18 '19 at 01:07
-
It does not mean to be copied. I thought that I should paste a lot of text to show the idea, then I realized that image would be more representative. Maybe I should have posted only a few lines separated with blank lines... My bad. – Evgeniy May 18 '19 at 02:11
1 Answers
2
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
.

melpomene
- 84,125
- 8
- 85
- 148
-
This works, thank you. I thought this is impossible to do automatically, because I have tried to return different values (1, 2, ...). – Evgeniy May 18 '19 at 02:17