最近剛裝了新系統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插件
步驟一:
[zhupengfei@localhost ~]$ git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle^C
步驟二: 在.vimrc中配置
[zhupengfei@localhost ~]$ 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 進行自動安裝。
進程如下,+號表示已經安裝,>表示正在安裝。
[zhupengfei@localhost ~]$ vim
:BundleInstall
安裝時有個錯誤,這是正常的,因為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++的補全
[zhupengfei@localhost 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
[zhupengfei@localhost YouCompleteMe]$git submodule update --init --recursive
等此命令更新完成后,再此執行命令:
./install.sh --clang-completer
[zhupengfei@localhost 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的配置就不貼了,網上有很多,可以根據個人的喜好進行配置。