1. 程式人生 > >Vim-安裝 YouCompleteMe

Vim-安裝 YouCompleteMe

process gic 頭文件 anti fig filename comm .mm connect

轉自:Vim安裝YouCompleteMe插件。

安裝前的準備

1.首先必須要保證Vim的版本至少是7.3584,並且支持python2腳本。

在vim中輸入:version 來查看版本,如果版本低於7.3.584,那麽就需要重裝vim。 直接在終端中輸入python就可查看自己的python版本號。

2.安裝vundle插件

首先保證在用戶目錄下有.vim文件夾和.vimrc文件,沒有就新建。

[cpp] view plain copy print?
  1. <span style="font-size:18px;">cd
  2. mkdir .vim
  3. vim .vimrc</span>

安裝git

Ubuntu下直接輸入以下代碼即可: [cpp] view plain copy print?
  1. <span style="font-size:18px;">sudo apt-get install git</span>
使用如下命令,查看git版本 [cpp] view plain copy print?
  1. <span style="font-size:18px;">git --version</span>

若終端裏輸出版本信息則說明安裝成功。

使用git安裝Vundle

[cpp] view plain copy
print?
  1. <span style="font-size:18px;">$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim</span>

修改配置文件

以下是官方配置文件,根據修改即可。 [cpp] view plain copy print?
  1. <span style="font-size:18px;">set nocompatible " be iMproved, required
  2. filetype off " required
  3. " set the runtime path to include Vundle and initialize
  4. set rtp+=~/.vim/bundle/Vundle.vim
  5. call vundle#begin()
  6. " alternatively, pass a path where Vundle should install plugins
  7. "call vundle#begin(‘~/some/path/here‘)
  8. " let Vundle manage Vundle, required
  9. Plugin ‘VundleVim/Vundle.vim‘
  10. " The following are examples of different formats supported.
  11. " Keep Plugin commands between vundle#begin/end.
  12. " plugin on GitHub repo
  13. Plugin ‘tpope/vim-fugitive‘
  14. " plugin from http://vim-scripts.org/vim/scripts.html
  15. Plugin ‘L9‘
  16. " Git plugin not hosted on GitHub
  17. Plugin ‘git://git.wincent.com/command-t.git‘
  18. " git repos on your local machine (i.e. when working on your own plugin)
  19. Plugin ‘file:///home/gmarik/path/to/plugin‘
  20. " The sparkup vim script is in a subdirectory of this repo called vim.
  21. " Pass the path to set the runtimepath properly.
  22. Plugin ‘rstacruz/sparkup‘, {‘rtp‘: ‘vim/‘}
  23. " Install L9 and avoid a Naming conflict if you‘ve already installed a
  24. " different version somewhere else.
  25. Plugin ‘ascenator/L9‘, {‘name‘: ‘newL9‘}
  26. " All of your Plugins must be added before the following line
  27. call vundle#end() " required
  28. filetype plugin indent on " required
  29. " To ignore plugin indent changes, instead use:
  30. "filetype plugin on
  31. "
  32. " Brief help
  33. " :PluginList - lists configured plugins
  34. " :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
  35. " :PluginSearch foo - searches for foo; append `!` to refresh local cache
  36. " :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
  37. "
  38. " see :h vundle for more details or wiki for FAQ
  39. " Put your non-Plugin stuff after this line</span>

