Search code examples
vimfolding

Configure Vim to always open first fold only


Is it possible to configure Vim to always open first fold only in a given file (and left the rest of them closed)?

Preferably I would like to setup Vim to do this for specific files but a general setting would be enough.


Solution

  • If you mean the first level of folding, that's controlled by the 'foldlevel' option, and 'foldlevelstart' governs its initial value. So :set foldlevelstart=1 would do that.


    If you mean the first fold in a file, you can do that via a sequence of commands:

    • zM closes all folds
    • ggzj goes to the first fold in the buffer
    • zo / zO opens one / all levels under the cursor.

    To apply this automatically, use:

    :autocmd BufWinEnter * normal! zMggzjzo