.vimrc for perl programmers
nferraz
created: 2006-03-30 08:21:06
I started to write this .vimrc file because I wanted my programs to use 4-space (actual spaces!) tabs.

That was a simple need, but, after a quick research, I found many features that made programming more enjoyable in Vim.

Without more delay, here's my .vimrc file:

" use visual bell instead of beeping
set vb

" incremental search
set incsearch

" syntax highlighting
set bg=light
syntax on

" autoindent
autocmd FileType perl set autoindent|set smartindent

" 4 space tabs
autocmd FileType perl set tabstop=4|set shiftwidth=4|set expandtab|set softtabstop=4

" show matching brackets
autocmd FileType perl set showmatch

" show line numbers
autocmd FileType perl set number

" check perl code with :make
autocmd FileType perl set makeprg=perl\ -c\ %\ $*
autocmd FileType perl set errorformat=%f:%l:%m
autocmd FileType perl set autowrite

" dont use Q for Ex mode
map Q :q

" make tab in v mode ident code
vmap  >gv
vmap   I
nmap  ^i

" paste mode - this will avoid unexpected effects when you
" cut or copy some text from one window and paste it in Vim.
set pastetoggle=

Most of the features are self-evident when you open your next perl script. Other features deserve some comments:

I hope you'll find those tricks useful!

Re: .vimrc for perl programmers
created: 2006-03-30 09:19:29

A few more useful tips for using vim to write perl...

" my perl includes pod
let perl_include_pod = 1
" syntax color complex things like @{${"foo"}}
let perl_extended_vars = 1

We're not surrounded, we're in a target-rich environment!
Re: .vimrc for perl programmers
created: 2006-03-30 09:24:14
  • :imap dumper ^iwarn Data::Dumper->Dump([\llyw$a], ['pa']);
    let's you type '@myarraydumper' and you'll get 'warn Data::Dumper->Dump([\@myarray], ['myarray']);
  • map ß O#!/usr/local/bin/perliuse strict;iuse warnings;
  • (un)comment:
    map §1 :s/^/# /
    map §2 :s/^# //
    
Re^2: .vimrc for perl programmers
created: 2006-03-30 12:58:40

Youll get more accurate results on self referential structures by using

warn Data::Dumper->Dump(sub{\@_}->(\@myarray), ['myarray'])

it doesnt make that much difference when the example is an array, but when its a scalar it does make a lot of difference.

Alternatively install [cpan://Data::Dump::Streamer] and get prettier, easier to read and more accurate dumps outright.

Yes, this is a shameless plug. :-)

---
$world=~s/war/peace/g

Re^2: .vimrc for perl programmers
created: 2006-03-30 16:53:01

For commenting and uncommenting code (in perl and many other languages) have a look at the BlockComment plugin.

Re: .vimrc for perl programmers
created: 2006-03-30 09:25:16
Nice suggestions. Have you also tried some of these:

Folding:

let perl_fold=1
let perl_fold_blocks=1
Highlight POD correctly:
let perl_include_pod = 1
Insert Data::Dumper dumping statement (note that ^[ must be entered as a real escape, use ctrl-V):
imap   use Data::Dumper; warn Dumper(  );^[hhi
And one I got from this site, it comments out a block (again ^M is a real return):
map  :'a,.s/^/#/gi^M:nohl^M
To use this last one, make an 'a' mark (ma) at the top of the block. Move to the bottom of the block and hit F8.

Phil

Re^2: .vimrc for perl programmers
created: 2006-03-30 10:27:01
imap use Data::Dumper; warn Dumper( );^[hhi
after several years of using Dumper() like this i wrote a much more useful macro (see my other post).
forgetting Dumper() statements in code and finding the right ones to comment out can be really annoying. by using the Dump() method and variable names (Data::Dumper->Dump([\$test], ['test'])) you can debug much more sophisticated.
Re: .vimrc for perl programmers
created: 2006-03-30 09:55:28

See also these related nodes:

Re^2: .vimrc for perl programmers
created: 2006-03-30 12:00:45
Re: .vimrc for perl programmers
created: 2006-03-30 10:02:36

See Appendix C (Editor Configurations) in Perl Best Practices, which has vim, vile, emacs, BBEdit and TextWrangler configuration recommendations.

Much of the info in that appendix is from the thread Desparately seeking a bilingual vim/Emacs expert. The thread also has a link to the useful document vim for Perl developers

Re: .vimrc for perl programmers
created: 2006-03-30 10:16:04
I have a couple

Using folding:

When you hit F2 inside a code block it folds on that block, working on your {}'s
set foldmethod=marker
nmap  0v/{%zf

Code Skeletons:

When you start a new perl file have vim automaticaly give you #!/usr/local/perl -w and use strict; :-)
autocmd BufNewFile  *.pm        0r ~/.vim/skeleton.pm
autocmd BufNewFile  *.pl        0r ~/.vim/skeleton.pl


_____________________
Remember that amateurs built Noah's Ark. Professionals built the Titanic.
Re: .vimrc for perl programmers
created: 2006-03-30 11:20:47
Something to note - Mac users, particularly powerbook users, won't be able to use all the F-keys. I personally use comma-commands. So, for example, I use:
nmap ,c :!perl -wc %^M
nmap ,t :!prove -wlv t/*.t^M

nmap ,ac :'a,.s/^/#/gi^M:nohl^M
nmap ,auc :'a,.s/^#//gi^M:nohl^M
and so on. The cool thing is that you can have the comma-command use as many characters as possible and reuse commands. So, I have ",s" do one thing and ",sf" do another. If you don't hit the 'f' in a second, vim will execute ",s".

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re^2: .vimrc for perl programmers
created: 2006-03-30 17:07:35

Instead of just having those mappings for !perl and !prove, there are actual compiler plugins for both.

The main benefit of this is that vim will automatically parse any warnings and errors and jump to the correct location in the file. You can then jump to the next or previous error/warning with :cn and :cp or just list all the errors with :cl.

Re^2: .vimrc for perl programmers
created: 2006-04-04 04:56:54
Thanks for sharing those mappings :-) Maybe this is only applicable to Macs other than PBs, but I am able to map my F-keys. I had to type ^V and then the F-key to get it to map, but it works great. Part of my .vimrc now looks like this:
map ^[OQ :w!^M:! perl %^M
which just saves and runs the current file with perl. I'm using a MacMini with a USB keyboard, so I know it's probably not exactly the same. Have you tried pressing ^V before the F-key to map something, or am I just really fortunate?
Re: .vimrc for perl programmers
created: 2006-03-30 16:50:17

I can't live without perltidy:

" Tidy selected lines (or entire file) with _t:
nnoremap  _t :%!perltidy -q
vnoremap  _t :!perltidy -q

That lets you highlight some text and hit _t to run it through perltidy and pretty up your code. I often use it in conbination with some of the highlight commands:

  • tidy current block: shift-V%_t
  • tidy next 10 lines: shift-V10j_t
  • whole file: _t
Re: .vimrc for perl programmers
created: 2006-03-30 17:30:52

If you want to turn on 'paste' mode while already in 'insert' mode you need a couple of extra mappings. Here is what I have (note that I have mapped F7 and F8 instead of just F11 which you were using):

:map  :set paste
:map  :set nopaste
:imap  :set paste
:imap  
:set pastetoggle=

Now you can hit F8 at any time to switch to paste mode.

Re: .vimrc for perl programmers
created: 2006-03-31 03:39:28

See also: Vim as a Perl IDE. These two nodes complement each other nicely, thanks! :-)

HTH,

planetscape
Re: .vimrc for perl programmers
created: 2006-03-31 03:40:29
These are all great. Because of all of your autocmd lines, I'd suggest creating a function to be called:

function! Perl()
" autoindent
    set autoindent
    set smartindent

" 4 space tabs
    set tabstop=4
    set shiftwidth=4
    set expandtab
    set softtabstop=4

" show matching brackets
    set showmatch

" show line numbers
    set number

" check perl code with :make
    set makeprg=perl\ -c\ %\ $*
    set errorformat=%f:%l:%m
    set autowrite
endfunction

" Call Perl() when you open a Perl file
autocmd FileType perl call Perl()

Now, I have some things to add that I really like. I like cindent because I can control the indentation style with some rules. I can also add to or remove from the list of indentation keywords:

" restore defaults (incase I :e or :sp)
  set cinkeys&
" don't go to column zero when # is the first thing on the line
  set cinkeys-=0#
" restore defaults
  set cinwords&
" add Perl-ish keywords
  set cinwords+=elsif,foreach,sub,unless,until
  set cinoptions&
" I don't rememer these OTTOMH; :help cinoptions to learn more
  set cinoptions+=+2s,(1s,u0,m1
  set cindent

Then sometimes I want <Tab> to indent (insert a tab), sometimes I want it to do word autocompletion (like bash). So, I stole^H^H^H^H^Hborrowed this from a former coworker:

" SmartTab wrapper
function! SmartTab()
  let col = col('.') - 1
  if !col || getline('.')[col - 1] !~ '\k'
    return "\"
  else
    return "\"
  endif
endfunction

" turn on SmartTabs
inoremap  =SmartTab()

Then a current coworker found/modified/developed (not totally sure which) this nifty status bar which tells you what function (sub) you are in:

  set statusline=%f%{CurrSubName()}\ %m%h%r\ %=%25(%-17(%l\,%c%V%)\ %p%%%)
  set laststatus=2
  set maxfuncdepth=250

  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 =~ '^sub'
      let l:str = substitute( l:str, " *{.*", "", "" )
      let l:str = substitute( l:str, "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 = ': '
        return -1
      endif
    else
      return -1
    endif
  endfunction
Finally, everyone has their way to do compiler checks, block comment/uncomment, etc. Mine are:
  map !! :!perl -c %
  map ## :s/^/#/
  map !# :s/^#//

These are, of course, all part of my Perl() function. I have others for toggling list, paste, number, and wrap. And since I use split windows I map Ctrl+[arrow keys] to move between panes. This one is tricky because it really depends on terminal settings; I'm still working on some bugs with it.

Ivan Heffner
Sr. Software Engineer, DAS Lead
WhitePages.com, Inc.
Re: .vimrc for perl programmers
created: 2006-03-31 08:45:27
Nice node [nferraz], special thanks for "paste mode", I'v looked for that. Heres my 2c.:
" run srcipt from Vim pressing F5
map   :!perl ./%
Can't remember where did I get it, I guess its from article at [http://mamchenkov.net/wordpress/2004/05/10/vim-for-perl-developers|http://mamchenkov.net] but its down for now(I hope temporarily).
Re: .vimrc for perl programmers
created: 2006-04-01 22:23:30
A .vimrc is also available (along with other sample code) for TheDamian's Perl Best Practices here.

-- Burvil

perlmonks.org content © perlmonks.org and bowei_99, cees, Codon, demerphq, dragonchild, idle, jasonk, jhourcle, nferraz, philcrow, planetscape, rnahi, runrig, tinita, unobe, vagnerr

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03