Search code examples
perlvimfolding

Vim syntax folding with perl


I use vim at work for developing pretty sizable perl code, and I got stuck while trying to learn how to use folds properly.

(Note: I'm still relatively new to Vim - switched over from gedit about half a year ago, but I'm still learning the new powerful things everyday!)

This is my portion of .vimrc related to folding

" mouse is on
set mouse=a
" folding
set foldmethod=syntax
set foldlevelstart=1
let perl_fold=1
let sh_fold_enabled=1
let perl_extended_vars=1
let perl_sync_dist=250

So this thing is good in that it folds all the code, and I can freely open / close folds with simply pressing z-a. But it's also bad in that everything is folded when I open a file.

Is there a way to have the file not be folded when I open the file, but still allow me to open/close blocks of code based on perl syntax with z-a? (or some other keystroke)


Solution

  • The "fold level" is the depth of the fold:

    1
      2
        3
    

    The line that you put in your ~/.vimrc,

    set foldlevelstart=1
    

    tells vim fo close every fold up to level 1 by default.

    Set it to an impossibly high value to open all folds by default:

    set foldlevelstart=999
    

    Note that you can also try a low level like 2 or 3 which may have interesting results depending on your coding style.

    See :h foldlevelstart.