YouCompleteMe (YCM) 被譽為 Vim 最強自動補全引擎,支援語義級補全、跳轉定義、錯誤診斷。本文記錄從 Vim 編譯、Vundle 管理、YCM 安裝編譯到專案配置的完整流程。
YCM 要求 Vim 編譯支援 Python 2.6+。建議重新編譯 Vim:
./configure --with-features=huge \
--enable-pythoninterp \
--enable-python3interp \
--enable-luainterp \
--enable-multibyte \
--enable-cscope \
--prefix=/usr
make && sudo make install
註:若系統 Python 3 版本較新,--enable-python3interp 可能失敗,但 YCM 僅需 Python 2 即可運行。
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'
Plugin 'easymotion/vim-easymotion'
Plugin 'scrooloose/nerdtree'
" YouCompleteMe
Plugin 'ycm-core/YouCompleteMe'
call vundle#end()
filetype plugin indent on
:PluginInstall 等待完成cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer
# 或完整參數:--clang-completer --go-completer --rust-completer 等
編譯需安裝cmake、build-essential、python-dev、clang等開發工具。
YCM 需專案配置檔提供編譯旗標。預設模板位於:
~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py
" .vimrc 中添加
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_collect_identifiers_from_tag_files = 1 " 利用 ctags 補全
" 快捷鍵:跳轉定義
nnoremap jd :YcmCompleter GoToDefinitionElseDeclaration
:YcmDiags 顯示語法錯誤診斷
:version 確認 +python/+python3~/.vim/bundle/YouCompleteMe/install.log.ycm_extra_conf.py 或正確設定 g:ycm_global_ycm_extra_conf