ubuntu下安装自动补全YouCompleteMe

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

apt install vim-nox-py2 curl gitpython

#安装python头文件c++

apt install python-dev python3-devgit

#安装c/c++编译包github

apt install build-essentialweb

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

#注意:clang不要提早安装,若是已经安装了,最好卸载,由于YouCompleteMe会自动下载指定版本的clang,c#

#若是,提早安装了clang,可能会形成版本冲突。缓存

apt install cmakepython2.7

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

一、下载Vundle到制定目录

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

二、编写适用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
" 将你本身对非插件片断放在这行以后

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

3、安装YouCompleteMe

一、在~/.vim/vimrc中添加
Plugin 'Valloric/YouCompleteMe'
二、退出后,运行vim,并在命令行模式中运行:
:PluginInstall
 
4、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未编译,则说明安装已经成功了。

5、配置

不一样于不少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.

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

6、

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。
相关文章
相关标签/搜索