Vim 下部署Python IDE

1 簡述

本文為記錄在ubuntu16.04TLS環(huán)境下,將Vim打造成python IDE的部署手冊。有關(guān)Vim的功能和使用方法,大家可以自行了解。網(wǎng)上雖然有很多的相關(guān)教程,但覺得欠缺一些關(guān)鍵信息,如具體環(huán)境、配置說明以及錯誤處理。因此,我想寫一個既能完美部署Python IDE,又繁簡適當(dāng)?shù)牟渴鹗謨?,一同和大家交流學(xué)習(xí)。

2 環(huán)境

不同環(huán)境下可能會存在一些不同。因此,我在這里詳細(xì)的列出了我所使用的部署環(huán)境。

  • 操作系統(tǒng):Ubuntu 16.04 TLS (Ubuntu14.04 同樣適用);
  • Vim版本: 7.4 (2013. 8. 10);
  • Python版本:2.7 & 3.5 ;

3 安裝過程

3.1 Vim版本號是否高于vim7.41578 ?

(1) 若未安裝vim,請先安裝vim,在Terminal中輸入:

$ sudo apt-get update
$ sudo apt-get install vim

(2) 若已安裝vim,在Terminal中輸入下列命令,查看版本信息:

$ vim --version

我的版本信息顯示為7.4,因此需要升級:


vimv.png

(3) 如果版本號小于vim7.41578,請先更新Vim,因為Ubuntu16.04及以下版本最高僅提供vim7.4,所以我們需要添加新的包,輸入如下命令:

$ sudo add-apt-repository ppa:jonathonf/vim
$ sudo apt-get update
$ sudo apt-get install vim

更新后的版本信息為:


vimup.png

注:保證vim版本序號在vim7.41578+的主要原因,在于代碼自動補(bǔ)全的插件包“YouCompleteMe”只有在相關(guān)版本之上才能夠正確運(yùn)行。否則,在運(yùn)行插件時會出現(xiàn)如下錯誤:


YoucompleteMeerror.png

3.2 安裝Vundle & 配置 vimrc

(1) Vundle 是Vim的一款插件管理器。在Terminal中輸入:

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

建議使用默認(rèn)路徑‘~/.vim/bundle/Vundle.vim’。如果還沒有安裝git,請先安裝:

$ sudo apt-get install git

(2) 創(chuàng)建Vundle配置文件.vimrc(是一個隱藏文件)

$ touch ~/.vimrc

(3) 配置vimrc,在terminal中輸入:

$ sudo vim ~/.vimrc

即開始配置vim的插件信息。
請將如下信息粘貼至該文件的開始。其中“代表注釋行,是一些配置vimrc的一些提示信息。

"vundle
set nocompatible
filetype off

set rtp+=~/.vim/bundle/Vundle.vim
set splitbelow
set splitright
set foldmethod=indent
set foldlevel=99
set encoding=utf-8

call vundle#begin()

Plugin 'VundleVim/Vundle.vim'
"git interface
Plugin 'tpope/vim-fugitive'
"filesystem
Plugin 'scrooloose/nerdtree'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'kien/ctrlp.vim' 

"html
"  isnowfy only compatible with python not python3
Plugin 'isnowfy/python-vim-instant-markdown'
Plugin 'jtratner/vim-flavored-markdown'
Plugin 'suan/vim-instant-markdown'
Plugin 'nelstrom/vim-markdown-preview'
"python sytax checker
Plugin 'nvie/vim-flake8'
Plugin 'vim-scripts/Pydiction'
Plugin 'vim-scripts/indentpython.vim'
Plugin 'scrooloose/syntastic'

"auto-completion stuff
"Plugin 'klen/python-mode'
Plugin 'Valloric/YouCompleteMe'
Plugin 'klen/rope-vim'
"Plugin 'davidhalter/jedi-vim'
Plugin 'ervandew/supertab'
""code folding
Plugin 'tmhedberg/SimpylFold'

"Colors!!!
Plugin 'altercation/vim-colors-solarized'
Plugin 'jnurmine/Zenburn'

call vundle#end()

filetype plugin indent on    " enables filetype detection
let g:SimpylFold_docstring_preview = 1

"autocomplete
let g:ycm_autoclose_preview_window_after_completion=1

"custom keys
let mapleader=" "
map <leader>g  :YcmCompleter GoToDefinitionElseDeclaration<CR>
"
call togglebg#map("<F5>")
"colorscheme zenburn
"set guifont=Monaco:h14

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

"I don't like swap files
set noswapfile

"turn on numbering
set nu

