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'


            return foldlevel(a:line - 1)



Read free Software Algorithms and Computer Science Books by CodeAhoy Learn.

Introduction to Recursion and Backtracking

Sorting Algorithms

Introduction to C Programming Language

And More...

Speak Your Mind