ubuntu下安裝自動補全YouCompleteMe

1、安裝預備軟件。
#vim要帶python2.7的支持,curl是下載插件必須用到的軟件,還有githtml

apt install vim-nox-py2 curl gitpython

#安裝python頭文件c++

apt install python-dev python3-devgit

#安裝c/c++編譯包github

apt install build-essentialweb

#安裝cmake,編譯YCM時候要用到。vim

#注意:clang不要提早安裝,若是已經安裝了,最好卸載,由於YouCompleteMe會自動下載指定版本的clang,c#

#若是,提早安裝了clang,可能會形成版本衝突。緩存

apt install cmakepython2.7

2、安裝vim插件管理工具Vundle

一、下載Vundle到制定目錄

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

二、編寫適用Vundle最小的配置文件:vim ~/.vim/vimrc

set nocompatible              " 去除VI一致性,必須
filetype off                  " 必須

" 設置包括vundle和初始化相關的runtime path
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" 讓vundle管理插件版本,必須
Plugin 'VundleVim/Vundle.vim'

" 如下範例用來支持不一樣格式的插件安裝.
" 請將安裝插件的命令放在vundle#begin和vundle#end之間.
" Github上的插件
" 格式爲 Plugin '用戶名/插件倉庫名'
Plugin 'tpope/vim-fugitive'
" 來自 http://vim-scripts.org/vim/scripts.html 的插件
" Plugin '插件名稱' 其實是 Plugin 'vim-scripts/插件倉庫名' 只是此處的用戶名能夠省略
Plugin 'L9'
" 由Git支持但再也不github上的插件倉庫 Plugin 'git clone 後面的地址'
Plugin 'git://git.wincent.com/command-t.git'
" 本地的Git倉庫(例如本身的插件) Plugin 'file:///+本地插件倉庫絕對路徑'
Plugin 'file:///home/gmarik/path/to/plugin'
" 插件在倉庫的子目錄中.
" 正確指定路徑用以設置runtimepath. 如下範例插件在sparkup/vim目錄下
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" 安裝L9,若是已經安裝過這個插件,可利用如下格式避免命名衝突
Plugin 'ascenator/L9', {'name': 'newL9'}

" 你的全部插件須要在下面這行以前
call vundle#end()            " 必須
filetype plugin indent on    " 必須 加載vim自帶和插件相應的語法和文件類型相關腳本
" 忽視插件改變縮進,可使用如下替代:
"filetype plugin on
"
" 簡要幫助文檔
" :PluginList       - 列出全部已配置的插件
" :PluginInstall    - 安裝插件,追加 `!` 用以更新或使用 :PluginUpdate
" :PluginSearch foo - 搜索 foo ; 追加 `!` 清除本地緩存
" :PluginClean      - 清除未使用插件,須要確認; 追加 `!` 自動批准移除未使用插件
"
" 查閱 :h vundle 獲取更多細節和wiki以及FAQ
" 將你本身對非插件片斷放在這行以後

三、安裝插件:
運行 vim 再運行 :PluginInstall
經過命令行直接安裝 vim +PluginInstall +qall
查閱 :h vundle Vimdoc 以獲取更多細節.

3、安裝YouCompleteMe

一、在~/.vim/vimrc中添加
Plugin 'Valloric/YouCompleteMe'
二、退出後,運行vim,並在命令行模式中運行:
:PluginInstall
 
4、YCM不一樣於以往其餘vim插件,YCM是一款編譯型的插件。安裝過程種會自動下載指定版本的clang,
若是有衝突,須要卸載以前安裝的clang,再進行安裝。在下載完後,須要手動編譯後才能使用。
 
cd ~/.vim/bundle/YouCompleteMe
 
./install.py --clang-completer

