Vim自动补全神器YouCompleteMe的配置

简洁:YouCompleteMe号称Vim的自动补全神器,该项目在github的地址:YouCompleteMe;以下在Ubuntu14.04平台配置完成

插件安装操作:

1、确保Vim版本至少是7.4.273,并且支持python2脚本;vim版本升级:sudo add-apt-repository ppa:fcwu-tw/ppa; sudo apt-get update; sudo apt-get install vim

操作系统字符编码必须设置为UTF-8

查询语系命令:locale

在Ubuntu14.04中修改语系的方法:

打开文件/etc/default/locale,修改变量LANG:

中文:LANG="zh_CN.UTF-8"

英文:LANG="en_US.UTF-8"

在CentOS7.0中修改语系的命令行方法:

修改为中文:localectl set-locale LANG=zh_CN.utf8

修改为英文:localectl set-locale LANG=en_US.UTF-8

或者打开文件/etc/locale.conf修改LANG=zh_CN.utf8

修改后重启即可

2、下载最新版的libclang。Clang是一个开源编译器,能够编译C/C++/Objective-C/Objective-C++。Clang提供的libclang库是用于驱动YCM对这些语言的语义补全支持。YCM需要版本至少为3.6的libclang,但是理论上3.2+版本也行。也可以使用系统libclang,如果确定是3.3版本或者更高。sudo apt-get install libclang-dev;首先要确保你已经安装类cmake。如果没有,请按如下安装:sudo apt-get install cmake;其次需要确保你有一些 Python 头文件。如果没有,请按如下安装:sudo apt-get install python-dev

3、编译YCM需要的ycm_support_libs库。YCM的C++引擎通过这些库来获取更快的补全速度。需要cmake,如果未安装,安装之:sudo apt-get install build-essential cmake(也可以下载安装http://www.cmake.org/cmake/resources/software.html)。确保python头文件已安装:sudo apt-get install python-dev

4、假设你已经通过Vundle装好YCM了,那么它应该位于~/.vim/bundle/YouCompleteMe;如果~/.vim/没有bundle文件夹,执行命令:git clone http://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

5、手动安装YouCompleteMe。切换至 ~/.vim/bundle 手动下载YouCompleteMe,输入如下命令:git clone https://github.com/Valloric/YouCompleteMe.git; 手动下载完后检查仓库的完整性,切换到 YouCompleteMe 目录下,输入如下命令:git submodule update --init --recursive

6、编译YCM,如果需要对C-family的语义支持: cd ~/.vim/bundle/YouCompleteMe; ./install.sh --clang-completer;如果不需要对C-family的语义支持:cd ~/.vim/bundle/YouCompleteMe; ./install.sh ;如果需要支持C#,添加 --omnisharp-complete。如果需要支持Go添加--gocode-completer

编译clang+llvm时内存要大于1.5G,否则会报错: g++: internal compiler error: Killed (program cc1plus) Please submit a full bug report

主要原因大体上是因为内存不足,临时使用交换分区来解决:

sudo dd if=/dev/zero of=/swapfile bs=64M count=16
sudo mkswap /swapfile
sudo swapon /swapfile

编译完成后释放交换空间:

sudo swapoff /swapfile
sudo rm /swapfile

7、接着再次打开 .vimrc 配置YCM,添加内容如下:

filetype off                  " required!
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
Plugin 'Valloric/YouCompleteMe'
filetype plugin indent on     " required!
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'

8、为了补全,我们还需要在 .ycm_extra_conf.py 文件中进行配置,vim ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py;添加信息如下:

'-isystem',
'/usr/include',
'-isystem',
'/usr/include/c++/4.8.4',
'-isystem',
'/usr/include/c++/4.9.2',
'-isystem',
'/usr/include',
'/usr/include/x86_64-linux-gnu/c++',

实际上以上是vim自动补全时搜索路径,如果自动补全的内容位于/usr/local/include里面,则添加以下信息:

'-isystem',
'/usr/local/include',

根据实际的/usr/include/c++/中的文件夹名称(即C++版本号)修改:

'-isystem',
'/usr/include/c++/4.8.4',
'-isystem',
'/usr/include/c++/4.9.2',

添加结果如下:

下载并安装ctags,下载地址:http://ctags.sourceforge.net/

完工。

如果遇到Error detected while processing function <SNR>20_ActivateBuffer这种错误,请打开vim ~/.vim/plugin/bufexplorer.vim将第95行注释掉。

参考:
http://blog.csdn.net/yangkuanqaz85988/article/details/48886367
http://www.linuxidc.com/Linux/2015-07/120352.htm
http://blog.jobbole.com/58978/

posted on 2016-11-04 21:11  懒惰的码农  阅读(16889)  评论(0编辑  收藏  举报

导航