Vim与Python:上下文无关的语言方法自动完成功能

1 投票
2 回答
1087 浏览
提问于 2025-04-16 10:26

我正在写一个小的Python脚本,目的是学习VIM的基础知识(我还是VIM的新手)。

我已经配置好VIM可以使用自动补全功能,而且它确实可以工作。

比如说,当我输入str.然后按下ctrl+x和ctrl+o时,它会给我推荐所有的字符串方法。

但是在我的代码中,有这样的内容:

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

我希望VIM在我输入line.rpart之后能自动补全rpartition这个方法名。我并不指望它能知道line对象的类型,但我希望VIM能根据Python库的知识,给我提供一个不考虑上下文的补全列表。

比如说,如果我在Eclipse中尝试补全:

anObject.rpart

它会给我推荐rpartition这个方法,即使它和anObject没有任何关系!

请问在VIM中能做到这一点吗?
谢谢。

我的.vimrc文件:

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

2 个回答

2

说真的,赶紧去用Jedi吧!

它真的比其他所有的自动补全工具要好得多。

祝你好运!

1

你一定要看看 PySmell,这个工具可以很方便地在Vim中安装。它会根据对你项目的静态分析生成一个补全菜单。它还可以根据为外部库(比如Python标准库或Django库)生成的标签,提供补全建议。

我之前对Vim的Python自动补全很满意,但自从我换用了PySmell后,就再也没有回头过。

撰写回答