9

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).

3
  • 1
    You're confusing zr with zm; the latter reduces the foldlevel. Commented Mar 17, 2013 at 19:51
  • @IngoKarkat, you're right, I am. Sorry for the confusion - I always find those key combos counter-intuitive. I've fixed my question. Commented Mar 17, 2013 at 21:39
  • "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
    Commented Jun 3, 2014 at 21:44

3 Answers 3

12

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)'))
2
  • 1
    Ingo, perfect, that's exactly what I was looking for. Thanks. Shame foldlevelstart doesn't have a option like this. Commented Mar 17, 2013 at 21:43
  • This doesn't seem to work on Cygwin Vim. My foldlevelis 0 and foldlevelstart is -1 when I launch a file with those settings. Commented May 15, 2016 at 12:18
0

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

2
  • 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. Commented Mar 17, 2013 at 18:00
  • 2
    You can do zR (set foldlevel to highest level) then zm will work. It's just one extra step.
    – wisbucky
    Commented Jun 3, 2014 at 21:48
-1

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

1
  • That's not the question I asked. I want folding enabled. Commented Jul 31, 2018 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .