" 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 vmapMost of the features are self-evident when you open your next perl script. Other features deserve some comments:>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=
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! |
|---|
map §1 :s/^/# /map §2 :s/^# //
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. :-)
Folding:
let perl_fold=1 let perl_fold_blocks=1Highlight POD correctly:
let perl_include_pod = 1Insert Data::Dumper dumping statement (note that ^[ must be entered as a real escape, use ctrl-V):
imapAnd one I got from this site, it comments out a block (again ^M is a real return):use Data::Dumper; warn Dumper( );^[hhi
mapTo 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.:'a,.s/^/#/gi^M:nohl^M
Phil
imapafter several years of using Dumper() like this i wrote a much more useful macro (see my other post).use Data::Dumper; warn Dumper( );^[hhi
See also these related nodes:
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
set foldmethod=marker nmap0v/{ %zf
autocmd BufNewFile *.pm 0r ~/.vim/skeleton.pm autocmd BufNewFile *.pl 0r ~/.vim/skeleton.pl
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^Mand 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".
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.
map ^[OQ :w!^M:! perl %^Mwhich 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?
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:
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.
See also: Vim as a Perl IDE. These two nodes complement each other nicely, thanks! :-)
HTH,
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.
" run srcipt from Vim pressing F5 mapCan'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).:!perl ./%
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