Linux Vim 安裝 YouCompleteMe 插件 
YouCompleteMe 被譽為 Vim 自動補全神器,對提升編碼效率至關重要。本文記錄從編譯 Vim、安裝 Vundle 到編譯 YCM 核心庫的完整流程。
YCM 要求 Vim 支援 Python 2.6+。配置選項:
./configure --with-features=huge \
--enable-pythoninterp \
--enable-python3interp \
--enable-luainterp \
--enable-multibyte \
--enable-sniff \
--enable-fontset
我的機器裝有 Python 2.7.5 和 3.3,雖加了 --enable-python3interp 但未生效,暫不深究,YCM 僅需 Python 2.6+ 即可。
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
set nocompatible " be iMproved, required
filetype off " required
" 設置 runtime path 包含 Vundle 並初始化
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" 或者指定插件安裝路徑
" let path = '~/some/path/here'
" call vundle#rc(path)
" 讓 Vundle 管理 Vundle 自身
Plugin 'gmarik/vundle'
" 以下為插件示例,保持 Plugin 命令在 vundle#rc() 與 filetype plugin indent on 之間
" GitHub 倉庫腳本
Plugin 'tpope/vim-fugitive'
Plugin 'Lokaltog/vim-easymotion'
Plugin 'tpope/vim-rails.git'
" 子目錄腳本需指定 rtp
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" vim-scripts.org 腳本
Plugin 'L9'
Plugin 'FuzzyFinder'
" 非 GitHub 倉庫
Plugin 'git://git.wincent.com/command-t.git'
" 本地機器倉庫(開發自有插件時)
Plugin 'file:///home/gmarik/path/to/plugin'
filetype plugin indent on " required
" YouCompleteMe
Bundle 'Valloric/YouCompleteMe'
啟動 Vim,執行:
:BundleInstall
過程中 + 表示已安裝,> 表示正在安裝。結束時會報錯:
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!
這是正常的,因為 YCM 需手動編譯核心庫。
cd ~/.vim/bundle/YouCompleteMe
./install.sh --clang-completer
--clang-completer 參數啟用 C/C++ 語義補全。
YCM 配置文件位於 ~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py(隱藏文件)。默認使用該文件,也可複製到項目根目錄修改,打開項目文件時優先使用當前目錄配置;找不到則回退到全局配置。
在 ~/.vimrc 添加:
let mapleader = "," " leader 鍵映射為逗號
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py'
nnoremap jd :YcmCompleter GoToDefinitionElseDeclaration " 按 ,jd 跳轉到定義
let g:ycm_confirm_extra_conf = 0 " 啟動不再詢問加載配置
let g:ycm_collect_identifiers_from_tag_files = 1 " 使用 ctags 生成的 tags 文件
完成上述步驟,Vim 即擁有強大的語義級自動補全與跳轉能力,編碼體驗質變提升。