1

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.

2
  • How am I supposed to copy/paste text from an image? Commented May 18, 2019 at 1: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. Commented May 18, 2019 at 2:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This works, thank you. I thought this is impossible to do automatically, because I have tried to return different values (1, 2, ...).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.