Vim Tips Wiki
Advertisement
Tip 419 Printable Monobook Previous Next

created 2003 · complexity intermediate · author Mina Naguib · version 7.0


The $VIMRUNTIME/syntax/perl.vim file provided with Vim has options that allow folding in Perl programs. To enable folding in files with "perl" filetype, put the following line in your vimrc (see :help perl.vim for more details):

let perl_fold = 1

The Vim 7.2 perl.vim has some bugs. A patch is available.

Alternative

It is better to use the built-in procedure outlined above, but the following old information may be helpful for some users.

Add this to your vimrc to automatically fold Perl functions. You may also like to modify the script for similar languages that define a subroutine with "sub ...".

function GetPerlFold()
  if getline(v:lnum) =~ '^\s*sub\s'
    return ">1"
  elseif getline(v:lnum) =~ '\}\s*$'
    let my_perlnum = v:lnum
    let my_perlmax = line("$")
    while (1)
      let my_perlnum = my_perlnum + 1
      if my_perlnum > my_perlmax
        return "<1"
      endif
      let my_perldata = getline(my_perlnum)
      if my_perldata =~ '^\s*\(\#.*\)\?$'
        " do nothing
      elseif my_perldata =~ '^\s*sub\s'
        return "<1"
      else
        return "="
      endif
    endwhile
  else
    return "="
  endif
endfunction
setlocal foldexpr=GetPerlFold()
setlocal foldmethod=expr

See also

Comments

When let perl_fold = 1 is used, the fold method (:set fdm?) will be "syntax", and you will not be able to use manual or marker folds. If wanted, you can change the fold method, for example, :set foldmethod=manual.


Advertisement