Search code examples
pythonvimautocompleteomnicomplete

Vim and python: context-unaware autocompletion of language methods


I am writing a little python script to learn VIM basics (I'm a beginner with VIM). I've configured VIM to work with omnicompletion, and it does. For example, if I write str. then press ctr+x, ctr+o it suggests me all of the string methods. However in my code I have something like this:

for line in inFile.readlines():
    something = line.rpartition(" ")[0]

I'd like VIM to autocomplete the rpartition method name after typing line.rpart. I don't expect it to know line object type, but I'd like VIM to propose a context-unaware completion list based on python libraries acquaintance. For example if with eclipse i try to complete

anObject.rpart

it suggests me the rpartition method even if it has nothing to do with anObject!

Is it possible to get this to work with VIM?
Thanks.

My .vimrc file:

set showcmd
set textwidth=80
set expandtab
set smarttab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set number
set autoindent
filetype indent on
filetype plugin on
autocmd BufRead,BufNewFile *.py syntax on
autocmd BufRead,BufNewFile *.py set ai
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,with,try,except,finally,def,class
set modeline
syntax on
" Closes the Omni-Completion tip window when a selection is
" made
autocmd CursorMovedI * if pumvisible() == 0|pclose|endif
autocmd InsertLeave * if pumvisible() == 0|pclose|endif

Solution

  • You definitely should take a look at PySmell which can be installed for Vim easily. It generates a completion menu based on a static analysis for a given project. It also can generate completion suggestions based on tags it produces for external libraries like the Python Standard Library or Django Lib.

    I was happy with Vims Python omnicimpletion, but since I switched to PySmell, I never looked back.