How do I extend the Vim folding support?

You can use the ‘foldexpr’ option to fold using a user specified function. For example, to fold subroutines of the following form into a single line:

    sub foo {

      my $barf;

      $barf = 3;

      return $barf;

    }

You can use the following commands:

    set foldmethod=expr

    set foldexpr=MyFoldExpr(v:lnum)

    fun! MyFoldExpr(line)

        let str = getline(a:line)

        if str =~ '^sub\>'

            return '1'

        elseif str =~ '^}'

            return '<1'

        else

            return foldlevel(a:line - 1)

        endif

    endfun

Comments (1)


Pepsh Pepshinsky

Let’s see you to :help viminfo-file-name (option -i) :set viminfofile=NONE


Speak Your Mind