在Vim初探(四)中介绍了Vim的插件技术,本节将利用这些技术打造一个Python IDE。
1.文法高亮
为了能在Vim中支持Python文法需要用到插件python.vim,该插件默认位于//syntax/下,如果你在该路径下没有找到这个插件,需要到python.vim : Enhanced version of the python syntax highlighting script下载。然后为了能让Vim识别Python文法需要在vimrc中添加:
set filetype=python
au BufNewFile,BufRead *.py,*.pyw setf python
2.缩进
在vimrc中添加如下缩进相关的代码:
set autoindent " same level indent
set smartindent " next level indent
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
3.项目视图
像Visual Studio或Eclipse之类的IDE都会提供项目视图(位于左侧或右侧),程序员利用该视图在文件间或类间跳转。利用Ctags和插件Tasklist可以在vim中实现此功能。
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1
下图展示了MiniBufExplorer的使用效果:
5.Omnicompletion
Vim7中添加了对文法提示和自动完成的支持,对于python来说需下载pythoncomplete.vim并将其放在//autoload/目录下,接着在vimrc中添加如下命令:
filetype plugin on
set ofu=syntaxcomplete#Complete
autocmd FileType python set
omnifunc=pythoncomplete#Complete
autocmd FileType python runtime! autoload/pythoncomplete.vim
最后在编写代码时通过ctrl-x ctrl-o来打开文法提示上下文菜单,如下图所示:
参考文献
1.http://www.swaroopch.com/notes/Vim
2.http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
3.http://www.phacks.net/macvim-code-completion-syntax-highlighting-for-python-pyqt4-twisted-on-mac-osx-snow-leopard/
4.http://vim.wikia.com/wiki/Omni_completion