Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
1  
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 at 21:44
add comment

2 Answers

up vote 4 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
add comment

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 at 21:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.