Vim 安装 YouCompleteMe

Vim 下的自动补全,最好的工具莫过于 YouCompleteMe,官方文档在这里 http://valloric.github.io/YouCompleteMe/

安装稍显复杂,以下记录我的过程。

1. 安装 Vundle

(这里顺带就把 YouCompleteMe 下载了)

Vundle 是 Vim 下的一个插件管理器,如果之前你配置 Vim 是手动放置 xxx.vim 配置文件到相应目录,那么现在你要安装 Vundle, 因为 YouCompleteMe 是基于 Vundle 安装的,

Vundle 的 github 地址: https://github.com/VundleVim/Vundle.vim

- git 克隆插件  $ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

-  添加如下内容到 ~/.vimrc 文件最前面,

"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For Vundle Start
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'

" Install YouCompleteMe
Plugin 'Valloric/YouCompleteMe'  " 注意,这就是要安装的插件 YouCompleteMe

" All of your Plugins must be added before the following line
call vundle#end()             " required
filetype plugin indent on     " required


"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For Vundle End

保存关闭。

-   启动 Vim,执行命令 :PluginInstall 将出现如下安装画面,( YouCompleteMe 插件较大,请耐心等待)

 

2. 编译 YouCompleteMe

- 安装相关工具

$ sudo apt-get install build-essential cmake

- 安装相关 Python 库

$ sudo apt-get install python-dev python3-dev

- 编译 (为支持 C,C++,Python 智能补全,添加选项 --clang-completer)

cd ~/.vim/bundle/YouCompleteMe

./install.py --clang-completer

注: 为支持其他语言,比如 Javascript,Go 等,要首先安装相应的工具,具体参见官方文档。

 

3. 设置 .vimrc

“ 配置默认的 .ycm_extra_conf.py 路径
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py'


注:当有新库加入时,需要更新此文件,例如,我要支持 boost 的补全,则要加入 boost 头文件路径 /usr/local/boost_1_67_0,加到 flags 的最后即可,

flags = [

...

# for boost complete
'-isystem',
'/usr/local/boost_1_67_0'
]

4. 使用中的问题

- 补全功能可用,但是会有恼人的信息,

我当前安装的 gVim 版本是 7.4.52, 根据 YouCompleteMe 官网的建议,升级到 7.4.314 以上可解决这个问题,于是,

# add-apt-repository ppa:pi-rho/dev

# apt-get update

# apt-get install vim-gnome  (重装即可升级)

升级后,问题解决。

注: 补全时,当弹出 tip 窗口,按 Tab 来 cycle 可选项。

另, Centos 升级 gvim 到 8.x 版本见:https://www.cnblogs.com/gaowengang/p/10546815.html

 

 对于 Ubuntu 18.04 的 workaround 方法  

因为我之前一直用 Ubuntu 14.04 和  CentOS 7,这两个操作系统默认的 gcc 版本是 4.8,我在这两个系统下完成了对 gvim 的各种配置(包括 YouCompleteMe 的配置),并且各项功能都调好了。

因项目需要,在升级系统到 Ubuntu 18.04 后,为方便起见,我把旧系统的 .vimrc 和 .vim 拷贝到新系统的 home 目录下, 并调整 YouCompleteMe 配置文件的内容,使其指向新系统的 c++ 头文件目录,但是却不能实现 c++ 的自动补全。目前怀疑和 Ubuntu 18.04 的 gcc 和 g++ 版本有关,新系统默认的版本是 7.4。

workaround 方法是:安装 gcc 4.8  ( $ sudo apt-get install gcc-4.8 ) 和 g++ 4.8 ( $ sudo apt-get install g++-4.8 ),并调整 YouCompleteMe 配置文件,使其指向 c++ 4.8 的头文件目录。

 

完。

 

 

原文地址:https://www.cnblogs.com/gaowengang/p/5743318.html