zoukankan      html  css  js  c++  java
  • Vim-安装 YouCompleteMe

    转自:Vim安装YouCompleteMe插件

    安装前的准备

    1.首先必须要保证Vim的版本至少是7.3584,并且支持python2脚本。

    在vim中输入:version 来查看版本,如果版本低于7.3.584,那么就需要重装vim。
    直接在终端中输入python就可查看自己的python版本号。

    2.安装vundle插件

    首先保证在用户目录下有.vim文件夹和.vimrc文件,没有就新建。

    1. <span style="font-size:18px;">cd   
    2. mkdir .vim  
    3. vim .vimrc</span>  

    安装git

    Ubuntu下直接输入以下代码即可:
    1. <span style="font-size:18px;">sudo apt-get install git</span>  
    使用如下命令,查看git版本
    1. <span style="font-size:18px;">git --version</span>  

    若终端里输出版本信息则说明安装成功。

    使用git安装Vundle

    1. <span style="font-size:18px;">$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim</span>  

    修改配置文件

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

    下面是我的配置文件
    1. <span style="font-size:18px;">""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""  
    2.   
    3. set nocompatible              " be iMproved, required  
    4. filetype off                  " required  
    5. "设置Vundle的运行路径并初始化  
    6. set rtp+=~/.vim/bundle/Vundle.vim  
    7. call vundle#begin()  
    8. " Vundle安装位置与插件路径不同时,需要Vundle插件的路径  
    9. "call vundle#begin('~/some/path/here')  
    10. "------------------要安装的插件不能写在此行前!------------------  
    11.   
    12. "Vundle对自己的调用,不可删去  
    13. Plugin 'VundleVim/Vundle.vim'  
    14.   
    15. "以下是所支持的各种不同格式的示例  
    16. "需要安装的插件应写在调用的vundle#begin和vundle#end之间  
    17.   
    18. "如果插件托管在Github上,写在下方,只写作者名/项目名就行了  
    19.   
    20. Plugin 'Valloric/YouCompleteMe'  
    21. Plugin 'majutsushi/tagbar'  
    22. Plugin 'vim-syntastic/syntastic'  
    23. Plugin 'vim-airline/vim-airline-themes'  
    24. Plugin 'vim-airline/vim-airline'  
    25.   
    26. "如果插件来自vim-scripts(官方),写插件名就行了  
    27. " Plugin 'L9'  
    28.   
    29. "如果Git仓库不在Github上,需要提供完整的链接  
    30. " Plugin 'git://git.wincent.com/command-t.git'  
    31.   
    32. "本地的插件需要提供文件路径  
    33. " Plugin 'file:///home/gmarik/path/to/plugin'  
    34. "一定要确保插件就在提供路径的文件夹中(没有子文件夹,直接在这层目录下)  
    35.   
    36. "运行时目录的路径  
    37. "Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}  
    38.   
    39. "避免插件间的命名冲突  
    40. "Plugin 'ascenator/L9', {'name': 'newL9'}  
    41.   
    42. "------------------要安装的插件不能写在此行后!------------------  
    43. call vundle#end()            " required  
    44. filetype plugin indent on    " required  
    45. "要忽略插件缩进更改,请改用:  
    46. "filetype plugin on  
    47. "  
    48. " 安装插件的相关指令  
    49. ":PluginList            - 列出已安装插件  
    50. ":PluginInstall         - 安装新添加的插件;添加`!`或使用`:PluginUpdate`来更新已安装插件  
    51. ":PluginSearch xxx      - 寻找名字带有xxx的插件;添加`!`刷新本地缓存  
    52. ":PluginClean           - 删除已经从列表移除的插件;添加`!`静默卸载  
    53. ":h                     - 帮助和说明文档   
    54. "Vundle的设置到此为止了  
    55. "</span>  


    安装完成之后在vim中执行
    1. :PluginInstall  
    等待安装即可(安装时间视网速而定完成后会有Done!提示)如图所示:

    安装开始

    1.Ubuntu快速安装

    下载完成后检查仓库的完整性

    切换到YouCompleteMe目录下,执行以下命令:
    1. git submodule update --init --recursive  
    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头文件,如果没有安装执行下面的语句
    1. sudo apt-get install build-essential cmake  
    2. sudo apt-get install python-dev python3-dev  
    不需要语义补全:
    1. cd ~/.vim/bundle/YouCompleteMe  
    2. ./install.py  
    需要语义补全的:
    1. cd ~/.vim/bundle/YouCompleteMe  
    2. ./install.py --clang-completer  
    过程有点漫长,期间可以看个电影放松一下吐舌头
     

    配置YCM

    打开.vimrc文件加上
    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 
    1. vim .vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py   
    在这个文件中加入
    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++',  
    注意以上的路径都是自己的路径,参照自己的路径修改即可。
    以下是我的配置文件
    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.   
    31. import os  
    32. import ycm_core  
    33.   
    34. # These are the compilation flags that will be used in case there's no  
    35. # compilation database set (by default, one is not set).  
    36. # CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.  
    37. flags = [  
    38. '-Wall',  
    39. '-Wextra',  
    40. '-Werror',  
    41. '-Wno-long-long',  
    42. '-Wno-variadic-macros',  
    43. '-fexceptions',  
    44. '-DNDEBUG',  
    45. # You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM  
    46. # source code needs it.  
    47. '-DUSE_CLANG_COMPLETER',  
    48. # THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which  
    49. # language to use when compiling headers. So it will guess. Badly. So C++  
    50. # headers will be compiled as C headers. You don't want that so ALWAYS specify  
    51. # a "-std=<something>".  
    52. # For a C project, you would set this to something like 'c99' instead of  
    53. # 'c++11'.  
    54. '-std=c++11',  
    55. # ...and the same thing goes for the magic -x option which specifies the  
    56. # language that the files to be compiled are written in. This is mostly  
    57. # relevant for c++ headers.  
    58. # For a C project, you would set this to 'c' instead of 'c++'.  
    59. '-x',  
    60. 'c++',  
    61. '-isystem',  
    62. '../BoostParts',  
    63. '-isystem',  
    64. # This path will only work on OS X, but extra paths that don't exist are not  
    65. # harmful  
    66. '/System/Library/Frameworks/Python.framework/Headers',  
    67. #'-isystem',  
    68. #'../llvm/include',  
    69. #'-isystem',  
    70. #'../llvm/tools/clang/include',  
    71. #'-I',  
    72. #'.',  
    73. #'-I',  
    74. #'./ClangCompleter',  
    75. #'-isystem',  
    76. #'./tests/gmock/gtest',  
    77. #'-isystem',  
    78. #'./tests/gmock/gtest/include',  
    79. #'-isystem',  
    80. #'./tests/gmock',  
    81. #'-isystem',  
    82. #'./tests/gmock/include',  
    83.  '-isystem',  
    84.  '/usr/include',  
    85.  '-isystem',  
    86.  '/usr/include/c++/5.4.0',  
    87.  '-isystem',  
    88.  '/usr/include',  
    89.  '/usr/include/x86_64-linux-gnu/c++',  
    90. ]  
    91.   
    92.   
    93. # Set this to the absolute path to the folder (NOT the file!) containing the  
    94. # compile_commands.json file to use that instead of 'flags'. See here for  
    95. # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html  
    96. #  
    97. # You can get CMake to generate this file for you by adding:  
    98. #   set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )  
    99. # to your CMakeLists.txt file.  
    100. #  
    101. # Most projects will NOT need to set this to anything; you can just change the  
    102. # 'flags' list of compilation flags. Notice that YCM itself uses that approach.  
    103. compilation_database_folder = ''  
    104.   
    105. if os.path.exists( compilation_database_folder ):  
    106.   database = ycm_core.CompilationDatabase( compilation_database_folder )  
    107. else:  
    108.   database = None  
    109.   
    110. SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]  
    111.   
    112. def DirectoryOfThisScript():  
    113.   return os.path.dirname( os.path.abspath( __file__ ) )  
    114.   
    115.   
    116. def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):  
    117.   if not working_directory:  
    118.     return list( flags )  
    119.   new_flags = []  
    120.   make_next_absolute = False  
    121.   path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]  
    122.   for flag in flags:  
    123.     new_flag = flag  
    124.   
    125.     if make_next_absolute:  
    126.       make_next_absolute = False  
    127.       if not flag.startswith( '/' ):  
    128.         new_flag = os.path.join( working_directory, flag )  
    129.   
    130.     for path_flag in path_flags:  
    131.       if flag == path_flag:  
    132.         make_next_absolute = True  
    133.         break  
    134.   
    135.       if flag.startswith( path_flag ):  
    136.         path = flag[ len( path_flag ): ]  
    137.         new_flag = path_flag + os.path.join( working_directory, path )  
    138.         break  
    139.   
    140.     if new_flag:  
    141.       new_flags.append( new_flag )  
    142.   return new_flags  
    143.   
    144.   
    145. def IsHeaderFile( filename ):  
    146.   extension = os.path.splitext( filename )[ 1 ]  
    147.   return extension in [ '.h', '.hxx', '.hpp', '.hh' ]  
    148.   
    149.   
    150. def GetCompilationInfoForFile( filename ):  
    151.   # The compilation_commands.json file generated by CMake does not have entries  
    152.   # for header files. So we do our best by asking the db for flags for a  
    153.   # corresponding source file, if any. If one exists, the flags for that file  
    154.   # should be good enough.  
    155.   if IsHeaderFile( filename ):  
    156.     basename = os.path.splitext( filename )[ 0 ]  
    157.     for extension in SOURCE_EXTENSIONS:  
    158.       replacement_file = basename + extension  
    159.       if os.path.exists( replacement_file ):  
    160.         compilation_info = database.GetCompilationInfoForFile(  
    161.           replacement_file )  
    162.         if compilation_info.compiler_flags_:  
    163.           return compilation_info  
    164.     return None  
    165.   return database.GetCompilationInfoForFile( filename )  
    166.   
    167.   
    168. def FlagsForFile( filename, **kwargs ):  
    169.   if database:  
    170.     # Bear in mind that compilation_info.compiler_flags_ does NOT return a  
    171.     # python list, but a "list-like" StringVec object  
    172.     compilation_info = GetCompilationInfoForFile( filename )  
    173.     if not compilation_info:  
    174.       return None  
    175.   
    176.     final_flags = MakeRelativePathsInFlagsAbsolute(  
    177.       compilation_info.compiler_flags_,  
    178.       compilation_info.compiler_working_dir_ )  
    179.   
    180.     # NOTE: This is just for YouCompleteMe; it's highly likely that your project  
    181.     # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR  
    182.     # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.  
    183.     try:  
    184.       final_flags.remove( '-stdlib=libc++' )  
    185.     except ValueError:  
    186.       pass  
    187.   else:  
    188.     relative_to = DirectoryOfThisScript()  
    189.     final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )  
    190.   
    191.   return { 'flags': final_flags }  

    2.完全安装

    未完待续

    结束

  • 相关阅读:
    关于在函数中返回动态的内存
    C与C++中的const
    strcat函数的坑点
    面试题30.最小的k个数
    面试题29.数组中出现次数超过一半的数字
    面试题28.字符串的排列
    面试题27.二叉搜索树与双向链表
    C++中构造函数初始化成员列表总结
    Oracle merge into
    检索 COM 类工厂中 CLSID 解决办法
  • 原文地址:https://www.cnblogs.com/JohnABC/p/7376322.html
Copyright © 2011-2022 走看看