1. 程式人生 > 實用技巧 >vim 安裝外掛

vim 安裝外掛

1 簡介

  • 外掛介紹:YouCompleteMe是一款針對Vim的快速,即用型的模糊搜尋程式碼補全引擎。包括以下幾種引擎:
    • 基於識別符號的引擎,可與每種程式語言一起使用
    • 基於Clang的引擎,為C / C ++ / Objective-C / Objective-C ++(C系列)提供本地語義程式碼補全
    • 基於Jedi的Python 2和3的補全引擎
    • 基於OmniSharp的C#補全引擎
    • Go的GocodeGodef語義引擎的組合,
    • 基於TSServer的TypeScript完成引擎,
    • 基於Tern的JavaScript補全引擎,
    • 基於racer的Rust補全引擎,
    • 基於jdt.ls的Java實驗性補全引擎。
    • 基於omnifunc的補全引擎,它使用來自Vim的omnicomplete系統的資料為許多其他語言(Ruby,PHP等)提供語義補全。
    • 基於UltiSnips的程式碼片補全引擎
  • 倉庫地址https://github.com/Valloric/YouCompleteMe

入坑vim多天後逐步裝外掛對vim進行完善,終於今天開始嘗試裝最牛外掛之一YouCompleteMe。一路也是遇到不少問題,甚至想從原始碼編譯,最後發現其實都是些小問題。

本系統為ubuntu

先上外掛網址:https://github.com/Valloric/YouCompleteMe

1、

請確保有以下依賴

sudo apt install build-essential cmake python3-dev

2、

這裡用的是 vim-plug 下載,用vundle也差不多

  1. call plug#begin('~/.vim/plugged')
  2. Plug 'Valloric/YouCompleteMe', { 'do': './install.py --clang-completer'}
  3. call plug#end()

然後漫長等待至下好,這個時候並不是結束,而是才剛剛開始

3、

cd ~/.vim/plugged/YouCompleteMe/ (就是找到安裝目錄)

一般這個時候都沒有完整克隆下來的,執行命令

git submodule update --init --recursive

好像就可以完整安裝下來

4、本文章的重點之一,就是這裡開始遇到問題的

在YouCompleteMe目錄下執行編譯安裝

  1. cd ~/.vim/bundle/YouCompleteMe
  2. ./install.py --clang-completer

然後就驚奇發現終端有如下回復

CMake Error: The source directory "/home/rivalak/.cache/vimfiles/repos/github.com/Valloric/YouCompleteMe/third_party/ycmd/third_party/cregex" does not appear to contain CMakeLists.txt.

其實這是因為 ~/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/cregex/ 的內部檔案沒有clone下來

解決方案:rm -rf ~/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/cregex (即刪除掉)

再執行git submodule update --init --recursive

然後就可以 ./install.py --clang-completer 成功編譯了

5、

然後是問題2:

開啟vim,發現YouCompleteme unavailable : no module named future

原因是 ~/.vim/plugged/YouCompleteMe/third_party/python-future/ 的內部檔案沒有clone下來

解決方案:rm -rf ~/.vim/plugged/YouCompleteMe/third_party/python-future/

再執行git submodule update --init --recursive

然後 ./install.py --clang-completer 再編譯

6、

然後是問題3:

當你認為終於搞定了高興的用vim開啟.c檔案,然後發現

No .ycm_extra_conf.py file detected, so no compile flags are available. Thus no semantic support for C/C++/ObjC/ObjC++. Go READ THE DOCS NOW, DON'T file a bug report.

居然對c和c++不能用!!!

莫方,看英文,是因為找不到.ycm_extra_conf.py 這個檔案

所以解決方法上:發現這個檔案在 ~/.vim/plugged/YouCompleteMe/third_party/ycmd/examples/ 中,先用命令

mv ~/.vim/plugged/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/plugged/YouCompleteMe/third_party/ycmd/cpp/ycm/

從而移動該檔案到~/.vim/plugged/YouCompleteMe/third_party/ycmd/cpp/ycm/ 中

然後開啟 .vimrc 檔案,加入

let g:ycm_global_ycm_extra_conf = ‘~/.vim/plugged/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py’

然後就大工告成了。

最後附上一個小提示:遇到問題可以查詢github的issues,裡面有很多dalao的解決方案

=====================================================

  • rm -rf YouCompleteMe/third_party/ycmd/third_party/cregex
  • git submodule update --init --recursive at the dir of "YouCompleteMe/third_party/ycmd/third_party"

