http://www.perlmonks.org?node_id=657871


in reply to .vimrc for perl programmers

I know that this is an old node. And that I have already commented on it. However, I wanted to add some updates.

I had a regex mapped to comment out lines or blocks of code and another to uncomment them. I always hated that this killed my "previous search expression" so that I could not go through a file looking for a pattern, comment out the line, and go to the next occurrence without re-issuing the search command. I always thought "There must be a better way. After OSCON 2007, I found that better way:

function! Comment() let l:line = "#".getline(".") call setline(".",l:line) endfunction map ## :call Comment()<cr> function! UnComment() let l:line = getline(".") let l:pos = stridx(l:line,"#") if l:pos > -1 let l:line = strpart(l:line,0,l:pos).strpart(l:line,l:pos+1) endif call setline(".",l:line) endfunction map !# :call UnComment()<cr>

I also got tired of the fancy status line function not working inside closures because sub was indented. So, I did some poking at the code (that a coworker had put together) and made some minor fixes:

if has( "folding" ) set statusline=%f%{CurrSubName()}\ %m%h%r\ %=%25(%-17(%l\,%c%V%)\ %p +%%%) set laststatus=2 set maxfuncdepth=1000 endif function! CurrSubName() let g:subname = "" let g:subrecurssion = 0 " See if this is a Perl file let l:firstline = getline(1) if ( l:firstline =~ '#!.*perl' || l:firstline =~ '^package ' ) call GetSubName(line(".")) endif return g:subname endfunction function! GetSubName(line) let l:str = getline(a:line) if l:str =~ '^\s*sub\>' let l:str = substitute( l:str, ' *{.*', '', '' ) let l:str = substitute( l:str, '^\s*sub *', ': ', '' ) let g:subname = l:str return 1 elseif ( l:str =~ '^}' || l:str =~ '^} *#' ) && g:subrecurssion >= 1 return -1 elseif a:line > 2 let g:subrecurssion = g:subrecurssion + 1 if g:subrecurssion < 190 call GetSubName(a:line - 1) else let g:subname = ': <too deep>' return -1 endif else return -1 endif endfunction

Share and enjoy.

Ivan Heffner
Sr. Software Engineer
WhitePages.com, Inc.

Replies are listed 'Best First'.
Re^2: .vimrc for perl programmers
by john_oshea (Priest) on Dec 19, 2007 at 14:55 UTC

    Possibly overkill for your needs, but I have to say that since I've found the NERDCommenter plugin I've not bothered with anything else for commenting. Does comment / uncomment / toggle comment / add end-of-line comment etc, and does it for lots of different filetypes as well. Worth a look if you have time/inclination.