"python with virtualenv support
py << EOF
import os.path
import sys
import vim
if 'VIRTUA_ENV' in os.environ:
  project_base_dir = os.environ['VIRTUAL_ENV']
  sys.path.insert(0, project_base_dir)
  activate_this = os.path.join(project_base_dir,'bin/activate_this.py')
  execfile(activate_this, dict(__file__=activate_this))
EOF

"it would be nice to set tag files by the active virtualenv here
":set tags=~/mytags "tags for ctags and taglist
"omnicomplete
autocmd FileType python set omnifunc=pythoncomplete#Complete

"------------Start Python PEP 8 stuff----------------
" Number of spaces that a pre-existing tab is equal to.
au BufRead,BufNewFile *py,*pyw,*.c,*.h set tabstop=4

"spaces for indents
au BufRead,BufNewFile *.py,*pyw set shiftwidth=4
au BufRead,BufNewFile *.py,*.pyw set expandtab
au BufRead,BufNewFile *.py set softtabstop=4

" Use the below highlight group when displaying bad whitespace is desired.
highlight BadWhitespace ctermbg=red guibg=red

" Display tabs at the beginning of a line in Python mode as bad.
au BufRead,BufNewFile *.py,*.pyw match BadWhitespace /^\t\+/
" Make trailing whitespace be flagged as bad.
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/

" Wrap text after a certain number of characters
au BufRead,BufNewFile *.py,*.pyw, set textwidth=100

" Use UNIX (\n) line endings.
au BufNewFile *.py,*.pyw,*.c,*.h set fileformat=unix

" Set the default file encoding to UTF-8:
set encoding=utf-8

" For full syntax highlighting:
let python_highlight_all=1
syntax on

" Keep indentation level from previous line:
autocmd FileType python set autoindent

" make backspaces more powerfull
set backspace=indent,eol,start


"Folding based on indentation:
autocmd FileType python set foldmethod=indent
"use space to open folds
nnoremap <space> za 
"----------Stop python PEP 8 stuff--------------

"js stuff"
autocmd FileType javascript setlocal shiftwidth=2 tabstop=2

如果對vimrc的意義感興趣,建議前往Vim+IDE了解。
(4) 在將上面信息粘貼好后,請按Esc后輸入:

:wq

保存文件。

(5) 再次進(jìn)入vimrc:

$ sudo vim /.vimrc

此時可能會報錯:


EOF.png

解決' py << EOF '錯誤的方法為安裝依賴包:

$ sudo apt-get install vim-nox-py2

3.3 搭建IDE環(huán)境

(1) 我們的操作對象依然是vimrc,需要在其中加入我們需要的插件信息。請再次進(jìn)入vimrc:

$ sudo vim /.vimrc

(2)請按Esc后輸入:

:PluginInstall

如圖:


plugin.png

回車,等待插件安裝完成后,顯示:


done.png

(3) 請按Esc后輸入:
:q

退出當(dāng)前操作。


t.png

(4) 再請按Esc后輸入:

:q

退出vimrc。

3.4 測試

(1) 我們當(dāng)前目錄下創(chuàng)建一個測試文件t.py,并編輯。

$ touch ~/t.py
$ vim ~/t.py

(2) 進(jìn)入文件后,下方會出現(xiàn)ycmd 服務(wù)關(guān)閉的消息(如下圖),本異常是因代碼自動補(bǔ)全插件YouCompleteMe尚未安裝完畢,暫時不用管它,我們稍后處理。


ycmd.png

好像沒什么變化,對嗎?試著按F3,應(yīng)該看到變化了吧,這就是目錄顯示列表。


cg.png

至此,我們就完成了vimrc的配置。

3.5 YouCompleteMe安裝

(1) 接下來,我們將完成代碼補(bǔ)全功能。在Terminal中輸入:

$ cd ~
$ mkdir ycm_build
$ cd ycm_build
$ cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

(2) 如果你還沒有安裝cmake,就會出現(xiàn)提示。


cm.png

沒有安裝,就請在Terminal中輸入:

$ sudo apt-get install cmake

(3) 安裝完成,再次嘗試:

$ cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

可能會報錯:


cme.png

解決方法,安裝依賴包:

$ sudo apt-get install python-dev

再次嘗試:

$ cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

安裝成功。


cmm.png

(4) 安裝其他依賴包:

$ sudo apt install build-essential python3-dev

(5) 安裝YCM:

$ cd ~/.vim/bundle/YouCompleteMe
$ sudo python3 install.py --clang-completer

安裝完成。


ycmm.png

4 體驗

至此,我們就可以開始高效的工作了。可能,繼續(xù)要學(xué)習(xí)的就是各個插件的使用方法了。


ycmmm.png

歡迎大家交流!

5 參考

(1) Vim+IDE

(2) Vimrc

(3) YouCompleteMe

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容