1. 程式人生 > >Python+Vim:天作之合

Python+Vim:天作之合

1.與Vim的緣分

7年的Java開發生涯,一年半Java和Python混合的研究生學習,到現在最近全職Python開發,不知不覺已經半年沒碰Java了。從2013年開始從Eclipse轉到Intellij,完全習慣了用JetBrain的產品做各種語言的IDE。現在Python開發也不例外,依舊沿用Intellij IDEA外加Python外掛。雖然一直用Intellij這種“重型”IDE,但文字編輯方面一直用Vim,所以在Intellij裡也繼續使用Vim外掛。

多年前嘗試過使用Vim作為Java的IDE,但最後還是放棄了。最近又重新思考發現:轉向Python開發後,Vim與Intellij之間的差距越來越小了。可能對於所有指令碼語言大家都有同樣的感覺,於是最近又重新嘗試將Vim作為IDE來使用。

本文主要參考了這篇《VIM and Python - a Match Made in Heaven》。很多其他的Vim環境搭建文章都提到了這篇文章,內容確實非常不錯,而且名字也非常美,Vim和Python,天作之合!

2.Vim作為IDE的好處

首先,相比其他IDE,Vim更加輕量級。如果不裝過多外掛或外掛效能沒問題的話,使用Vim開發對硬體的要求是比較低的。其二,Vim能在本地和遠端伺服器上獲得一致的程式設計體驗,簡單說就是一個vimrc個性化的配置檔案走天下,非常便攜。最後,也是最最主要的原因就是Vim本身的特點:所思即所得的編輯速度。這也是在接觸瞭解Vim之後便一發不可收拾,使用Intellij、Sublime等各種編輯器都要加裝Vim外掛的原因。

3.打造現代化的IDE

作為開胃菜,先給大家看一下最終的效果,這樣也許能更有動力去鼓搗後面那些配置。

vim

3.1 環境準備

首先,檢查Vim的版本以及Vim能否定位到Python,這裡跳過Vim的安裝說明了。

 :version
 :python import sys; print(sys.version)

其次,因為我們要安裝很多外掛,所以就需要一個現代化的外掛管理器,而不是一個個地手動下載安裝。目前流行的Vim外掛管理器有Vundle、Pathogen、Vim-plugin。其中Vim-plugin號稱速度最快最小巧,但本文還是使用老牌的Vundle,安裝命令非常簡單。

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

3.2 開箱即用

下面就是本文的主菜了——最終摸索出的一個比較好的Vim配置。先列在這裡給心急的同學,後面會慢慢介紹涉及到的每個外掛的用處和基本用法。

set nocompatible              " required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'


" ===== Add all your plugins here  =====
" (note older versions of Vundle used Bundle instead of Plugin)

Plugin 'Valloric/YouCompleteMe'

" Class structure viewer
Plugin 'majutsushi/tagbar'

" Move like AceJump in Emacs
Plugin 'easymotion/vim-easymotion'

" Color scheme
Plugin 'jnurmine/Zenburn'
Plugin 'altercation/vim-colors-solarized'

" File/folder navigation
Plugin 'scrooloose/nerdtree'
Plugin 'jistr/vim-nerdtree-tabs'

" Super searching
Plugin 'kien/ctrlp.vim'

" Powerful status bar
Plugin 'Lokaltog/powerline', {'rtp': 'powerline/bindings/vim/'}

" Out-of-box python development
"Plugin 'klen/python-mode'
"Plugin 'vim-syntastic/syntastic'
"Plugin 'nvie/vim-flake8'

" Git integration
"Plugin 'tpope/vim-fugitive'
Plugin 'airblade/vim-gitgutter'

" Navigate between tmux and vim seamlessly
Plugin 'christoomey/vim-tmux-navigator'

" ===== All of your Plugins must be added before the following line =====
call vundle#end()            " required

filetype plugin indent on    " required
set encoding=utf-8


