ubuntu下安裝自動補全YouCompleteMe


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

apt install vim-nox-py2 curl git

#安裝python頭文件

apt install python-dev python3-dev

#安裝c/c++編譯包

apt install build-essential

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

#注意:clang不要提前安裝,如果已經安裝了,最好卸載,因為YouCompleteMe會自動下載指定版本的clang,

#如果,提前安裝了clang,可能會造成版本沖突。

apt install cmake

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

1、下載Vundle到制定目錄

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

2、編寫適用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
" 將你自己對非插件片段放在這行之后

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

三、安裝YouCompleteMe

1、在~/.vim/vimrc中添加
Plugin 'Valloric/YouCompleteMe'
2、退出后,運行vim,並在命令行模式中運行:
:PluginInstall
 
四、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未編譯,則說明安裝已經成功了。

五、配置

不同於很多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.

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

六、

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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM