How can I autofold POD in Perl with vim?
Question
I'm trying to edit files with vim and get the POD automatically folded (just the POD, not the Perl). I can't get it to work. I can get folding itself to work because I can manually highlight the lines and type zF
and it folds properly.
Can anyone shed light on this?
Solution
You forgot
:set foldmethod=syntax
:setf perl
:syntax on
:set foldenable
:syn region POD start=/^=head[123]/ end=/^=cut/ fold
OTHER TIPS
Paste the following at the end of your ~/.vimrc file:
fu! MyFoldSettings()
set foldmethod=expr
set foldexpression=MyFoldLevel(v:lnum)
set foldenable
set foldminlines=1
endf
fu! MyFoldLevel(lev)
let mytext = getline(a:lev)
let result="="
if mytext =~ '^=item'
let result=">3"
elsei mytext =~ '^=back'
let result="<2"
elsei mytext =~ '^=over'
let result=">2"
elsei mytext =~ '^=cut'
let result="<1"
elsei mytext =~ '^=\w\+'
let result='>1'
en
return result
endf
augroup MyFoldSettings_AutoCommands
au!
au BufReadPost {*.pl,*.pod,*.pm} MyFoldSettings()
augroup END
This should work in most cases -- It's just a quick and dirty example, but you can add more too it if you need more functionality. Also, to make certain that this is enabled you might want to add the modeline:
# vim:fen:fdo=hor:fcl=all:fdm=expr:fde=MyFoldLevel(v:lnum):fml=1
If you dont see folded text at this point, try setting the 'foldopen=hor' and 'foldclose=all' which will make navigation into/out of folds easier. Also, if you need this to be applied to other files or filetyps, you can set up the au (autocommand) accordingly. Cheers.
You can put this in your .vimrc:
au FileType perl
\ setlocal foldexpr=getline(v:lnum)=~'^=cut'?'<1':getline(v:lnum)=~'^='?'1':'=' |
\ setlocal foldmethod=expr
Just define perl_fold_* variables, the system perl syntax highlighter will take care of the rest.
In .vimrc, just add
let perl_fold=1
let perl_fold_blocks=1
This is what I'm using,
set syntax
augroup perl_folding
au!
autocmd FileType perl setlocal foldmethod=syntax
autocmd FileType perl setlocal foldlevel=1
autocmd FileType perl let perl_fold = 1
autocmd FileType perl let perl_fold_blocks = 1
autocmd FileType perl setlocal foldenable
au BufRead,BufNewFile *.pm set filetype=perl
augroup END