" give you the standard four spaces when you hit tab, ensure your line length
" doesn’t go beyond 80 characters
au BufNewFile,BufRead *.py
    \ set tabstop=4     |
    \ set softtabstop=4 |
    \ set shiftwidth=4  |
    "\ set textwidth=79  |
    \ set expandtab     |
    \ set autoindent
    "\ set fileformat=unix

" Autocomplete window goes away when you’re done with it
let g:ycm_autoclose_preview_window_after_completion=1

" F8 key will toggle the Tagbar window
nmap <F8> :TagbarToggle<CR>
let g:tagbar_width=50

" Easymotion: enhance default vim search by named jump and highlighting
":nmap <Space> <Leader><Leader>w
let g:EasyMotion_smartcase = 1
map  / <Plug>(easymotion-sn)
omap / <Plug>(easymotion-tn)
map  n <Plug>(easymotion-next)
map  N <Plug>(easymotion-prev)
map <Leader>j <Plug>(easymotion-j)
map <Leader>k <Plug>(easymotion-k)

" Color. Fix scheme missing problem in tmux+iterm2
colorscheme zenburn
set background=dark
set t_Co=256

" Ignore files in NERDTree
let NERDTreeIgnore=['\.pyc$', '\~$'] "ignore files in NERDTree

" Always use the directory you started vim in to search
let g:ctrlp_working_path_mode = 0

" For pymode or other syntatic plugin?
let python_highlight_all=1
syntax on
set nu
set autoread

現在啟動Vim,執行Vundle的安裝命令將所有外掛都安裝上,後續使用時不用再執行這條命令。

 :PluginInstall

以後再啟動Vim時,進入專案所在的根資料夾,然後執行下面命令。因為這樣會自動開啟NERDTree導航欄,開啟Tagbar邊欄,一個IDE就這樣準備就緒了。

 $ vim +':NERDTree' +':TagbarToggle'

3.3 外掛使用

編輯部分是Vim的強項,所以其實外掛主要增強的是智慧提示、視窗UI、專案內查詢以及與其他開發工具結合(如Git)等方面:

  • 智慧提示YouCompleteMe即插即用,如果需要進一步的符號跳轉,則需要額外的ctags支援。
  • 瀏覽NERDTreetagbar提供檔案目錄和結構的瀏覽。而vim-tmux-navigator則是視窗內跳轉的神器,用ctrl+H/J/K/L就可以無差別的在所有vim和tmux內部視窗(pane)間跳轉。
  • 查詢easymotion提供很多功能,但最簡單方便的就是與Vim本身的”/”查詢整合,直接提供AceJump功能。j和k提供行跳轉。ctrlp用Ctrl+P啟用,輸入檔名進行模糊查詢,或可以水平或垂直分割視窗來開啟選中的檔案進行編輯。
  • Gitpowerline顯示檔案路徑和當前檔案所在的Git分支。vim-gitgutter可以像IDE一樣在行號位置標記出新增、刪除、修改的行,同時]c和[c可以跳轉到修改位置,hp和hu可以方便地預覽和回滾某一塊的更改(leader預設是”\”)。

還有一些很不錯的外掛還沒有用起來,例如vim-fugitive還沒用上,vim-flake8不支援與Git結合只提示修改過部分的程式碼格式問題,syntasitc官方推薦配置出現效能問題導致Vim經常性的卡死。

4.碰到的問題

4.1 YCM報錯

YCM安裝後報錯:YouCompleteMe unavailable: requires Vim 7.4.1578+.
Info: You appear to be running the default system Vim on macOS. It reports as patch 8056, but it is really older than 1578. Please consider MacVim, homebrew Vim or a self-built Vim that satisfies the minimum requirement.

The ycmd server SHUT DOWN (restart with ‘:YcmRestartServer’)

解決方法如下:

 $ cd ~/.vim/bundle/YouCompleteMe
 $ ./install.py (cmake required)

4.2 Vim顏色在tmux中丟失