VIM安裝程式碼補全外掛YouCompleteMe

2020-08-13 14:22:16

一、解決下載速度過慢問題

A、修改hosts檔案

  • 開啓IP查詢網站:https://www.ipaddress.com/
  • 分別輸入網址:raw.githubusercontent.com、github.global.ssh.fastly.net、github.com。查詢IP,or:
#example:
$ nslookup github.com  #查詢IP
  • 開啓hosts檔案
$ sudo vim /etc/hosts
  • 寫入查到的IP,儲存並關閉
199.232.68.133  raw.githubusercontent.com  #vim-plug
0.0.0.0 github.global.ssh.fastly.net  #git clone
0.0.0.0 github.com  #git clone
  • 重新整理DNS
$ sudo apt-get install nscd  #安裝依賴:nscd
$ sudo /etc/init.d/nscd restart  #重新整理

B、利用「碼雲」,https://gitee.com/

二、安裝外掛管理器vim-plug

$ sudo apt install curl git  #安裝依賴:curl,git
$ git --version  #版本資訊
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

三、安裝YouCompleteMe

1、準備工作
$cd .vim  #進入.vim資料夾
$mkdir plugged  #建立外掛目錄

$vim .vimrc  #建立並編輯.vimrc
#寫入程式碼:
call plug#begin('~/.vim/plugged')
Plug 'ycm-core/YouCompleteMe'
call plug#end()
2、選擇安裝方式

A、vim-plug安裝

$ vim  #開啓vim
:PlugInstall  #SHIFT+:,進入vim命令模式,鍵入此命令
#等待安裝結束。
:PlugStatus  #檢視外掛裝態

B、clone

$ cd .vim/plugged
$ git clone https://github.com/ycm-core/YouCompleteMe.git
or
$ git clone --recursive https://github.com/Valloric/YouCompleteMe.git  #github
or
$ git clone https://gitee.com/zhuixixi/YouCompleteMe.git --depth=1  #碼雲
#拉取子模組
$ cd .vim/plugged/YouCompleteMe
$ git submodule update --init --recursive

C、從其它地方複製整個YouCompleteMe資料夾到plugged下

3、編譯
#安裝依賴:clang,cmake,python3-dev
$ sudo apt install clang cmake python3-dev
$ cd .vim/plugged/YouCompleteMe  #進入YCM目錄
#方式一:python
$ python3 install.py --clang-completer
or
$ ./install.py --clang-completer --system-libclang
#方式二:cmake
$ mkdir .ycm_build  #建立目錄:.ycm_build
$ cd .ycm_build
$ cmake -G "Unix Makefiles" . ~/.vim/plugged/YouCompleteMe/third_party/ycmd/cpp
$ cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=ON -DUSE_SYSTEM_LIBCLANG=ON . ~/.vim/plugged/YouCompleteMe/third_party/ycmd/cpp
$ cmake --build . --target ycm_corer
4、設定

A、.ycm_extra_conf.py

  • 複製.ycm_extra_conf.py檔案到主目錄
$ cp ~/.vim/plugged/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/
  • 檢視c庫地址
$ echo | clang -v -E -x c -
$ echo | clang -v -E -x c++ -
#找到如下地址:
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/backward
/usr/local/include
/usr/lib/llvm-10/lib/clang/10.0.0/include
/usr/include/x86_64-linux-gnu
/usr/include
  • 編輯.ycm_extra_conf.py,寫入C庫地址:
//flag內
'c',  //或改c++爲c
'-isystem',
'/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9',
'-isystem',
'/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9',
'-isystem',
'/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/backward',
'-isystem',
'/usr/lib/llvm-10/lib/clang/10.0.0/include',
'-isystem',
'/usr/include/x86_64-linux-gnu',

'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',

'-isystem',
'usr/include/clang/10.0.0/include',

B、.vimrc

  • 編輯.vimrc,寫入程式碼:
//YCM設定
let g:ycm_server_python_interpreter='/usr/bin/python3'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'  //全域性組態檔路徑
let g:ycm_confirm_extra_conf = 0  //關閉每次匯入組態檔前的詢問
let g:syntastic_always_populate_loc_list = 1  //方便使用syntastic進行語法檢查
let g:ycm_seed_identifiers_with_syntax=1  //開啓語法關鍵字補全
let g:ycm_min_num_of_chars_for_completion=1  //從第一個鍵入字元就開始羅列匹配項

//設定YCM語意補全自動觸發條件
let g:ycm_semantic_triggers =  {
  \   'c' : ['->', '.'],
  \   'objc' : ['->', '.', 're!\[[_a-zA-Z]+\w*\s', 're!^\s*[^\W\d]\w*\s',
  \             're!\[.*\]\s'],
  \   'ocaml' : ['.', '#'],
  \   'cpp,objcpp' : ['->', '.', '::'],
  \   'perl' : ['->'],
  \   'php' : ['->', '::'],
  \   'cs,java,javascript,typescript,d,python,perl6,scala,vb,elixir,go' : ['.'],
  \   'ruby' : ['.', '::'],
  \   'lua' : ['.', ':'],
  \   'erlang' : [':'],
  \ }
let g:ycm_semantic_triggers =  {
			\ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
			\ 'cs,lua,javascript': ['re!\w{2}'],
			\ }

4、測試

$ vim hello.c