Why does creating a new fold sometimes cause the entire file to be folded?

86 Views Asked by At

Sometimes when I create a new fold in vim, it folds the entire file. Why?
(foldmethod=manual)

e.g.,

  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

Workaround:

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

My fold* settings:

  foldclose=
  foldcolumn=0
  foldenable
  foldexpr=0
  foldignore=#
  foldlevel=0
  foldlevelstart=-1
  foldmarker={{{,}}}
  foldmethod=manual
  foldminlines=1
  foldnestmax=20
  foldopen=block,hor,mark,percent,quickfix,tag,undo
  foldtext=foldtext()
1

There are 1 best solutions below

0
On BEST ANSWER

This is because line 377 (where I was creating a new fold) was within an existing open fold that started at line 1 of the file. So when I created the new fold, it increased the size of the existing open fold and closed it, causing the entire file to be folded closed.

To debug this, :set foldcolumn=12 shows open folds. And zM closes all open folds.

Note also that I thought zi toggled open/closed all folds, but that's not true. zM closes all folds, and zR opens all folds.

[I got the answer from this vi.stackexchange.com question]