VIM自動補全神器 — YouCompleteMe 安裝全教程

lm_y發表於2017-09-04


轉載地址:  http://blog.csdn.net/mr_zing/article/details/44263385


VIM自動補全神器 — YouCompleteMe 安裝全教程

前言

幾天前覺得YCM在補全類成員的時候會變卡,就決定重灌一次。結果很美滿,但過程很痛苦。官方文件很多其他細節沒有說清楚(或者是我沒看清楚?),網路上很多教程都不太適合我,還有許多紕漏,在此記錄一下本人安裝的過程,希望對大家有幫助。

轉載請註明原作者(Mr_Zing)及出處。


最基本的準備

  • linux系統(本機UBUNTU 14.04)
  • Vim 7.3以上版本(本機Vim 7.4)
  • git
  • cmake
  • 網際網路

Ubuntu系統可以通過以下命令安裝 vim , Git , cmake

sudo apt-get install vim
sudo apt-get install git
sudo apt-get install cmake
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

其他系統的安裝方法請自行搜尋


接下來安裝

  • LLVM-Clang 3.3
  • Clang 標準庫

安裝LLVM-Clang 3.3

注意:不要跳過任何一步,除非你可以確定已經安裝或者不需要。

建立目錄

mkdir ~/llvm-clang
  • 1
  • 1

下載
點選連結,放到剛建立的llvm-clang資料夾中
- llvm-3.3原始碼
- clang-3.3原始碼
- clang-tools-extra-3.3原始碼
- compiler-rt-3.3原始碼

解壓
終端工作目錄在llvm-clang/

tar -xvzf llvm-3.3.src.tar.gz
tar -xvzf compiler-rt-3.3.src.tar.gz
tar -xvzf clang-tools-extra-3.3.src.tar.gz
tar -xvzf cfe-3.3.src.tar.gz
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

移動
為了可以一起編譯

mv cfe-3.3.src/ llvm-3.3.src/tools/clang/
mv clang-tools-extra-3.3.src/ llvm-3.3.src/tools/clang/extra/
mv compiler-rt-3.3.src/ llvm-3.3.src/projects/compiler-rt/
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

下載
LLVM、clang 及輔助庫原始碼

cd ~/llvm-clang
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd ../..
cd llvm/tools/clang/tools
svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
cd ../../../..
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
cd ..
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

建立編譯資料夾
防止汙染原始碼

cd ~/llvm-clang
mkdir llvm-build
cd llvm-build/
../llvm-3.3.src/configure --enable-optimized
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

此種配置後,llv-clang預設安裝到目錄 /usr/local/ 下, 如果想改變安裝目錄,則加上配置: –prefix=Path 來制定

開始編譯
很久…

make -j4
sudo make install
  • 1
  • 2
  • 1
  • 2

如果以後要解除安裝

cd ~/llvm-clang
sudo make uninstall
  • 1
  • 2
  • 1
  • 2


安裝Clang 標準庫

clang 的標準庫————libc++(介面層)和 libc++abi(實現層)需要安裝標頭檔案和動態連結庫(.so)*

安裝 libc++

cd ~/llvm-clang
svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
cd libcxx/lib
./buildit
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

複製.so檔案和標頭檔案

sudo cp -r ~/llvm-clang/libcxx/include/ /usr/include/c++/v1/
ln -s ~/llvm-clang/libcxx/lib/libc++.so.1.0 ~/llvm-clang/libcxx/lib/libc++.so.1
ln -s ~/llvm-clang/libcxx/lib/libc++.so.1.0 ~/llvm-clang/libcxx/lib/libc++.so
sudo cp ~/llvm-clang/libcxx/lib/libc++.so* /usr/lib/
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

複製需要管理許可權,也就是輸入密碼…

安裝 libc++abi 和 動態連結庫,以及複製…

cd  ~/llvm-clang/
svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
cd libcxxabi/lib
./buildit