下面是我的配置文件 [cpp] view plain copy print?
  1. <span style="font-size:18px;">""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  2. set nocompatible " be iMproved, required
  3. filetype off " required
  4. "設置Vundle的運行路徑並初始化
  5. set rtp+=~/.vim/bundle/Vundle.vim
  6. call vundle#begin()
  7. " Vundle安裝位置與插件路徑不同時,需要Vundle插件的路徑
  8. "call vundle#begin(‘~/some/path/here‘)
  9. "------------------要安裝的插件不能寫在此行前!------------------
  10. "Vundle對自己的調用,不可刪去
  11. Plugin ‘VundleVim/Vundle.vim‘
  12. "以下是所支持的各種不同格式的示例
  13. "需要安裝的插件應寫在調用的vundle#begin和vundle#end之間
  14. "如果插件托管在Github上,寫在下方,只寫作者名/項目名就行了
  15. Plugin ‘Valloric/YouCompleteMe‘
  16. Plugin ‘majutsushi/tagbar‘
  17. Plugin ‘vim-syntastic/syntastic‘
  18. Plugin ‘vim-airline/vim-airline-themes‘
  19. Plugin ‘vim-airline/vim-airline‘
  20. "如果插件來自vim-scripts(官方),寫插件名就行了
  21. " Plugin ‘L9‘
  22. "如果Git倉庫不在Github上,需要提供完整的鏈接
  23. " Plugin ‘git://git.wincent.com/command-t.git‘
  24. "本地的插件需要提供文件路徑
  25. " Plugin ‘file:///home/gmarik/path/to/plugin‘
  26. "一定要確保插件就在提供路徑的文件夾中(沒有子文件夾,直接在這層目錄下)
  27. "運行時目錄的路徑
  28. "Plugin ‘rstacruz/sparkup‘, {‘rtp‘: ‘vim/‘}
  29. "避免插件間的命名沖突
  30. "Plugin ‘ascenator/L9‘, {‘name‘: ‘newL9‘}
  31. "------------------要安裝的插件不能寫在此行後!------------------
  32. call vundle#end() " required
  33. filetype plugin indent on " required
  34. "要忽略插件縮進更改,請改用:
  35. "filetype plugin on
  36. "
  37. " 安裝插件的相關指令
  38. ":PluginList - 列出已安裝插件
  39. ":PluginInstall - 安裝新添加的插件;添加`!`或使用`:PluginUpdate`來更新已安裝插件
  40. ":PluginSearch xxx - 尋找名字帶有xxx的插件;添加`!`刷新本地緩存
  41. ":PluginClean - 刪除已經從列表移除的插件;添加`!`靜默卸載
  42. ":h - 幫助和說明文檔
  43. "Vundle的設置到此為止了
  44. "</span>


安裝完成之後在vim中執行 [cpp] view plain copy print?
  1. :PluginInstall
等待安裝即可(安裝時間視網速而定完成後會有Done!提示)如圖所示: 技術分享

安裝開始

1.Ubuntu快速安裝

下載完成後檢查倉庫的完整性

切換到YouCompleteMe目錄下,執行以下命令: [cpp] view plain copy print?
  1. git submodule update --init --recursive
[cpp] view plain copy print?
  1. <pre code_snippet_id="2320416" snippet_file_name="blog_20170408_8_6616764"></pre>
  2. <pre></pre>
  3. <pre></pre>
  4. <pre></pre>

安裝編譯YouCompleteMe的必要插件

YouCompleteMe需要編譯之後才能使用所以接下來需要編譯 確保安裝Cmake,和一些Python頭文件,如果沒有安裝執行下面的語句 [cpp] view plain copy print?
  1. sudo apt-get install build-essential cmake
  2. sudo apt-get install python-dev python3-dev
不需要語義補全: [cpp] view plain copy print?
  1. cd ~/.vim/bundle/YouCompleteMe
  2. ./install.py
需要語義補全的: [cpp] view plain copy print?
  1. cd ~/.vim/bundle/YouCompleteMe
  2. ./install.py --clang-completer
過程有點漫長,期間可以看個電影放松一下技術分享

配置YCM

打開.vimrc文件加上 [cpp] view plain copy print?
  1. "YouCompleteMe 插件配置
  2. let g:ycm_global_ycm_extra_conf=‘~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py‘
  3. nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>
  4. let g:ycm_python_binary_path = ‘/usr/bin/python3‘
  5. nmap<C-a> :YcmCompleter FixIt<CR>
然後修改.ycm_extra_conf.py [cpp] view plain copy print?
  1. vim .vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py