完美解決!

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')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

Plugin 'scrooloose/nerdtree'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'Xuyuanp/nerdtree-git-plugin'

Plugin 'majutsushi/tagbar'
Plugin 'scrooloose/syntastic'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'jiangmiao/auto-pairs'
Plugin 'Valloric/YouCompleteMe'


Plugin 'tpope/vim-fugitive'
Plugin 'rstacruz/sparkup', {'rtp': '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/'}


" Pass the path to set the runtimepath properly.
" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Avoid a name conflict with L9
" Plugin 'user/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

" airline


"設定開啟vim的時候預設開啟目錄樹"
let g:nerdtree_tabs_open_on_console_startup=1
" 關閉NERDTree快捷鍵
map <C-t> :NERDTreeToggle<CR>

""當NERDTree為剩下的唯一視窗時自動關閉
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
let g:NERDTreeDirArrowExpandable = ''
let g:NERDTreeDirArrowCollapsible = ''
let NERDTreeAutoCenter=1
" 是否顯示隱藏檔案
let NERDTreeShowHidden=1
" 設定寬度
let NERDTreeWinSize=23

"let g:NERDTreeIndicatorMapCustom = {
"    \ "Modified"  : "✹",
"    \ "Staged"    : "✚",
"    \ "Untracked" : "✭",
"    \ "Renamed"   : "➜",
"    \ "Unmerged"  : "═",
"    \ "Deleted"   : "✖",
"    \ "Dirty"     : "✗",
"    \ "Clean"     : "✔︎",
"    \ "Ignored"   : "☒'",
"    \ "Unknown"   : "?"
"    \ }


"""""""""""""""""""""""""""""""""""""""
let g:airline_powerline_fonts = 1
set t_Co=256
set laststatus=2

" ycm
"let g:ycm_python_binary_path='/usr/bin/python'
let g:ycm_confirm_extra_conf=0

"let g:ycm_server_python_interpreter='/usr/bin/python'
"let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py'

let g:ycm_confirm_extra_conf=0
"let g:clang_use_library=1
set completeopt-=preview

" 不顯示開啟vim時檢查ycm_extra_conf檔案的資訊  
let g:ycm_confirm_extra_conf=0
" 開啟基於tag的補全,可以在這之後新增需要的標籤路徑  
let g:ycm_collect_identifiers_from_tags_files=1
"註釋和字串中的文字也會被收入補全
let g:ycm_collect_identifiers_from_comments_and_strings = 0
"" 輸入第2個字元開始補全
let g:ycm_min_num_of_chars_for_completion=2
" 禁止快取匹配項,每次都重新生成匹配項
let g:ycm_cache_omnifunc=0

" " 開啟語義補全
let g:ycm_seed_identifiers_with_syntax=1
" "在註釋輸入中也能補全
let g:ycm_complete_in_comments = 1
" "在字串輸入中也能補全
let g:ycm_complete_in_strings = 1
" " 設定在下面幾種格式的檔案上遮蔽ycm

let g:ycm_filetype_blacklist = {
      \ 'tagbar' : 1,
      \ 'nerdtree' : 1,
      \}


let g:ycm_key_list_select_completion = ['<Down>']
let g:ycm_key_list_previous_completion = ['<Up>']
" 修改對C函式的補全快捷鍵,預設是CTRL + space,修改為ALT + ;
let g:ycm_key_invoke_completion = '<M-;>'

"let g:ycm_add_preview_to_completeopt = 0
""let g:ycm_show_diagnostics_ui = 0
let g:ycm_server_log_level = 'info'
let g:ycm_min_num_identifier_candidate_chars = 2
let g:ycm_collect_identifiers_from_comments_and_strings = 1
let g:ycm_key_invoke_completion = '<c-z>'
"set completeopt=menu,menuone

noremap <c-z> <NOP>
let g:ycm_semantic_triggers =  {
                        \ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
                        \ 'cs,lua,javascript': ['re!\w{2}'],
                        \ }


synta on

set number
set cindent
set autoindent

set expandtab

set smarttab
" set mouse=a
set shiftwidth=4
set softtabstop=4
set hls
set bg=dark

注意:補全c++標頭檔案,或者c的標頭檔案,需要在.ycm_extra_conf.py 檔案裡修改 flags

flags = [

] 裡面加上,下面

'-isystem',
'/usr/include',
'-isystem',
'/usr/include/c++/5.4.0',