zoukankan      html  css  js  c++  java
  • ubuntu下安装自动补全YouCompleteMe

    一、安装预备软件。
    #vim要带python2.7的支持,curl是下载插件必须用到的软件,还有git

    apt install vim-nox-py2 curl git

    #安装python头文件

    apt install python-dev python3-dev

    #安装c/c++编译包

    apt install build-essential

    #安装cmake,编译YCM时候要用到。

    #注意:clang不要提前安装,如果已经安装了,最好卸载,因为YouCompleteMe会自动下载指定版本的clang,

    #如果,提前安装了clang,可能会造成版本冲突。

    apt install cmake

    二、安装vim插件管理工具Vundle

    1、下载Vundle到制定目录

    git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

    2、编写适用Vundle最小的配置文件:vim ~/.vim/vimrc

    set nocompatible              " 去除VI一致性,必须
    filetype off                  " 必须

    " 设置包括vundle和初始化相关的runtime path
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()

    " 让vundle管理插件版本,必须
    Plugin 'VundleVim/Vundle.vim'

    " 以下范例用来支持不同格式的插件安装.
    " 请将安装插件的命令放在vundle#begin和vundle#end之间.
    " Github上的插件
    " 格式为 Plugin '用户名/插件仓库名'
    Plugin 'tpope/vim-fugitive'
    " 来自 http://vim-scripts.org/vim/scripts.html 的插件
    " Plugin '插件名称' 实际上是 Plugin 'vim-scripts/插件仓库名' 只是此处的用户名可以省略
    Plugin 'L9'
    " 由Git支持但不再github上的插件仓库 Plugin 'git clone 后面的地址'
    Plugin 'git://git.wincent.com/command-t.git'
    " 本地的Git仓库(例如自己的插件) Plugin 'file:///+本地插件仓库绝对路径'
    Plugin 'file:///home/gmarik/path/to/plugin'
    " 插件在仓库的子目录中.
    " 正确指定路径用以设置runtimepath. 以下范例插件在sparkup/vim目录下
    Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
    " 安装L9,如果已经安装过这个插件,可利用以下格式避免命名冲突
    Plugin 'ascenator/L9', {'name': 'newL9'}

    " 你的所有插件需要在下面这行之前
    call vundle#end()            " 必须
    filetype plugin indent on    " 必须 加载vim自带和插件相应的语法和文件类型相关脚本
    " 忽视插件改变缩进,可以使用以下替代:
    "filetype plugin on
    "
    " 简要帮助文档
    " :PluginList       - 列出所有已配置的插件
    " :PluginInstall    - 安装插件,追加 `!` 用以更新或使用 :PluginUpdate
    " :PluginSearch foo - 搜索 foo ; 追加 `!` 清除本地缓存
    " :PluginClean      - 清除未使用插件,需要确认; 追加 `!` 自动批准移除未使用插件
    "
    " 查阅 :h vundle 获取更多细节和wiki以及FAQ
    " 将你自己对非插件片段放在这行之后

    3、安装插件:
    运行 vim 再运行 :PluginInstall
    通过命令行直接安装 vim +PluginInstall +qall
    查阅 :h vundle Vimdoc 以获取更多细节.

    三、安装YouCompleteMe

    1、在~/.vim/vimrc中添加
    Plugin 'Valloric/YouCompleteMe'
    2、退出后,运行vim,并在命令行模式中运行:
    :PluginInstall
     
    四、YCM不同于以往其他vim插件,YCM是一款编译型的插件。安装过程种会自动下载指定版本的clang,
    如果有冲突,需要卸载之前安装的clang,再进行安装。在下载完后,需要手动编译后才能使用。
     
    cd ~/.vim/bundle/YouCompleteMe
     
    ./install.py --clang-completer

    如果不需要c-family的补全,可以去掉--clang-completer。如果需要c#的补全,请加上--omnisharp-completer。
    正常来说,YCM会去下载clang的包,如果已经有,也可以用系统--system-libclang。
    就这样,安装结束。打开vim,如果没有提示YCM未编译,则说明安装已经成功了。

    五、配置

    不同于很多vim插件,YCM首先需要编译,另外还需要有配置。在vim启动后,YCM会找寻当前路径以及上层路径的.ycm_extra_conf.py.在~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py中提供了默认的模板。将该文件拷贝到~/.vim/bundle/YouCompleteMe目录

    配置.ycm_extra_conf.py,我把flags增加对c++相关目录的配置,我把针对OS X的配置删除了。

    下面是flags的配置部分(注意结尾也要有逗号):

    flags = [
    '-Wall',
    '-Wextra',
    '-Werror',
    '-Wc++98-compat',
    '-Wno-long-long',
    '-Wno-variadic-macros',
    '-fexceptions',
    '-DNDEBUG',
    # You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM
    # source code needs it.
    '-DUSE_CLANG_COMPLETER',
    # 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++',
    '-isystem',
    '../BoostParts',
    '-isystem',
    '/usr/include',
    '-isystem',
    '/usr/include/c++/',
    '-isystem',
    '/usr/include/c++/5.4.0',
    ]

    然后在vimrc加入该目录:

    "指定.ycm_extra_conf.py的目录

    let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'

    "ctags
    let g:ycm_collect_identifiers_from_tags_files = 1
    let g:ycm_seed_identifiers_with_syntax = 1

    "如果为1的话,会总是提示是否加载.ycm_extra_conf.py文件

    let g:ycm_confirm_extra_conf = 0

    "在补全的时候默认是可以打开补全预览的,所谓的补全预览就是在vim的上面展开一个小的名为"草稿"窗口, 里面显示的是当前补全列表中选择内容的完整内容预览, 这

    "功能并不实用, 因为补全列表中的内容已经相当详细了, 突然打开的草稿窗口只会给编辑带来不顺畅感.因此这里建议将其设置为1来关闭预览功能.

    set completeopt-=preview
    let g:ycm_add_preview_to_completeopt = 0

    官方给这个上方弹出的窗口也给了详细的解释:

    I get a weird window at the top of my file when I use the semantic engine

    This is Vim's preview window. Vim uses it to show you extra information about something if such information is available. YCM provides Vim with such extra information. For instance, when you select a function in the completion list, the preview window will hold that function's prototype and the prototypes of any overloads of the function. It will stay there after you select the completion so that you can use the information about the parameters and their types to write the function call.

    If you would like this window to auto-close after you select a completion string, set the g:ycm_autoclose_preview_window_after_completion option to 1 in your vimrc file. Similarly, the g:ycm_autoclose_preview_window_after_insertion option can be set to close the preview window after leaving insert mode.

    If you don't want this window to ever show up, add set completeopt-=preview to your vimrc. Also make sure that the g:ycm_add_preview_to_completeopt option is set to 0.

    看官们,给个赞吧,在那么厚的文档里,把这一段翻出来也不容易。

    六、

    YCM除了提供了基本的补全功能,自动提示错误的功能外,还提供了类似tags的功能:

    • 跳转到定义GoToDefinition
    • 跳转到声明GoToDeclaration
    • 以及两者的合体GoToDefinitionElseDeclaration

    可以在.vimrc中配置相应的快捷键。

    nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
    nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
    nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>

    另外,YCM也提供了丰富的配置选项,同样在.vimrc中配置。具体请参考

    let g:ycm_error_symbol = '>>'
    let g:ycm_warning_symbol = '>*'

    同时,YCM可以打开location-list来显示警告和错误的信息:YcmDiags。个人关于ycm的配置如下:

    " for ycm
    let g:ycm_error_symbol = '>>'
    let g:ycm_warning_symbol = '>*'
    nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
    nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
    nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
    nmap <F4> :YcmDiags<CR>
     
    YCM提供的跳跃功能采用了vim的jumplist,往前跳和往后跳的快捷键为Ctrl+O以及Ctrl+I。
  • 相关阅读:
    Lambda表达式、解决端口占用问题
    springboot初始化报错: Failed to instantiate [XXX]: Specified class is an interface
    Spring声明式事务配置
    Springboot集成jsp
    点击redisserver.exe闪退
    Spring学习笔记
    Mybatis中 <![CDATA[ ]]> 的使用
    Mybatis学习笔记
    context:annotationconfig与context:componentscan的作用
    Spring学习笔记
  • 原文地址:https://www.cnblogs.com/litifeng/p/6671446.html
Copyright © 2011-2022 走看看