1. 程式人生 > >為Python配置Vim編輯器(GUI/非GUI皆可)

為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/