How can I set foldlevelstart in vim to just fold nothing initially, still allowing zr to work immediately?

8

3

In vim, I typically set foldmethod to indent for most file types. In general, I like files to open with all folds open. However, it is useful to start immediately using zm to start closing folds globally across the file, one level at a time, allowing me to see the overall structure of the file without details getting in the way.

The only general solution I have found is to set foldlevelstart to a very high number, e.g. 99, to make sure that all files (bearing in mind different files will have different levels of maximum indent) start completely unfolded. However, I'd then have to use zm repeatedly to reduce foldlevel down to the maximum fold level of the file, normally much less than 99, which is cumbersome and impractical. I could set foldlevel manually using the vim command line, but I'd still need to know the maximum indent in the file.

Is there a practical way to open a file with foldlevel set to exactly one more than the maximum current indent/fold level in the file itself?

For example, given the following file:

a
  b
    c
      d1
      d2
    e
      f1
      f2

The first keypress of zm would show this:

a
  b
    c
+--  2 lines: d1------
    e
+--  2 lines: f1------

(Note, however, that it should work for the general case where the initial maximum indent of the file could be any value).

Andrew Ferrier

Posted 12 years ago

Reputation: 1 604

1You're confusing zr with zm; the latter reduces the foldlevel. – Ingo Karkat – 12 years ago

@IngoKarkat, you're right, I am. Sorry for the confusion - I always find those key combos counter-intuitive. I've fixed my question. – Andrew Ferrier – 12 years ago

"I always find those key combos counter-intuitive." It's supposed to mean zm = fold more (subtract foldlevel) and zr = fold less (add foldlevel) – wisbucky – 11 years ago

Answers

9

This finds the highest foldlevel in the current file:

:let &foldlevel = max(map(range(1, line('$')), 'foldlevel(v:val)'))

To set this automatically, you can use an :autocmd:

:autocmd BufWinEnter * let &foldlevel = max(map(range(1, line('$')), 'foldlevel(v:val)'))

Ingo Karkat

Posted 12 years ago

Reputation: 19 513

This doesn't seem to work on Cygwin Vim. My foldlevelis 0 and foldlevelstart is -1 when I launch a file with those settings. – CMCDragonkai – 9 years ago

1Ingo, perfect, that's exactly what I was looking for. Thanks. Shame foldlevelstart doesn't have a option like this. – Andrew Ferrier – 12 years ago

0

The option set nofoldenable lets you start off the file with all folds open

HarshaNadimpalli

Posted 12 years ago

Reputation: 109

That's not the question I asked. I want folding enabled. – Andrew Ferrier – 7 years ago

0

Instead of using zr, use zR, which will open all folds. Similarly, zM will close all folds.

garyjohn

Posted 12 years ago

Reputation: 29 085

Gary, thanks. That's close, but doesn't do what I describe. I'd like a command that reduces the folding level just by one, but from the maximum fold level relevant to the file in question. To do that, I'd need to use zM to reduce the foldlevel to 0, then zr repeatedly to open up more of the detail. I'm looking for one command/key combination. – Andrew Ferrier – 12 years ago

2You can do zR (set foldlevel to highest level) then zm will work. It's just one extra step. – wisbucky – 11 years ago