爲Python配置Vim編輯器(GUI/非GUI皆可)

**

Vim as a python IDE

**
這裏寫圖片描述
最近一直在寫Python,但一直沒有像樣的配置一下Vim,沒有代碼提示、沒有Highlight導致寫代碼效率低下,於是輾轉找到了英國的一篇配置Vim for Python的文章,抽空翻譯了一下。

時間不多,前言部分就不翻譯了,直接開始正文。

首先,在開始爲python配置之前,你需要安裝vim和Vundle。Vundle是一種vim插件包安裝工具(類似於python的pip、ubuntu的apt-get),可以大大加快你實用vim、添加vim插件的效率。它從github獲取資源,可以從其github repo的README獲取更多信息。使用如下命令安裝Vundle:

git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

然後在你的.vimrc文件中添加下面幾行:

set nocompatible
syntax on
filetype off

set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" let Vundle manage Vundle
" required! 
“ 啓用Vundle

Bundle 'gmarik/vundle'

" The bundles you install will be listed here 
“ 你所安裝的包將被列在這裏
filetype plugin indent on

" The rest of your config follows here
“ 你的其他配置代碼

然後運行vim,在Normal模式下運行下面命令:

:BundleList

一個新的窗口應該就顯現了,即Vundle,會列出你安裝的所有包(bundle),這就意味着你的安裝成功了。在本文下面你會安裝不同的包,你只需分別將他們添加到.vimrc中即可,下面將詳細敘述。

限制高亮超長行寬度
你可能會想要限制python文件的行寬。我喜歡限制在120個字符,標準情況下是80個,但是在現在的高清顯示器下顯示更多效果更好,你也可以自己調整到自己喜歡的數值。想要啓用這個功能,在.vimrc中加入下面代碼:

augroup vimrc_autocmds
    autocmd!
    " highlight characters past column 120
    autocmd FileType python highlight Excess ctermbg=DarkGrey guibg=Black
    autocmd FileType python match Excess /\%120v.*/
    autocmd FileType python set nowrap
    augroup END

所有超過這個行寬的代碼都會被黑色高亮,你也可以修改高亮顏色以適配你的vim主題。

Powerline

下面我們安裝Powerline插件,使得vim運行時顯示如下信息:

它會顯示當前在Git中的分支、你正在編輯的文件名以及其他一些有用的信息。
只需添加:

Bundle 'Lokaltog/powerline', {'rtp': ‘powerline/bindings/vim/‘}

到你的.vimrc

" The bundles you install will be listed here.

的下面即可。
然後重啓vim,再次輸入:BundleList,顯示出你所有添加的插件,然後輸入

:BundleInstall

來安裝這些插件中尚未安裝的部分。Powerline還有一些別的可選配置,你可以登錄它的github repo看詳細說明
https://github.com/Lokaltog/powerline-fonts

Fugitive

Fugitive是一個Git插件,使得你可以在Vim裏面調用Git命令。在vim中實用git命令時要在原本命令前面加上’G’,如”Gcommit”等等。安裝Fugitive,在.vimrc中添加

Bundle ‘tpope/vim-fugitive'

然後像上面一樣使用

:BundleInstall

來進行安裝

NerdTree

NerdTree是一個在vim中新窗口顯示的文件瀏覽器,效果如下:

添加

Bundle ‘scrooloose/nerdtree'

.vimrc,安裝之後,再在.vimrc最後添加

map <F2> :NERDTreeToggle<CR>

來設置按F2啓動NerdTree。

Python Mode

重頭戲,這個插件基本上添加了你想在vim中實用的關於python的所有功能,比如語法檢查、代碼補全、顯示代碼文檔、類間跳躍等等工具,詳細的可以到repo中查看:
https://github.com/klen/python-mode
添加

Bundle ‘klen/python-mode'

.vimrc中進行安裝。在vim中你也可以用:help python-mode來查看python mode的實用說明。下面對pythonmode進行配置,下面是我喜歡的一些配置:

" Python-mode
" Activate rope
" Keys: 按鍵:
" K             Show python docs 顯示Python文檔
" <Ctrl-Space>  Rope autocomplete  使用Rope進行自動補全
" <Ctrl-c>g     Rope goto definition  跳轉到定義處
" <Ctrl-c>d     Rope show documentation  顯示文檔
" <Ctrl-c>f     Rope find occurrences  尋找該對象出現的地方
" <Leader>b     Set, unset breakpoint (g:pymode_breakpoint enabled) 斷點
" [[            Jump on previous class or function (normal, visual, operator modes)
" ]]            Jump on next class or function (normal, visual, operator modes)
"            跳轉到前一個/後一個類或函數
" [M            Jump on previous class or method (normal, visual, operator modes)
" ]M            Jump on next class or method (normal, visual, operator modes)
"              跳轉到前一個/後一個類或方法
let g:pymode_rope = 1

" Documentation 顯示文檔
let g:pymode_doc = 1
let g:pymode_doc_key = 'K'

“Linting 代碼查錯,=1爲啓用
let g:pymode_lint = 1
let g:pymode_lint_checker = "pyflakes,pep8"
" Auto check on save
let g:pymode_lint_write = 1

" Support virtualenv
let g:pymode_virtualenv = 1

" Enable breakpoints plugin
let g:pymode_breakpoint = 1
let g:pymode_breakpoint_bind = '<leader>b'

" syntax highlighting 高亮形式
let g:pymode_syntax = 1
let g:pymode_syntax_all = 1
let g:pymode_syntax_indent_errors = g:pymode_syntax_all
let g:pymode_syntax_space_errors = g:pymode_syntax_all

" Don't autofold code 禁用自動代碼摺疊
let g:pymode_folding = 0

Jedi vim

Jedi vim可能是一種更好的python代碼補全插件,可以到
https://github.com/davidhalter/jedi-vim
查看使用說明。安裝之後,需要先禁用Rope,用

let g:pymode_rope = 0

替換剛纔添加的

let g:pymode_rope = 1

其他設置
在vimrc中:

" Use <leader>l to toggle display of whitespace
nmap <leader>l :set list!<CR>
" automatically change window's cwd to file's dir
set autochdir

" I'm prefer spaces to tabs
set tabstop=4
set shiftwidth=4
set expandtab

" more subtle popup colors 
if has ('gui_running')
    highlight Pmenu guibg=#cccccc gui=bold    
endif

比較常用,就不解釋每一條具體含義了。
這是一些基本的Vim for Python配置,其他的功能可以去Vundle頁面查看使用其方法,或者找其他的插件使用。

翻譯自:http://unlogic.co.uk/2013/02/08/vim-as-a-python-ide/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章