vim自动补全插件YouCompleteMe的安装及配置

  • Post author:
  • Post category:其他


vim自动补全插件YouCompleteMe的安装及配置

本文主要介绍了vim自动补全插件YouCompleteMe的安装教程,仅供参考。

本教程操作系统环境为:Ubuntu 12.04 LTS


YouCompleteMe的Github网址如下




https://github.com/Valloric/YouCompleteMe

1.安装前准备

1.1 vim版本

安装YouCompleteMe插件需要vim的版本号不低于7.3.584,且需要支持python2,已经安装的可以使用vim –version查看版本号,若版本号低于7.3.584,可通过sudo apt-get upgrade升级。由于Ubuntu 12.04 LTS软件仓库中的vim版本过旧,因此需要先添加vim的PPA源:

$ sudo add-apt-repository ppa:nmi/vim-snapshots
$ sudo apt-get update
$ sudo apt-get install vim

1.2 安装vim的插件管理插件vundle


vundle的Github网址如下



https://github.com/gmarik/Vundle.vim

1.2.1安装最新版vundle插件

$ git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim 
$ vim ~/.vimrc

打开.vimrc后,将下面的代码复制进去(与旧版vundle不同)

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 'gmarik/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
Plugin 'Valloric/YouCompleteMe'
" plugin from http://vim-scripts.org/vim/scripts.html
Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Avoid a name conflict with L9
Plugin 'user/L9', {'name': 'newL9'}

" 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

1.3 安装cmake

YouCompleteMe插件需要进行编译才能正常使用,安装最新版cmake,本文采用源码安装方式,进入

http://www.cmake.org/download/

下载最新版的cmake源码,最新版本为3.2.2,故下载cmake-3.2.2.tar.gz,并放入 /usr/local

$ su root
$ cd /usr/local
$ wget http://www.cmake.org/files/v3.2/cmake-3.2.2.tar.gz
$ tar -zxvf cmake-3.2.2.tar.gz
$ cd cmake-3.2.2
$ ./configure 
$ make
$ make install

添加环境变量,打开文件/etc/profile

$ vim /etc/profile

然后在文件末尾添加

PATH=/usr/local/cmake/bin:$PATH
export PATH 

输入以下命令使修改生效

$ source /etc/profile

查看cmake版本

$ cmake --version

若输出下述字符则表示安装成功

cmake version 3.2.2

2.安装YouCompleteMe插件

2.1 安装

在~/.vimrc中添加如下语句,若是直接按照1.2.1安装的vundle则可以不添加

Plugin 'Valloric/YouCompleteMe'

添加后即可以开始安装~/.vimrc中指定的插件,打开vim,在Normal模式下输入:PluginInstall开始安装插件,提示DONE!时安装成功

2.2 编译

安装YouCompleteMe插件后需编译才能正常使用

$ cd ~/.vim/bundle/YouCompleteMe
$ ./install.sh --clang-completer

2.3 配置

该项配置主要用于添加C++标准库和自定义类成员的自动补全

2.3.1 ~/.virmc文件配置

在~/.virmc文件末尾添加如下语句,不同版本的.ycm_extra_conf.py路径不同,下述语句为最新版的.ycm_extra_conf.py路径

"-------------------YouCompleteMe----------------
"c++标准库支持,自定义类成员补全
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
"诊断窗口,:lopen开启,:lclose关闭
let g:ycm_always_populate_location_list = 1 

2.3.2 .ycm_extra_conf.py文件配置

这个文件决定了YCM在进行c系语言(c,c++,etc.) 语法补全时的行为,默认配置文件是支持c++的,但是需要修改一处地方,打开.ycm_extra_conf.py,找到如下代码,注释掉

# NOTE: This is just for YouCompleteMe; it's highly likely that your project
# does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
# ycm_extra_conf IF YOU'RE NOT 100% YOU NEED IT.
try:
final_flags.remove( '-stdlib=libc++' )
except ValueError:
pass

注释后如下:

# NOTE: This is just for YouCompleteMe; it's highly likely that your project
# does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
# ycm_extra_conf IF YOU'RE NOT 100% YOU NEED IT.
# try:
# final_flags.remove( '-stdlib=libc++' )
# except ValueError:
# pass

3 最后

至此,YouCompleteMe插件安装完毕



版权声明:本文为shixuehancheng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。