YCM(YouCompleteMe)是 Vim 上最強大的程式碼補全外掛之一,支援語法補全與語意補全。在 CentOS 上安裝的關鍵在於先備妥編譯 YCM 所需的 gcc、CMake、clang/llvm 等工具,再回頭編它。
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-fugitive'
Bundle 'Valloric/YouCompleteMe'
call vundle#end()
filetype plugin indent on
:BundleInstall
yum install gcc gcc-c++ make automake python-devel
CentOS 7 自帶的 CMake 版本偏低,建議手動編譯一份:
wget https://www.cmake.org/cmake/resources/software.html
tar -zxv -f cmake-*.*.*.*.tar.gz
cd cmake-*.*.*.*
./bootstrap
gmake
gmake install
這裡直接使用官方編譯好的版本,省下大量時間:
wget https://llvm.org/releases/3.5.1/clang+llvm-3.5.1-x86_64-fedora20.tar.xz
xz -d clang+llvm-3.5.1-x86_64-fedora20.tar.xz
tar xvf clang+llvm-3.5.1-x86_64-fedora20.tar
cd clang+llvm-3.5.1-x86_64-fedora20
註:也可以直接 tar xvf clang+llvm-3.5.1-x86_64-fedora20.tar.xz 一步解開。
在 /etc/profile 末尾加上 clang 與 cmake 的路徑:
PATH=/usr/local/cmake/bin:/usr/clang_3_3/bin:$PATH
export PATH
source /etc/profile
cd ~/.vim/bundle/YouCompleteMe
./install.sh --clang-completer
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
let g:ycm_confirm_extra_conf = 0
let g:ycm_semantic_triggers = {
\ 'c' : ['->', ' ', '.', ' ', '(', '[', '&'],
\ 'cpp,objcpp' : ['->', '.', ' ', '(', '[', '&', '::'],
\ 'perl' : ['->', '::', ' '],
\ 'php' : ['->', '::', '.'],
\ 'cs,java,javascript,d,vim,python,perl6,scala,vb,elixir,go' : ['.'],
\ 'ruby' : ['.', '::'],
\ 'lua' : ['.', ':']
\ }
完成後重新開啟 Vim,輸入程式碼時就會看到補全選單跳出來了。