一、安装预备软件。
#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 % 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。

ubuntu下安装自动补全YouCompleteMe的更多相关文章

  1. linux-python在vim下的自动补全功能

    linux-python在vim下的自动补全功能 安装配置: wget https://github.com/rkulla/pydiction/archive/master.zipunzip -q m ...

  2. vim安装自动补全插件

    1. 先安装Pathogen,以便后续的插件安装. 打开网址https://github.com/tpope/vim-pathogen可以查看具体安装方法. a.创建目标并安装: mkdir -p ~ ...

  3. [operator]ubuntu + sublime + anaconda 自动补全、指定python版本

    ubuntu .sublime.anaconda都安装好之后,首先要解决的就是自动补全问题 Perference---->Browes Packages --->新建一个Python的文件 ...

  4. RedHat/Fedora/Centos 下bash 自动补全命令

    本文转自:运维生存时间:http://www.ttlsa.com/linux/rhel- ... matically-function/ linuser  :http://www.linuser.co ...

  5. Mac下终端自动补全功能

    记录一下终端的一些使用命令,跟自动补全的配置,主要怕以后忘记了. 1.终端自动补全的配置 打开终端,输入 : nano .inputrc 在文件里面写上: set completion-ignore- ...

  6. Python交互模式下代码自动补全

    这个功能是以lib的形式提供的,配置写到home下的.pythonrc文件中, 并设置好环境变量让python启动时执行初始化: # ~/.pythonrc # enable syntax compl ...

  7. Ubuntu vim java 自动补全javacomeplete2

    一 安装vundle $ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim 默认安装在/.v ...

  8. CentOS安装自动补全安装包

    CentOS7标准版有这个功能,但是CentOS6却没有,其实很简单: 1.安装bash-completion yum install bash-completion 2.保存一下最新的缓存 yum ...

  9. Python2.7.12开发环境构建(自动补全)

    一.安装readline-devel包 Python的编译安装依赖于这个包 yum -y install readline-devel 二.安装Python2.7.12 Python官方网站(到此处下 ...

随机推荐

  1. Birt中实现字段拆分为表的还有一种方法

    来源:     http://developer.actuate.com/community/forum/index.php? /topic/36204-split-data-row/. 将字段拆分为 ...

  2. linux 程序移植到Android

    用动态链接的方法: arm-linux-gcc hello.c -o hello.out -Wl,-dynamic-linker=/system/lib/ld-linux.so.3 并且拷贝文件到安卓 ...

  3. GPGPU OpenCL 获取设备信息

    在使用OpenCL编程中,需要对GPU设备的底层理解,这样才能更好的进行代码优化. 比如计算单元CU数量,每个CU的执行单元PE数量,每个CU中的共享内存大小等等.只有了解了这些才能更好的使用共享内存 ...

  4. Linux下读取RFID卡号(C串口编程)

    由于项目需要用到RFID.GPRS.摄像头等模块所以便看了一下,整理了一下学习思路,本篇先是整理一下串口读取RFID卡号的程序思路,后面还会更其他的 RFID模块: 本次采用的是125K的RFID读卡 ...

  5. 【SSH三大框架】Hibernate基础第十一篇:对继承映射的操作

    在java中.类之间能够有继承关系.可是在数据库中是没有继承关系的.只是Hibernate是为了把面向对象的关系反映到数据库中.Hibernate为我们提供了3种方案: 第一.一个继承体系放在一张表中 ...

  6. 用curl抓取网站数据,仿造IP、防屏蔽终极强悍解决方式

    最近在做一些抓取其它网站数据的工作,当然别人不会乖乖免费给你抓数据的,有各种防抓取的方法.不过道高一尺,魔高一丈,通过研究都是有漏洞可以钻的.下面的例子都是用PHP写的,不会用PHP来curl的孩纸先 ...

  7. TextView子类的常用属性

    TextView常见的子类包括EditText,Button,CheckBox, RadioButton等. 1.EditText EditText继承自TextView,因此TextView所有属性 ...

  8. RS布局问题之块的不完美之完美

    早上一来,便传来喜讯...说我们做的报表太美.客户不敢看----于是便开启征程,亲自尝试了一把,如下面的操作,首次运行报表,在不考虑UI美观度的情况下,报表还是 在预测范围内显示的 那么接下来我们选择 ...

  9. 【Python】理想论坛每小时发帖量统计图表

    写以下代码的目的是分析一天中各时段理想论坛中用户发帖回帖的活跃程度,获得结尾那张图表是核心. 以下代码两种爬虫协助,论坛爬虫先爬主贴,爬到主贴后启动帖子爬虫爬子贴,然后把每个子贴的发表时间等存入数据库 ...

  10. (剑指Offer)面试题7:用两个栈实现队列

    题目: 用两个栈实现一个队列. 队列的声明如下:请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 思路: 根据栈的“先进后出”特点, ...