Vim Tips Wiki
Register
Advertisement
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Tip 419 Printable Monobook Previous Next

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


This tip is deprecated for the following reasons:

The patch mentioned below is outdated, does not apply cleanly to the Vim 7.3 runtime, and encounters problems with hash literals. Until this is fixed, if the syntax folding bugs bother you, you can use one of the alternate methods listed below the main tip.

The $VIMRUNTIME/syntax/perl.vim file provided with Vim has a configurable option that allows 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.

Alternatives

It is easier for most users to use the built-in procedure outlined above, but it is also possible to work around any issues you have with the built-in folding, using a plugin or directly writing and using a 'foldexpr'.

Plugin: SimpleFold

If you are able to install scripts, the SimpleFold script is very nice and supports Perl, among other languages.

Version 0.5 of this plugin does not support a lot of fold cases for Perl properly, however. Nevertheless, a simple patch can fix it.

About the only thing that doesn’t work is lines which both close and open a block, e.g.:

if (something) {
    stuff
} elsif (somethingelse) {                 #this doesn’t work
   otherstuff
}

This doesn't appear simple to fix, so you're better off just adding a line break and refolding (by default it calculates and sets folds with <leader>f ).

foldexpr workaround

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

  • Folding presents an overview of how to use folding

References

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.

In other words, if you want vim to autofold perl subroutines, use let perl_fold = 1 in your .vimrc. This should automatically set foldmethod=syntax when you load a perl file, but if not, you can manually set foldmethod=syntax or put this command in a file in $HOME/.vim/after/syntax called perl.vim (create this directory and file if it does not yet exist).


Advertisement