tags:

views:

2062

answers:

8

Surprisingly as you get good at vim, you can code even faster than standard IDEs such as Eclipse. But one thing I really miss is code completion, especially for long variable names and functions.

Is there any way to enable code completion for Perl in vim?

+20  A: 

Ctrl-P (Get Previous Match) and Ctrl-N (Get Next Match) are kind of pseudo code completion. They basically search the file (Backwards for Ctrl-P, Forwards for Ctrl-N) you are editing (and any open buffers, and if you are using TAGS anything in your TAG file) for words that start with what you are typing and add a drop down list. It works surprisingly well for variables and function names, even if it isn't intellisense. Generally I use Ctrl-P as the variable or function I am looking for is usually behind in the code. Also if you keep the same copy of Vim open, it will search the files you have previously opened.

Kris Erickson
Works perfectly, just what I was after, thanks!
Edward Tanguay
+5  A: 

Well, Vim's generic completion mechanism is surprisingly good, just using Ctrl-N in insert mode. Also, line completion is very handy, using C-x C-l.

Also check out this vim script for perl.

Christian Oudard
+7  A: 

Vim 7 supports omni completion.

For example, I have this in my vimrc

autocmd FileType php set omnifunc=phpcomplete#CompletePHP

and then, when I press Ctrl-x Ctrl-o in Insert mode, I get a dropdown list of autocomplete possibilities.

Here's an omnicfunc for perl. No idea how well it works though.

Mark Biek
+1  A: 

Cntl-N

This is explained in the Perl Hacks book, along with how to do Package completion. Highly recommended.

Matthew Watson
+1  A: 

You should look at the SuperTab plugin: http://www.vim.org/scripts/script.php?script_id=1643 It let's you do completion (either the OmniCompletion or the regular completion) using tab and shift-tab instead of ^N and ^P.

indentation
+2  A: 

The .vimrc clip in one of the other answers is slightly wrong. To turn your tab key into an auto-complete key, use this code:

inoremap <tab> <c-r>=InsertTabWrapper()<cr>

function! InsertTabWrapper()
    let col = col('.') - 1
    if !col || getline('.')[col - 1] !~ '\k'
        return "\<tab>"
    else
        return "\<c-p>"
    endif
endfunction

You can find this, and tons of other vim tricks in this thread at Perlmonks--which links to even more threads with lots more customizations.

Matt Siegman
+2  A: 

The standard CTRL-N and CTRL-P work even better if you add the following to your ~/.vim/ftplugin/perl.vim file:

set iskeyword+=:

It will then auto-complete module names, etc.

+3  A: 

hey, checkthis:

http://github.com/c9s/perl-completion.vim