Wikia

Vim Tips Wiki

Auto-fold Perl subs

Talk0
1,612pages on
this wiki

Redirected from VimTip419

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.

AlternativesEdit

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: SimpleFoldEdit

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 workaroundEdit

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 alsoEdit

  • Folding presents an overview of how to use folding

ReferencesEdit

CommentsEdit

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).


Around Wikia's network

Random Wiki