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:
zE (delete all folds.)
:1,309fold (fold 1-309, first initial fold you described.)
:617,$fold (fold 617-eof, second initial fold you described.)
:1,400fold (a fold starting at the top and including line 377.)
zo (open this last fold!)
377G (go to line 377.)
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
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
The command that will close all folds is actually