天天看點

CentOS7 Vim自動補全插件----YouCompleteMe安裝與配置

最近剛裝了新系統CentOS7,想要把編碼環境配置一下,使用Vim編寫程式少不了使用自動補全插件,我以前用的是neocomplcache+code_complete+omnicppcomplete。但在網上搜尋時,看到了YouCompleteMe,說YCM更好用一些。個人也喜歡新鮮事物,故決定安裝YCM。但安裝過程遇到了不少坑,網上的教程有不同的安裝方法,基本都試遍了。最終在倒騰了一下午的情況下終于弄好了,現在把可行的安裝配置方法貼出來,以供需要的人參考。

YouCompleteMe:一個随鍵而全的、支援模糊搜尋的、高速補全的插件。YCM 由 google 公司搜尋項目組的軟體工程師 Strahinja Val Markovic 所開發,YCM 後端調用 libclang(以擷取AST,當然還有其他語言的語義分析庫)、前端由 C++ 開發(以提升補全效 率)、外層由 python 封裝(以成為 vim 插件),它可能是我見過安裝最複雜的 vim 插件了。

要安裝YouCompleteMe ,vim須支援python。看是否支援,可以在vim中:version 檢視, 如果python前有+号,就是支援,減号就是不支援。

如果不支援,需要以編譯安裝方式重新安裝vim。編譯配置選項:

./configure --with-features=huge --enable-pythoninterp --enable-python3interp --enable-luainterp --enable-multibyte --enable-sniff --enable-fontset
           

一、安裝vundle插件

步驟一:

[[email protected] ~]$ git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle^C
           

 步驟二: 在.vimrc中配置

[[email protected] ~]$ vim .vimrc
           
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" alternatively, pass a path where Vundle should install plugins
"let path = '~/some/path/here'
"call vundle#rc(path)

" let Vundle manage Vundle, required
Plugin 'gmarik/vundle'

" The following are examples of different formats supported.
" Keep Plugin commands between here and filetype plugin indent on.
" scripts on GitHub repos
Plugin 'tpope/vim-fugitive'
Plugin 'Lokaltog/vim-easymotion'
Plugin 'tpope/vim-rails.git'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" scripts from http://vim-scripts.org/vim/scripts.html
Plugin 'L9'
Plugin 'FuzzyFinder'
" scripts not on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" ...

filetype plugin indent on     " required
Bundle 'Valloric/YouCompleteMe'
           

步驟三:儲存退出,打開vim,輸入   :BundleInstall 進行自動安裝。

程序如下,+号表示已經安裝,>表示正在安裝。

[[email protected] ~]$ vim
           
:BundleInstall 
           
CentOS7 Vim自動補全插件----YouCompleteMe安裝與配置

安裝時有個錯誤,這是正常的,因為ycm需要手動編譯出庫檔案。

Done! With errors; press l to view log

ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; you need to compile YCM before using it. Read the docs!

二、安裝配置YouCompleteMe

 步驟四: 然後到.vim/bundle/YouCompleteMe 下執行指令:

./install.sh --clang-completer

參數是為了支援c/c++的補全

[zhu[email protected] YouCompleteMe]$ ./install.sh --clang-complete
           

然後可能還會出現報錯:

Some folders in /home/sky-tm/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party are empty; you probably forgot to run:    git submodule update --init --recursive

若出現此錯誤,則按照提示來,繼續輸入指令:

 git submodule update --init --recursive

[[email protected] YouCompleteMe]$git submodule update --init --recursive
           

等此指令更新完成後,再此執行指令:

./install.sh --clang-completer

[zhu[email protected] YouCompleteMe]$ ./install.sh --clang-complete
           

安裝完成後,進行一些簡單的配置就可以使用了。

YouCompleteMe進行補全時需要查找一個 ycm_global_ycm_extra_conf檔案。可以每次在工作目錄中放置這個檔案,也可以設定全局。全局設定要在.vimrc中添加一行即可。

注:.ycm_extra_conf.py 是個隐藏檔案,路徑在~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py

let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py '
           

 關于.vimrc的配置就不貼了,網上有很多,可以根據個人的喜好進行配置。

轉載于:https://www.cnblogs.com/lwenwen/p/4364014.html