Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


The first keypress of zm would show this:

+--  2 lines: d1------
+--  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).

share|improve this question
You're confusing zr with zm; the latter reduces the foldlevel. – Ingo Karkat Mar 17 '13 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. – Andrew Ferrier Mar 17 '13 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 Jun 3 '14 at 21:44
up vote 5 down vote accepted

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)'))
share|improve this answer
Ingo, perfect, that's exactly what I was looking for. Thanks. Shame foldlevelstart doesn't have a option like this. – Andrew Ferrier Mar 17 '13 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. – CMCDragonkai May 15 at 12:18

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

share|improve this answer
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 Mar 17 '13 at 18:00
You can do zR (set foldlevel to highest level) then zm will work. It's just one extra step. – wisbucky Jun 3 '14 at 21:48

You must log in to answer this question.

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