在這個文件中加入 [cpp] view plain copy print?
  1. ‘-isystem‘,
  2. ‘/usr/include‘,
  3. ‘-isystem‘,
  4. ‘/usr/include/c++/5.4.0‘,
  5. ‘-isystem‘,
  6. ‘/usr/include‘,
  7. ‘/usr/include/x86_64-linux-gnu/c++‘,
註意以上的路徑都是自己的路徑,參照自己的路徑修改即可。
以下是我的配置文件 [cpp] view plain copy print?
  1. # This file is NOT licensed under the GPLv3, which is the license for the rest
  2. # of YouCompleteMe.
  3. #
  4. # Here‘s the license text for this file:
  5. #
  6. # This is free and unencumbered software released into the public domain.
  7. #
  8. # Anyone is free to copy, modify, publish, use, compile, sell, or
  9. # distribute this software, either in source code form or as a compiled
  10. # binary, for any purpose, commercial or non-commercial, and by any
  11. # means.
  12. #
  13. # In jurisdictions that recognize copyright laws, the author or authors
  14. # of this software dedicate any and all copyright interest in the
  15. # software to the public domain. We make this dedication for the benefit
  16. # of the public at large and to the detriment of our heirs and
  17. # successors. We intend this dedication to be an overt act of
  18. # relinquishment in perpetuity of all present and future rights to this
  19. # software under copyright law.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  25. # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  26. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. # OTHER DEALINGS IN THE SOFTWARE.
  28. #
  29. # For more information, please refer to <http://unlicense.org/>
  30. import os
  31. import ycm_core
  32. # These are the compilation flags that will be used in case there‘s no
  33. # compilation database set (by default, one is not set).
  34. # CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
  35. flags = [
  36. ‘-Wall‘,
  37. ‘-Wextra‘,
  38. ‘-Werror‘,
  39. ‘-Wno-long-long‘,
  40. ‘-Wno-variadic-macros‘,
  41. ‘-fexceptions‘,
  42. ‘-DNDEBUG‘,
  43. # You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM
  44. # source code needs it.
  45. ‘-DUSE_CLANG_COMPLETER‘,
  46. # THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won‘t know which
  47. # language to use when compiling headers. So it will guess. Badly. So C++
  48. # headers will be compiled as C headers. You don‘t want that so ALWAYS specify
  49. # a "-std=<something>".
  50. # For a C project, you would set this to something like ‘c99‘ instead of
  51. # ‘c++11‘.
  52. ‘-std=c++11‘,
  53. # ...and the same thing goes for the magic -x option which specifies the
  54. # language that the files to be compiled are written in. This is mostly
  55. # relevant for c++ headers.
  56. # For a C project, you would set this to ‘c‘ instead of ‘c++‘.
  57. ‘-x‘,
  58. ‘c++‘,
  59. ‘-isystem‘,
  60. ‘../BoostParts‘,
  61. ‘-isystem‘,
  62. # This path will only work on OS X, but extra paths that don‘t exist are not
  63. # harmful
  64. ‘/System/Library/Frameworks/Python.framework/Headers‘,
  65. #‘-isystem‘,
  66. #‘../llvm/include‘,
  67. #‘-isystem‘,
  68. #‘../llvm/tools/clang/include‘,
  69. #‘-I‘,
  70. #‘.‘,
  71. #‘-I‘,
  72. #‘./ClangCompleter‘,
  73. #‘-isystem‘,
  74. #‘./tests/gmock/gtest‘,
  75. #‘-isystem‘,
  76. #‘./tests/gmock/gtest/include‘,
  77. #‘-isystem‘,
  78. #‘./tests/gmock‘,
  79. #‘-isystem‘,
  80. #‘./tests/gmock/include‘,
  81. ‘-isystem‘,
  82. ‘/usr/include‘,
  83. ‘-isystem‘,
  84. ‘/usr/include/c++/5.4.0‘,
  85. ‘-isystem‘,
  86. ‘/usr/include‘,
  87. ‘/usr/include/x86_64-linux-gnu/c++‘,
  88. ]
  89. # Set this to the absolute path to the folder (NOT the file!) containing the
  90. # compile_commands.json file to use that instead of ‘flags‘. See here for
  91. # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
  92. #
  93. # You can get CMake to generate this file for you by adding:
  94. # set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
  95. # to your CMakeLists.txt file.
  96. #
  97. # Most projects will NOT need to set this to anything; you can just change the
  98. # ‘flags‘ list of compilation flags. Notice that YCM itself uses that approach.
  99. compilation_database_folder = ‘‘
  100. if os.path.exists( compilation_database_folder ):
  101. database = ycm_core.CompilationDatabase( compilation_database_folder )
  102. else:
  103. database = None
  104. SOURCE_EXTENSIONS = [ ‘.cpp‘, ‘.cxx‘, ‘.cc‘, ‘.c‘, ‘.m‘, ‘.mm‘ ]
  105. def DirectoryOfThisScript():
  106. return os.path.dirname( os.path.abspath( __file__ ) )
  107. def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  108. if not working_directory:
  109. return list( flags )
  110. new_flags = []
  111. make_next_absolute = False
  112. path_flags = [ ‘-isystem‘, ‘-I‘, ‘-iquote‘, ‘--sysroot=‘ ]
  113. for flag in flags:
  114. new_flag = flag
  115. if make_next_absolute:
  116. make_next_absolute = False
  117. if not flag.startswith( ‘/‘ ):
  118. new_flag = os.path.join( working_directory, flag )
  119. for path_flag in path_flags:
  120. if flag == path_flag:
  121. make_next_absolute = True
  122. break
  123. if flag.startswith( path_flag ):
  124. path = flag[ len( path_flag ): ]
  125. new_flag = path_flag + os.path.join( working_directory, path )
  126. break
  127. if new_flag:
  128. new_flags.append( new_flag )
  129. return new_flags
  130. def IsHeaderFile( filename ):
  131. extension = os.path.splitext( filename )[ 1 ]
  132. return extension in [ ‘.h‘, ‘.hxx‘, ‘.hpp‘, ‘.hh‘ ]
  133. def GetCompilationInfoForFile( filename ):
  134. # The compilation_commands.json file generated by CMake does not have entries
  135. # for header files. So we do our best by asking the db for flags for a
  136. # corresponding source file, if any. If one exists, the flags for that file
  137. # should be good enough.
  138. if IsHeaderFile( filename ):
  139. basename = os.path.splitext( filename )[ 0 ]
  140. for extension in SOURCE_EXTENSIONS:
  141. replacement_file = basename + extension
  142. if os.path.exists( replacement_file ):
  143. compilation_info = database.GetCompilationInfoForFile(
  144. replacement_file )
  145. if compilation_info.compiler_flags_:
  146. return compilation_info
  147. return None
  148. return database.GetCompilationInfoForFile( filename )
  149. def FlagsForFile( filename, **kwargs ):
  150. if database:
  151. # Bear in mind that compilation_info.compiler_flags_ does NOT return a
  152. # python list, but a "list-like" StringVec object
  153. compilation_info = GetCompilationInfoForFile( filename )
  154. if not compilation_info:
  155. return None
  156. final_flags = MakeRelativePathsInFlagsAbsolute(
  157. compilation_info.compiler_flags_,
  158. compilation_info.compiler_working_dir_ )
  159. # NOTE: This is just for YouCompleteMe; it‘s highly likely that your project
  160. # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
  161. # ycm_extra_conf IF YOU‘RE NOT 100% SURE YOU NEED IT.
  162. try:
  163. final_flags.remove( ‘-stdlib=libc++‘ )
  164. except ValueError:
  165. pass
  166. else:
  167. relative_to = DirectoryOfThisScript()
  168. final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
  169. return { ‘flags‘: final_flags }

2.完全安裝

未完待續

結束

Vim-安裝 YouCompleteMe