Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

.vimrc options to assist with perl coding

by Malach (Scribe)
on Feb 05, 2002 at 03:54 UTC ( #143348=snippet: print w/replies, xml ) Need Help??
Description: Although not perl code, this has sped up my development in perl markedly.

Stole (and altered) this off a friend of mine who found it and altered it somewhere else.

For those who are that way inclined, you can also do similar things with php.

Simple instruction: F1 validates the per... checking for syntax errors etc etc, and brings up the results in a new window. f2 closes the window.

f3 runs the perl script, bringing up the results in a new window. (again) f2 closes the window. Generally, I use this for debugging.

Improvements and suggestions welcomed.
" F2 close current window (commonly used with my F1/F3 functions)
noremap <f2> <Esc>:close<CR><Esc>

" perl -cw buffer, using a temp file, into a new window
function! PerlCW()
    let l:tmpfile1 = tempname()
    let l:tmpfile2 = tempname()

    execute "normal:w!" . l:tmpfile1 . "\<CR>"
    execute "normal:! perl -cw ".l:tmpfile1." \> ".l:tmpfile2." 2\>\&1
+ \<CR>"
    execute "normal:new\<CR>"
    execute "normal:edit " . l:tmpfile2 . "\<CR>"

" perl buffer, using a temp file, into a new window
function! PerlOutput()
    let l:tmpfile1 = tempname()
    let l:tmpfile2 = tempname()
    execute "normal:w!" . l:tmpfile1 . "\<CR>"
    execute "normal:! perl ".l:tmpfile1." \> ".l:tmpfile2." 2\>\&1 \<C
    execute "normal:new\<CR>"
    execute "normal:edit " . l:tmpfile2 . "\<CR>"

" Settings for editing perl source (plus bind the above two functions)
function! MyPerlSettings()
    if !did_filetype()
        set filetype=perl

    set textwidth=78
    set expandtab
    set tabstop=4
    set shiftwidth=4
    set cindent
    set comments=:#
    set formatoptions=croql
    set keywordprg=man\ -S\ 3
    noremap <f1> <Esc>:call PerlCW()<CR><Esc>
    noremap <f3> <Esc>:call PerlOutput()<CR><Esc>

    if has("eval")
           augroup SetEditOpts
        autocmd FileType perl :call MyPerlSettings()
    augroup END


Replies are listed 'Best First'.
Re: .vimrc options to assist with perl coding
by toadi (Chaplain) on Feb 05, 2002 at 09:49 UTC
    Another part I have in my .vimrc:
    au BufNewFile *.pl,*.cgi s,^,#!/usr/bin/perl -w, \ | w \ | !chmod +x %
    Maybe not pretty but does the trick, maybe you can also add to this the use strict; stuff and some other header stuff. So eacht time you start a new perl file it automatically puts a header in. Try it for the next version :)

    My opinions may have changed,
    but not the fact that I am right

Re: .vimrc options (quickfix mode integration)
by markjugg (Curate) on Feb 22, 2002 at 01:45 UTC
    It looks like you are trying to re-invent the nice quickfix mode integration for Perl that's built into VIM 6.0. Here's how to get started with it. It's super handy.
    • Quickfix Mode Integration Like other great editors, vim has a built in mechanism to speed up the compile-debug-edit cycle. Here's the least you need to know to get running with this.
      1. Tweak your .vimrc file Add these lines to your .vimrc to get started. The first command tells vim to compile the current buffer when you type ":make", the second command tells vim to recognize the error format that Perl returns.

           set makeprg=$VIMRUNTIME/tools/\ -c\ %\ $*
              set errorformat=%f:%l:%m

        To make this cycle even faster, also add this line:

           set autowrite

        That will cause the file to automatically be saved in some cases, such as when you type ":make".

      2. Set your Perl Path If you are using some of your own modules, you need to give vim a hint about where to find them. There are two straightforward ways to handle this. One is to set the path in one of the environment variables that Perl will look in, such as PERL5LIB. The other method is to simply use vim command ":lcd" to set the Local Current Directory for the perl file you want to compile to be relevant libary path.
      3. Compile your Perl from within vim You are now ready to compile a script from within vim. Simply use ":make" for this. Vim will run "perl -c yourscript.cgi" and process the results. It will show you a list of the resulting errors, if any, and jump to the first error.
      4. Navigate your errors You are now in quickfix mode and have some commands available to quickly address the remaining errors. Here are the essential commands for this: ":cl" lists all the errors for you again, ":ccN" jumps to error number 'N', ":cn" jumps to the next error. You can read the details with ":help quickfix"


      Thanks mark... definitely looks worth investigating

      Just in case I'm missing something... What benefits does this add (over my method) other than the being able to skip through the errors?

      So, this baby seal walks into a club.....

        quickfix mode is nice because it offers a standard command set-- you can use the same commands for compiling as Perl as you for grepping through code, or compiling C or another language. I haven't actually tried your method, so I can't do a direct comparison.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://143348]
[Corion]: Oooh - I had another devious idea - "Host C" - a C language where every struct is 4K in size. This makes memory and disk access incredibly fast ;)
[Corion]: (because everything is aligned to a memory page and all pages can be read+written directly from disk without buffering)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-04-25 08:58 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (449 votes). Check out past polls.