sudo cp -r ~/llvm-clang/libcxxabi/include/ /usr/include/c++/v1/
ln -s ~/llvm-clang/libcxxabi/lib/libc++abi.so.1.0 ~/llvm-clang/libcxxabi/lib/libc++abi.so.1
ln -s ~/llvm-clang/libcxxabi/lib/libc++abi.so.1.0 ~/llvm-clang/libcxxabi/lib/libc++abi.so
sudo cp ~/llvm-clang/libcxxabi/lib/libc++abi.so* /usr/lib/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

至此,前期準備完畢。



開始編譯安裝 YCM

  • 通過 Vundle 或 git 安裝 YCM
  • 編譯
  • 編輯.ycm_extra_conf.py
  • 配置 .vimrc


通過 Vundle 安裝 YCM

注意:此方法簡單但不直觀,下載過程沒有及時反饋,可能讓人失去耐心(難道只有我是這樣的?)

*本段參考–百度Linux貼吧:vim智慧補全外掛YouCompleteMe新手完全攻略
作者:蘿蔔特頭*

安裝Vundle
1.將Vundle安裝到 ~/.vim/bundle/vundle

git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
  • 1
  • 1

2.編輯 .vimrc 檔案

  • 開啟
gedit ~/.vimrc
  • 1
  • 1
  • 在檔案末尾新增
""""""""""""""""""""" Vundle
set nocompatible
filetype off
set rtp+=~/.vim/bundle/vundle
call vundel#rc()
Bundle 'gmarik/vundle'
Bundle 'Valloric/YouCompleteMe'
filetype plugin indent on
""""""""""""""""""""" Vundle
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

關鍵的是其中以Bundle開頭的行,每一個Bundle代表一個vim外掛,這些省略完整URL外掛都是託管在github 上的。如果想要安裝新的外掛,在call vundle#rc()filetype plugin indent on之間新增新的Bundle外掛名稱即可。

3.開始下載安裝

:source ~/.vimrc
:BundleInstall
  • 1
  • 2
  • 1
  • 2

如果你發現,這怎麼這麼久都沒動靜,是正常的,幾十M且歪果伺服器呢。
安裝過程中,正在安裝的外掛左邊會有 > 標識,安裝完畢的是 +-
安裝結束後,會在狀態列看見Done字樣。

注意:安裝結束之後,開啟vim會出現錯誤:

ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; you need
to compile YCM before using it. Read the docs!
  • 1
  • 2
  • 1
  • 2


通過 git 安裝 YCM

本段參考– marchtea 的部落格,被Vim自動補全神器–YouCompleteMe 引用。

進入目標資料夾並下載

mkdir ~/.vim/bundle/
cd ~/.vim/bundle/
git clone --recursive https://github.com/Valloric/YouCompleteMe.git
git submodule update --init --recursive
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

說明:
1. 建立資料夾,如果已經存在,可以跳過。
2. 進入資料夾
3. 從 git 下載 YCM 檔案
4. 檢查倉庫(即資料夾)的完整性,不可跳過

下載過程挺久,但能看到螢幕一堆堆東西跑過去還是比較安心的呀!


編譯

  • 進入資料夾開始編譯
cd ~/.vim/bundle/YouCompleteMe
./install.sh --clang-completer --system-libclang
  • 1
  • 2
  • 1
  • 2
  • 說明:

    • 1.進入資料夾
    • 2.編譯。--clang-completer是用於C-Family的補全,不需要可以去掉。因為系統已經安裝好了clang,所以有--system-libclang
  • 建立資料夾並開始編譯

mkdir ~/ycm_build
cd ~/ycm_build
cmake -G "Unix Makefiles" -DUSE_SYSTEM_LIBclang=ON -DEXTERNAL_LIBCLANG_PATH=/usr/local/lib/libclang.so . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
make ycm_core
make ycm_support_libs
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 說明:
    • 1.建立編譯目錄
    • 2.進入目錄
    • 3.開始編譯
    • 4.在YouCompleteMe中生成libclang.so和ycm_core.so檔案
    • 5.生成第三個檔案ycm_client_support.so

注意: 如果這裡出錯,大概到80%時說缺少標頭檔案<clang.h>什麼的,那就是前面沒有認真安裝好。


編輯.ycm_extra_conf.py

