Sometimes when I create a new fold in vim, it folds the entire file. Why?


  1. zi (all folds are now open)
  2. zi (all folds are now closed)
  3. lines 1-309 are folded closed, and lines 617-662 are folded closed (file has 662 lines)
  4. my cursor is on line 377
  5. I do zfG to fold all lines from line 377 to end-of-file
    Expected: lines 377 thru end-of-file are closed, and lines 310-366 are visible
    Actual: all lines in file are closed


  1. zD (delete all folds)
  2. :1,309fold (fold 1-309)
  3. :377,$fold (fold 377-eof)

My fold* settings:

  • 1
    Have you tried vim --clean? Also see How to debug my vimrc
    – D. Ben Knoble
    Aug 23, 2020 at 20:56
  • I believe you have some opened manual folds in the middle of the file that you're not aware of... So when you're creating a fold (that spans some of those), Vim ends up closing them and that's what you're experiencing. Try this to see the folds visually: :%s/^/\=foldlevel('.')."\t"/ (Note that this will modify your buffer, but it should be easy to revert it with another :%s command, or to update the per-line fold level after you execute a fold operation...
    – filbranden
    Aug 24, 2020 at 3:55

1 Answer 1


This behavior will happen if line 377 is part of a fold that starts at the top of the file, and that fold that is currently open.

In that case, the new fold Vim will create will actually start at line 1 (not at line 377) and Vim will close the new fold, effectively folding the whole file.

You can reproduce this behavior with:

  1. zE (delete all folds.)
  2. :1,309fold (fold 1-309, first initial fold you described.)
  3. :617,$fold (fold 617-eof, second initial fold you described.)
  4. :1,400fold (a fold starting at the top and including line 377.)
  5. zo (open this last fold!)
  6. 377G (go to line 377.)
  7. zfG (fold to the end, but this actually folds 1-eof!)

It's possible you're not opening that fold explicitly, but through one of the actions in 'foldopen', such as following a tag or going to a mark. Even a horizontal movement (such as 377G0) will trigger that fold to open.

If you'd like to see the folds visually, you can use this command to add the fold level to the beginning of each line:


(Note that this will modify your buffer, but it should be easy to revert it with another :%s command. This will also get out-of-sync as you create or delete folds, you should also be able to update the per-line fold level after such operation with a similar :%s command.)

You also mentioned that the zi command would close all folds (when enabling fold), but that's not really accurate... Vim will keep track of the folds manually closed and opened and preserve those choices when you toggle 'foldenable' with zi.

The command that will close all folds is actually zM.

  • Revert with u :)
    – D. Ben Knoble
    Aug 24, 2020 at 13:17
  • 1
    @D.BenKnoble Well, only if that was the last change... In my case, I kept this around for a while to keep looking at how it evolved. But still simple to drop with a substitute anyways. (I wish using the sign column for this was easier to do... Maybe worthy of a plug-in?)
    – filbranden
    Aug 24, 2020 at 13:28
  • 1
    Perfect, thanks for the detailed answer @filbranden ! This happened to me again, and zM did indeed indicate that the line I was folding was part of an open fold, which I was also able to see when opened with :set foldcolumn=12 Aug 29, 2020 at 21:39

Your Answer

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

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