1. 程式人生 > >vim配置python編程環境及YouCompleteMe的安裝教程

vim配置python編程環境及YouCompleteMe的安裝教程

mil white mrc endif lead pep 文檔 進行 pre

python號稱人工智能語言,現在可算大熱,這篇博客將介紹如何用vim打造一款自己專屬的python編程環境。

step1

由於安裝YouCompleteMe需要vim8.0及以上版本,所以得安裝使用vim的8.0及以上版本,使用vim --version查看自己的vim版本,如果沒達到要求可以參考我的另一篇博客vim8.0安裝教程進行安裝。接著使用git安裝vim的包管理工具Vundle

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

然後在vim的配置文件~/.vimrc中添加如下內容

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中將不要下載的插件註釋掉或者去除。

step2

在~/.vimrc中添加讓vundle安裝的插件並且對插件進行配置,這裏直接貼出我~/vim.rc的全部內容

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

對安裝的幾個插件作一個簡要的介紹:

  • nerdtree可用於在vim窗口下查看文件的數形結構
  • YouCompleteMe可提供語法高亮以及代碼提示等,打造IDE必備
  • vim-flake8支持python PEP8代碼風格檢查
  • indentpython.vim用於寫python時自動縮進
  • SimpylFold用於代碼折疊(我配置了快捷鍵space或者za來折疊代碼)
  • ctrlp用於查找文件(按下ctrl+p即進入文件查找功能)
  • powerline是一款比較炫酷的狀態欄工具,顯示vim狀態及其打開文件的一些信息(我安裝powerline時提示我powerline只兼容python2.7以及python3.2版本,所以得註意你所使用的python版本)
  • vim-colors-solarized、Zenburn以及molokai是三款好看的配色方案,可以自己在~/.vimrc中配置時使用哪種顏色方案

step3

終端執行vim命令打開vim,然後輸入命令PLuginInstall對上面配置的插件進行安裝,下面是我安裝好之後插件截圖

技術分享圖片

step4

YouCompleteMe的安裝比較特殊,使用Vundle安裝好之後還需要進入到~/.vim/bundle/YouCompleteMe目錄下安裝一次才能正常使用YouCompleteMe的全部功能

./install.py --clang-completer

如果還想支持go和node.js的自動補全可以

./install.py --clang-completer --gocode-completer --tern-completer

後期需要其他的語言補全可以上網查一下對應的安裝選項然後附加在./install.py之後執行即可。

到此vim的python配置就全部完成了,且看vim的效果圖

技術分享圖片

參考:

https://www.cnblogs.com/cjy15639731813/p/5886158.html

https://blog.csdn.net/nzyalj/article/details/75331822

vim配置python編程環境及YouCompleteMe的安裝教程