若是不須要c-family的補全,能夠去掉--clang-completer。若是須要c#的補全,請加上--omnisharp-completer。
正常來講,YCM會去下載clang的包,若是已經有,也能夠用系統--system-libclang。
就這樣,安裝結束。打開vim,若是沒有提示YCM未編譯,則說明安裝已經成功了。

5、配置

不一樣於不少vim插件,YCM首先須要編譯,另外還須要有配置。在vim啓動後,YCM會找尋當前路徑以及上層路徑的.ycm_extra_conf.py.在~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py中提供了默認的模板。將該文件拷貝到~/.vim/bundle/YouCompleteMe目錄

配置.ycm_extra_conf.py,我把flags增長對c++相關目錄的配置,我把針對OS X的配置刪除了。

下面是flags的配置部分(注意結尾也要有逗號):

flags = [
'-Wall',
'-Wextra',
'-Werror',
'-Wc++98-compat',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-DNDEBUG',
# You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM
# source code needs it.
'-DUSE_CLANG_COMPLETER',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c++',
'-isystem',
'../BoostParts',
'-isystem',
'/usr/include',
'-isystem',
'/usr/include/c++/',
'-isystem',
'/usr/include/c++/5.4.0',
]

 

而後在vimrc加入該目錄:

"指定.ycm_extra_conf.py的目錄

let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'

"ctags
let g:ycm_collect_identifiers_from_tags_files = 1
let g:ycm_seed_identifiers_with_syntax = 1

"若是爲1的話,會老是提示是否加載.ycm_extra_conf.py文件

let g:ycm_confirm_extra_conf = 0

"在補全的時候默認是能夠打開補全預覽的,所謂的補全預覽就是在vim的上面展開一個小的名爲"草稿"窗口, 裏面顯示的是當前補全列表中選擇內容的完整內容預覽, 這

"功能並不實用, 由於補全列表中的內容已經至關詳細了, 忽然打開的草稿窗口只會給編輯帶來不暢感.所以這裏建議將其設置爲1來關閉預覽功能.

set completeopt-=preview
let g:ycm_add_preview_to_completeopt = 0

官方給這個上方彈出的窗口也給了詳細的解釋:

I get a weird window at the top of my file when I use the semantic engine

This is Vim's preview window. Vim uses it to show you extra information about something if such information is available. YCM provides Vim with such extra information. For instance, when you select a function in the completion list, the preview window will hold that function's prototype and the prototypes of any overloads of the function. It will stay there after you select the completion so that you can use the information about the parameters and their types to write the function call.

If you would like this window to auto-close after you select a completion string, set the g:ycm_autoclose_preview_window_after_completion option to 1 in your vimrc file. Similarly, the g:ycm_autoclose_preview_window_after_insertion option can be set to close the preview window after leaving insert mode.

If you don't want this window to ever show up, add set completeopt-=preview to your vimrc. Also make sure that the g:ycm_add_preview_to_completeopt option is set to 0.

看官們,給個贊吧,在那麼厚的文檔裏,把這一段翻出來也不容易。

6、

YCM除了提供了基本的補全功能,自動提示錯誤的功能外,還提供了相似tags的功能:

  • 跳轉到定義GoToDefinition
  • 跳轉到聲明GoToDeclaration
  • 以及二者的合體GoToDefinitionElseDeclaration

能夠在.vimrc中配置相應的快捷鍵。

nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>

另外,YCM也提供了豐富的配置選項,一樣在.vimrc中配置。具體請參考

let g:ycm_error_symbol = '>>'
let g:ycm_warning_symbol = '>*'

同時,YCM能夠打開location-list來顯示警告和錯誤的信息:YcmDiags。我的關於ycm的配置以下:

" for ycm
let g:ycm_error_symbol = '>>'
let g:ycm_warning_symbol = '>*'
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
nmap <F4> :YcmDiags<CR>
 
YCM提供的跳躍功能採用了vim的jumplist,往前跳和日後跳的快捷鍵爲Ctrl+O以及Ctrl+I。
相關文章
相關標籤/搜索