通过RopeVIM在VIM中实现Python自动完成

ropevim可以实现在vim中自动完成,自动import等功能。
参考: http://rope.sourceforge.net/ropevim.html

下载几个rope,ropevim,ropemode:

mkdir ~/install/ropehg
hg clone http://bitbucket.org/agr/rope/ ~/install/ropehg/rope
hg clone http://bitbucket.org/agr/ropevim/ ~/install/ropehg/ropevim
hg clone http://bitbucket.org/agr/ropemode/ ~/install/ropehg/ropemode

可以选择安装:

cd ~/install/ropehg/rope  
python setup.py install
cd ~/install/ropehg/ropevim  
python setup.py install
cd ~/install/ropehg/ropemode  
python setup.py install

或者在 .vimrc 中添加下面的代码:

let $PYTHONPATH .= ":/Users/liwen/install/ropehg/rope:/Users/liwen/install/ropehg/ropemode:/Users/liwen/install/ropehg/ropevim"
source /Users/liwen/install/ropehg/ropevim/ropevim.vim

配置ropevim的代码如下

let ropevim_codeassist_maxfixes=10
let ropevim_guess_project=1
let ropevim_vim_completion=1
let ropevim_enable_autoimport=1
let ropevim_extended_complete=1
 
function! CustomCodeAssistInsertMode()
    call RopeCodeAssistInsertMode()
    if pumvisible()
        return "\<C-L>\<Down>"
    else
        return ''
    endif
endfunction
 
function! TabWrapperComplete()
    let cursyn = synID(line('.'), col('.') - 1, 1)
    if pumvisible()
        return "\<C-Y>"
    endif
    if strpart(getline('.'), 0, col('.')-1) =~ '^\s*$' || cursyn != 0
        return "\<Tab>"
    else
        return "\<C-R>=CustomCodeAssistInsertMode()\<CR>"
    endif
endfunction
 
inoremap <buffer><silent><expr> <Tab> TabWrapperComplete()

自动完成直接可以用,但 RopeRename 不工作,可能是工程太大,分起来比较费劲。还不错。

PS:
1. hg – mercurial
2. 如果遇到以下错误

Error detected while processing function LoadRope:
line    1:
E319: Sorry, the command is not available in this version: python << EOF
line    2:
E492: Not an editor command: import ropevim
line    3:
E492: Not an editor command: EOF
Press ENTER or type command to continue

可能由于vim没有启用python支持,可以打开一个buffer,在命令模式下输入

:python print "hello"

如能正常打印,则配置正确,否则需要重新编译或者安装python支持

sudo port install vim +python26
sudo port install mvim +python26

Good luck!!

标签: ,

  • gdr

    Hi. With ropevim and your method, after I type I get one unnecessary tab:

    mymodel.object

    Any idea want I’m doing wrong? Thanks.

    • http://sunliwen.com Liwen

      I don’t know how exactly you type, but on me only when I press ‘tab’ it will give some prompt, or nothing will happen. :)