開啟

gedit ~/.ycm_extra_conf.py
  • 1
  • 1

輸入(如果已存在就覆蓋,也可以什麼都不做)

# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here's the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>

import os
import ycm_core

# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
'-Wextra',
#'-Werror',
#'-Wc++98-compat',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-stdlib=libc++',
# 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++',
'-I',
'.',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/Library/Developer/CommandLineTools/usr/include',
'-isystem',
'/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1',
]

# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''

if os.path.exists( compilation_database_folder ):
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )

def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return list( flags )
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags

def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]

def GetCompilationInfoForFile( filename ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  if IsHeaderFile( filename ):
    basename = os.path.splitext( filename )[ 0 ]
    for extension in SOURCE_EXTENSIONS:
      replacement_file = basename + extension
      if os.path.exists( replacement_file ):
        compilation_info = database.GetCompilationInfoForFile(
          replacement_file )
        if compilation_info.compiler_flags_:
          return compilation_info
    return None
  return database.GetCompilationInfoForFile( filename )

def FlagsForFile( filename, **kwargs ):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = GetCompilationInfoForFile( filename )
    if not compilation_info:
      return None

    final_flags = MakeRelativePathsInFlagsAbsolute(
      compilation_info.compiler_flags_,
      compilation_info.compiler_working_dir_ )

    # NOTE: This is just for YouCompleteMe; it's highly likely that your project
    # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
    # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
    #try:
    #  final_flags.remove( '-stdlib=libc++' )
    #except ValueError:
    #  pass
  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return {
    'flags': final_flags,
    'do_cache': True
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164

此配置檔案摘抄自 marchtea 的部落格 , 被 Vim自動補全神器–YouCompleteMe 引用。


編輯 .vimrc

開啟

gedit ~/.vimrc
  • 1
  • 1

複製貼上到末尾

" 自動補全配置
set completeopt=longest,menu    "讓Vim的補全選單行為與一般IDE一致(參考VimTip1228)
autocmd InsertLeave * if pumvisible() == 0|pclose|endif "離開插入模式後自動關閉預覽視窗
inoremap <expr> <CR>       pumvisible() ? "\<C-y>" : "\<CR>"    "回車即選中當前項
"上下左右鍵的行為 會顯示其他資訊
inoremap <expr> <Down>     pumvisible() ? "\<C-n>" : "\<Down>"
inoremap <expr> <Up>       pumvisible() ? "\<C-p>" : "\<Up>"
inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>"
inoremap <expr> <PageUp>   pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>"

"youcompleteme  預設tab  s-tab 和自動補全衝突
"let g:ycm_key_list_select_completion=['<c-n>']
let g:ycm_key_list_select_completion = ['<Down>']
"let g:ycm_key_list_previous_completion=['<c-p>']
let g:ycm_key_list_previous_completion = ['<Up>']
let g:ycm_confirm_extra_conf=0 "關閉載入.ycm_extra_conf.py提示

let g:ycm_collect_identifiers_from_tags_files=1 " 開啟 YCM 基於標籤引擎
let g:ycm_min_num_of_chars_for_completion=2 " 從第2個鍵入字元就開始羅列匹配項
let g:ycm_cache_omnifunc=0  " 禁止快取匹配項,每次都重新生成匹配項
let g:ycm_seed_identifiers_with_syntax=1    " 語法關鍵字補全
nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>    "force recomile with syntastic
"nnoremap <leader>lo :lopen<CR> "open locationlist
"nnoremap <leader>lc :lclose<CR>    "close locationlist
inoremap <leader><leader> <C-x><C-o>

"在註釋輸入中也能補全
let g:ycm_complete_in_comments = 1
"在字串輸入中也能補全
let g:ycm_complete_in_strings = 1
"註釋和字串中的文字也會被收入補全
let g:ycm_collect_identifiers_from_comments_and_strings = 0

nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR> " 跳轉到定義處
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

後話

至此 VIM 的補全神器 YCM 安裝完成,如果需要更加全面的配置方法,可以在 YCM 的 github主頁 上找到。

主要參考

相關文章