注:安装了YCM,并不代表它可以进行补全功能。单纯安装后,它只能补全在当前程序中出现过的关键字;若想要它能补全头文件中的关键字,还需要进行额外的配置,下面本文来统一说明。
一、安装YCM:
1 安装vim 和 git
这个很简单,我们只需要输入vim
,系统就会提示你进行安装,再输入第一条命令apt install vim
即可。
输入git
按照系统提示进行安装。
2 安装依赖软件
输入命令:
sudo apt-get install build-essential cmake python-dev python3-dev
3 安装Vundle
一款vim插件管理工具,需要使用 [git clone] 命令进行安装。
输入命令,这里注意我们存的目标文件位置
cd ~/.vim/bundle/Vundle.vim
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
但是不建议直接这样做,因为GitHub访问太慢。
方法一:我们需要使用Google的Github加速插件,然后将地址
https://github.com/VundleVim/Vundle.vim.git
拿出来输入浏览器地址栏中,获得加速地址,可以使用Google浏览器的应用商店搜索。
还是推荐方法二,对于GitHub地址https://github.com/VundleVim/Vundle.vim.git,我们在 [github.com] 后面加上[.cnpmjs.org],变成https://github.com.cnpmjs.org/VundleVim/Vundle.vim.git,然后再下载,会快很多。
此处参考:解决 git clone 慢的问题
输入cd ~
进入家目录,再输入vim ~/.vimrc
进行vim配置,让vim安装该插件。注意,没有.vimrc
文件也没事,会自动创建的,如果你不会vim基本操作,请自学。
将以下内容作为.vimrc
文件的内容:
set shell=/bin/bash
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
" call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
然后在vim下输入命令:PluginInstall
安装插件,再输入bdelete
删除高速缓存关闭窗口,之后退出。
(至于Vundle的入门使用,大家可以去网上学,非常简单)
4 安装YouCompleteMe
4.1 下载源文件包
下载源文件包
cd ~/.vim/bundle
git clone https://github.com/Valloric/YouCompleteMe.git
老样子,记得在github.com
后面加上.cnpmjs.org
,加速下载,上面提及以后不再重复。
4.2 下载相关依赖
然后进入YouCompleteMe目录下,之后再获取最新版本的相关依赖文件。
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
此处,先不要运行第二条命令,因为直接运行会非常慢!
我们按照以下步骤做:
- 运行
git submodule update --init
,这个过程需要等待一下。 - 输入
vim .gitmodules
。
- 将里面所有的[
github.com]
都加上[.cnpmjs.org]
,然后保存退出。 - 输入[
git submodule sync]
更新子项目的[url] - 输入[
git submodule update --init --recursive]
这个时候,如果提示fatal: Needed a single revision Unable to find current revision in submodule path 'third_party/requests_deps/certifi'
我们需要再做一些操作,注意关注结尾的[third_party/requests_deps/certifi]
,你的和我的可能不一样,请使用自己的提示完成下面的步骤。
输入rm -rf <你显示的结尾>
,这里我输入[rm -rf third_party/requests_deps/certifi]
。
然后再输入[git submodule update --init --recursive]
就没有问题了。
在[git submodule update --init --recursive]
这一步如果出现错误如下:
fatal: 无法连接到 github.com:
github.com[0: 192.30.253.113]: errno=Operation timed out
fatal: 无法克隆 'git://github.com/mitsuhiko/flask-sphinx-themes.git' 到子模组路径
可参照这篇博文解决:
在终端输入以下:git config --global url."https://".insteadOf git://
然后再用之前的命令就可以完成下载了。
(这是因为防火墙只接受http和https,不认git:开头的地址。需要在git里面设置一下。)
4.3 编译和配置支持自动补全的语言
若使用C/C++,可以安装C族,运行./install.py --clang-completer
;若安装全部支持的语言,可以输入./install.py --all
。
这里使用前者,速度较快,安装全部用不到的话也没必要。
完成之后,在 vim 的配置文件 ~/.vimrc 中添加一行
(在call vundle#begin() 和 call vundle#end() 之间)
call vundle#begin()
. . .
Plugin 'Valloric/YouCompleteMe'
. . .
call vundle#end()
保存,之后安装插件,在vim下输入:PluginInstall
,前面提及过,不再细说。
之后运行vim,提示YouCompleteMe unavailable: requires Vim 8.1.2269+
,看来还需要升级vim版本阿!。
- 删除当前的所有vim版本
dpkg -l | grep vim sudo apt-get remove vim vim-runtime vim-tiny vim-common
注意,第二条命令,是根据第一条命令的结果设置的,博主第一条命令显示的是这4个,所以删除这几个。
- 安装新的vim
sudo add-apt-repository ppa:jonathonf/vim
sudo apt-get update
sudo apt-get install vim
5.增强功能
在.vimrc
文件末尾加上:
let g:ycm_show_diagnostics_ui = 0
let g:ycm_server_log_level = 'info'
let g:ycm_min_num_identifier_candidate_chars = 2
let g:ycm_collect_identifiers_from_comments_and_strings = 1
let g:ycm_complete_in_strings=1
let g:ycm_key_invoke_completion = '<c-z>'
noremap <c-z> <NOP>
let g:ycm_semantic_triggers = {
\ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
\ 'cs,lua,javascript': ['re!\w{2}'],
\ }
接下来可能还会报错:NoExtraConfDetected: No .ycm_extra_conf.py file detected ………………
博主这里报错是不能找到.ycm_extra_conf.py
,因此我们需要查找一下,发现它在目录~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples
中,因此我们需要再配置一下,加上一条
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py'
注:到这里,算是初步的安装成功,但配置远远不够,此时你会发现YCM仍然不能根据头文件来补齐你的关键字,而只能补齐本文件中曾出现过的关键字。因此,需要如下的配置步骤:
第一步:安装 llvm 和 clang
到 www.llvm.org 下载相应版本的llvm和clang,因为我用的是ubuntu 14.04 64bit的系统所以我直接下载的pre-build binary版本的而不需要下载源码再自己重新编译了。省去了不少麻烦。
下载完成后,直接将下载后的压缩文件解压,并进入其目录
tar zxvf clang+llvm-3.7.0-amd64-Ubuntu-14.04..tar.gz
cd clang+llvm-3.7.0-amd64-Ubuntu-14.04.
其中 *.tar.gz就是您所下载的压缩文件。
运行命令:
sudo cp -R * /usr/
将目录里的所有文件拷贝到相应的系统目录下,然后再配置一下软链接:
sudo ln -sf /usr/bin/clang++ /etc/alternatives/c++
其中 “-sf”中 “s”代表了软链接,“f”即为force的意思。
运行:clang --version 如果您看到了它的版本号,就说明 llvm 和 clang配置好了。
第二步:配置
打开 ~/下的.vimrc文件,确保查看是否有这样一条配置:
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py'
如果没有,请修改.ycm_extra_conf.py的路径是正确的。
然后打开这个文件:
vim ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py
您可以在flags[ * ]数组的后面添加你想要的路径,例如: stdio.h等C语言的头文件包含在/usr/include中,那么您需要添加这样一条
'-isystem',
‘/usr/include’,
注意,不要忘记每一句后面的“,”。
需要CPP的补全,就需要添加:
'-isystem',
‘/usr/include/c++/4.7’,
需要什么,就添加什么,so easy .
这样,就可以实现对相应库的头文件的自动补全。