python號稱人工智能語言,如今可算大熱,這篇博客將介紹如何用vim打造一款本身專屬的python編程環境。css
因爲安裝YouCompleteMe須要vim8.0及以上版本,因此得安裝使用vim的8.0及以上版本,使用vim --version查看本身的vim版本,若是沒達到要求能夠參考個人另外一篇博客vim8.0安裝教程進行安裝。接着使用git安裝vim的包管理工具Vundlehtml
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
而後在vim的配置文件~/.vimrc中添加以下內容node
set nocompatible " required filetype off " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " alternatively, pass a path where Vundle should install plugins "call vundle#begin('~/some/path/here') " let Vundle manage Vundle, required <strong>Plugin 'gmarik/Vundle.vim'</strong> " Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin) " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required
配置好以後執行vim命令打開編輯窗口,輸入命令PluginList若能顯示已安裝的插件則代表Vundle已安裝成功,命令PluginInstall可用於安裝插件,PluginClean可用於卸載插件,不過須要先在~/.vimrc中將不要下載的插件註釋掉或者去除。python
在~/.vimrc中添加讓vundle安裝的插件而且對插件進行配置,這裏直接貼出我~/vim.rc的所有內容git
set nocompatible " required filetype off " required "設置Vundle的運行路徑 set rtp+=/home/brooksj/.vim/bundle/Vundle.vim "設置插件的安裝路徑,vundle插件起始標誌 call vundle#begin('/home/brooksj/.vim/bundle') "讓vundle管理插件版本 Plugin 'VundleVim/Vundle.vim' "添加nerdtree插件 Plugin 'scrooloose/nerdtree' "使用tab鍵切換窗口與目錄樹 Plugin 'jistr/vim-nerdtree-tabs' "添加jedi-vim代碼補全插件 "Plugin 'davidhalter/jedi-vim' Plugin 'Valloric/YouCompleteMe' "添加PEP8代碼風格檢查 Plugin 'nvie/vim-flake8' "配色方案 Plugin 'jnurmine/Zenburn' Plugin 'altercation/vim-colors-solarized' Plugin 'tomasr/molokai' "代碼摺疊插件 Plugin 'tmhedberg/SimpylFold' "自動縮進 Plugin 'vim-scripts/indentpython.vim' "在vim的normal模式下搜索文件 Plugin 'kien/ctrlp.vim' "Powerline狀態欄 Plugin 'Lokaltog/powerline', {'rtp': 'powerline/bindings/vim/'} "你的全部插件須要在下面這行以前 call vundle#end() "設置顯示powerline set laststatus=2 "設置分割窗口 set splitbelow set splitright "設置窗口移動快捷鍵 nnoremap <C-J> <C-W><C-J> nnoremap <C-K> <C-W><C-K> nnoremap <C-L> <C-W><C-L> nnoremap <C-H> <C-W><C-H> "設置按F2啓動NerdTree map <F2> :NERDTreeToggle<CR> "youcompleteme 默認tab s-tab 和自動補全衝突 ""let g:ycm_key_list_select_completion=['<c-n>'] let g:ycm_key_list_select_completion = ['<Down>'] "let g:ycm_key_list_previous_completion=['<c-p>'] let g:ycm_key_list_previous_completion = ['<Up>'] "關閉加載.ycm_extra_conf.py提示 let g:ycm_confirm_extra_conf=0 " 開啓 YCM 基於標籤引擎 let g:ycm_collect_identifiers_from_tags_files=1 " 從第2個鍵入字符就開始羅列匹配項 let g:ycm_min_num_of_chars_for_completion=2 " 禁止緩存匹配項,每次都從新生成匹配項 let g:ycm_cache_omnifunc=0 " 語法關鍵字補全 let g:ycm_seed_identifiers_with_syntax=1 "force recomile with syntastic nnoremap <F5> :YcmForceCompileAndDiagnostics<CR> "nnoremap <leader>lo :lopen<CR> "open locationlist "nnoremap <leader>lc :lclose<CR> "close locationlist inoremap <leader><leader> <C-x><C-o> "在註釋輸入中也能補全 let g:ycm_complete_in_comments = 1 "在字符串輸入中也能補全 let g:ycm_complete_in_strings = 1 "註釋和字符串中的文字也會被收入補全 let g:ycm_collect_identifiers_from_comments_and_strings = 0 "let g:ycm_autoclose_preview_window_after_completion=1 "隱藏目錄樹種的.pyc文件 let NERDTreeIgnore=['\.pyc$', '\~$'] "ignore files in NERDTree "設置主題顏色,以及設置快捷鍵F5 set t_Co=256 set background=dark if has('gui_running') colorscheme solarized else colorscheme molokai "let g:molokai_original=1 endif call togglebg#map("<F3>") if &diff colors blue endif "開啓代碼摺疊 set foldmethod=indent set foldlevel=99 "設置快捷鍵爲空格 nnoremap <space> za "顯示摺疊代碼的文檔字符串 let g:SimpylFold_docstring_preview=1 "python代碼縮進PEP8風格 au BufNewFile,BufRead *.py,*.pyw set tabstop=4 au BufNewFile,BufRead *.py,*.pyw set softtabstop=4 au BufNewFile,BufRead *.py,*.pyw set shiftwidth=4 au BufNewFile,BufRead *.py,*.pyw set expandtab au BufNewFile,BufRead *.py,*.pyw set textwidth=79 au BufNewFile,BufRead *.py,*.pyw set autoindent au BufNewFile,BufRead *.py,*.pyw set fileformat=unix "對其餘文件類型設置au命令 au BufNewFile,BufRead *.js, *.html, *.css set tabstop=2 au BufNewFile,BufRead *.js, *.html, *.css set softtabstop=2 au BufNewFile,BufRead *.js, *.html, *.css set shiftwidth=2 "高亮顯示行偉沒必要要的空白字符 highlight Whitespace ctermbg=red guibg=red au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match Whitespace /\s\+$\ \+/ "設置行號顯示 set nu "設置utf-8編碼 set encoding=utf-8 let python_highlight_all=1 syntax on filetype plugin indent on set backspace=indent,eol,start set cursorline set history=1000 set fileencodings=utf-8,gb18030,utf-16,big5 set hlsearch set clipboard=unnamed set expandtab set softtabstop=4 set tabstop=4 set shiftwidth=4
對安裝的幾個插件做一個簡要的介紹:github
終端執行vim命令打開vim,而後輸入命令PLuginInstall對上面配置的插件進行安裝,下面是我安裝好以後插件截圖shell
<div align="center"> <img src="https://raw.githubusercontent.com/tracy-talent/Notes/master/imgs/vundle_plugin.png"> </div>編程
YouCompleteMe的安裝比較特殊,使用Vundle安裝好以後還須要進入到~/.vim/bundle/YouCompleteMe目錄下安裝一次才能正常使用YouCompleteMe的所有功能。執行下面命令以前確保已安裝cmakevim
./install.py --clang-completer
若是還想支持go和node.js的自動補全能夠緩存
./install.py --clang-completer --gocode-completer --tern-completer
後期須要其餘的語言補全能夠上網查一下對應的安裝選項而後附加在./install.py以後執行便可。
到此vim的python配置就所有完成了,且看vim的效果圖
<div align="center"> <img src="https://raw.githubusercontent.com/tracy-talent/Notes/master/imgs/vim效果圖.png"> </div>
參考: