YouCompleteMe是一個十分強大的vim插件,有人說,它的代碼補全功能甚至可以媲美vs,雖然YCM的功能非常強大,但是非常難以安裝,網上雖然有許多教程,但每台ubuntu主機因為使用者的不同,系統配置也不一樣,接下來的安裝過程是本人在全新的ubuntu16.04系統上安裝的,當然也走了不少彎路,接下來本人将會一步步介紹YCM的安裝過程,本文主要參考http://www.cnblogs.com/Harley-Quinn/p/6418070.html。
- 1.安裝git
2.安裝cmakesudo apt-get install git
sudo apt-get install build-essential cmake
3.
安裝
vim
如果已經安裝過
vim
的話需要先移除
sudo apt-get remove vim vim-runtime gvim sudo apt-get remove vim-tiny vim-common vim-gui-common vim-nox
安裝依賴項
注意,
Ubuntu16.04
是
liblua5.1-dev
而不是
lua5.1-dev
sudo apt-get install libncurses5-dev libgnome2-dev \ libgnomeui-dev libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \ ibcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev \ python3-dev ruby-dev lua5.1 liblua5.1-dev libperl-dev git
-
從網上下載下傳
vim
安裝包,補充:為了簡化安裝過程,不要直接使用
apt-get
安裝
git clone https://github.com/vim/vim.git
下載下傳完畢後,接着下面的指令,一次性執行完畢
cd ~ git clone https://github.com/vim/vim.git cd vim ./configure --with-features=huge \ --enable-multibyte \ --enable-rubyinterp=yes \ --enable-pythoninterp=yes \ --with-python-config-dir=/usr/lib/python2.7/config-x86_64- linux-gnu \ --enable-python3interp=yes \ --with-python3-config-dir=/usr/lib/python3.5/config-3.5m- x86_64_linux-gnu \ --enable-perlinterp=yes \ --enable-luainterp=yes \ --enable-gui=gtk2 --enable-cscope --prefix=/usr make VIMRUNTIMEDIR=/usr/share/vim/vim80
然後直接安裝
cd ~/vim sudo make install
安裝成功後,打開
vim
,指令模式下輸入
:echo has("python") |
|
has("python3")
,結果是
1
就代表成功了。
4.Vundle
下載下傳相關資源,指令如下:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
打開
.vimrc,
沒有則直接建立一個,直接使用指令
gedit .vimrc
即可,将下面的代碼拷貝到
.vimrc
下,儲存,
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
Plugin 'Valloric/YouCompleteMe'
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted 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'
" 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/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
然後
vim
指令:
PluginInstall
由于
YouCompleteMe
有
223M
左右,是以需要耐心等待,可以時不時用指令
du -s ~/.vim/bundle/YouCompleteMe
觀察檔案夾大小。
5.
直接安裝
YCM
使用以下指令進行安裝:
cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer
安裝完畢後,将下面的代碼拷貝到
.
vimrc
下面,之前拷貝的内容可以删除,也可以繼續留着
" YouCompleteMe
set runtimepath+=~/.vim/bundle/YouCompleteMe
let g:ycm_collect_identifiers_from_tags_files = 1 " 開啟 YCM 基于标簽引擎
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注釋與字元串中的内容也用于補全
let g:syntastic_ignore_files=[".*\.py$"]
let g:ycm_seed_identifiers_with_syntax = 1 " 文法關鍵字補全
let g:ycm_complete_in_comments = 1
let g:ycm_confirm_extra_conf = 0
let g:ycm_key_list_select_completion = ['
', '
'] " 映射按鍵, 沒有這個會攔截掉tab, 導緻其他插件的tab不能用.
let g:ycm_key_list_previous_completion = ['
', '
']
let g:ycm_complete_in_comments = 1 " 在注釋輸入中也能補全
let g:ycm_complete_in_strings = 1 " 在字元串輸入中也能補全
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注釋和字元串中的文字也會被收入補全
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
let g:ycm_show_diagnostics_ui = 0 " 禁用文法檢查
inoremap
pumvisible() ? "\
" : "\
" | " 回車即選中目前項 nnoremap
:YcmCompleter GoToDefinitionElseDeclaration
| " 跳轉到定義處 "let g:ycm_min_num_of_chars_for_completion=2 " 從第2個鍵入字元就開始羅列比對項
關鍵是配置檔案
.ycm_extra_conf.py
,包含了補全的系統頭檔案和源檔案出處
gedit ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py
找到以下面代碼前四行所在的地方,并将剩下的代碼拷貝過去
'-x',
'c++',
'-isystem',
'../BoostParts',
'-isystem',
'/usr/include',
'-isystem',
'/usr/lib',
'-isystem',
'/usr/local/include',
'-isystem',
'/usr/include/c++/5',
效果如下: