标签:
原文: http://blog.marchtea.com/archives/161#rd?sukey=fc78a68049a14bb2ba33c15948d34749e1eb616df07efe977573d59996ea9b63dc82be61d5771f948f5d8f01a917acd7
第一次听说这个插件还是在偶然的情况下看到别人的博客,听说了这个插件的大名.本来打算在实训期间来完成安装的,无奈网实在不给力,也就拖到了回家的时候.在开始准备工作的时候就了解到这个插件不是很容易安装,安装的时候果然名不虚传.(关于这方面的内容,请查看另一篇文章)不过,有付出总有回报,安装之后用上这个插件,真心为这个插件的强大所折服.
那这个插件有何不同?
总所周知,Vim
是一款文本编辑器.也就是说,其最基础的工作就是编辑文本,而不管该文本的内容是什么.在Vim
被程序员所使用后,其慢慢的被肩负了与IDE一样的工作,文本自动补全(ie.acp
,omnicppcompleter
),代码检查(Syntastic
)等等工作.
针对文本自动补全这个功能来说,主要有两种实现方式.
我们常用的omnicppcompleter
,acp
,vim自带的c-x, c-n
的实现方式就是基于文本.更通俗的说法,其实就是一个字:
其通过文本进行一些正则表达式的匹配,再根据生成的tags(利用ctags
生成)来实现自动补全的效果.
顾名思义,其是通过分析源文件,经过语法分析以后进行补全.由于对源文件进行分析,基于语义的补全可以做到很精确.但是这显然是vim所不可能支持的.而且经过这么多年发展,由于语法分析有很高的难度,也一直没有合适的工具出现.直到,由apple支持的clang/llvm
横空出世.YouCompleteMe
也正是在clang/llvm
的基础上进行构建的.
对于其他的语言,会调用vim设置的omnifunc
来匹配,因此同样支持php
,ruby
等语言.
已知的有 * javascript —-tern_for_vim * ruby/java —-eclim
include
的文件进行补全:w
进行强制检测说完了那么多好处,就要说到安装了.不同于以往其他vim插件,YCM是一款编译型的插件.在下载完后,需要手动编译后才能使用.对应其他的插件来说,仅仅就是把.vim的文件丢到相应文件夹下就可以.而这也加大了使用YCM的难度.
vim --version
查看)brew install cmake
,ubuntu可以通过sudo apt-get install cmake
)在.vimrc
中添加下列代码
Bundle ‘Valloric/YouCompleteMe‘
保存退出后打开vim,在正常模式下输入
:BundleInstall
等待vundle
将YouCompleteMe安装完成
而后进行编译安装:
cd ~/.vim/bundle/YouCompleteMe
./install --clang-completer
如果不需要c-family的补全,可以去掉--clang-completer
.如果需要c#
的补全,请加上--omnisharp-completer
.
正常来说,YCM会去下载clang的包,如果已经有,也可以用系统--system-libclang
.
就这样,安装结束.打开vim,如果没有提示YCM未编译,则说明安装已经成功了.
安装的脚本并不是什么时候都好用,至少对我来说是这样的.安装完之后出现了问题,参考issue#809.
在用:BundleInstall
安装完成或者使用
git clone --recursive https://github.com/Valloric/YouCompleteMe.git
获取最新的仓库,而后使用git submodule update --init --recursive
确认仓库的完整性后,开始安装流程.
clang
版本 > 3.2,一般来说都是下载最新的.sudo apt-get install python-dev
,mac下默认提供,否则请安装command line tools)编译
cd ~
mkdir ycm_build
cd ycm_build
cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir . ~/.vim/bundle/YouCompleteMe/cpp
make ycm_support_libs
这里需要注意的是,~/ycm_temp/llvm_root_dir中包含的是根据第一步下载的压缩包解压出来的内容(包括include
, bin
等等文件)
这样就完成了,开始感受YCM提供的完全不逊色于大型IDE所提供的自动补全功能吧.
不同于很多vim插件,YCM首先需要编译,另外还需要有配置.在vim启动后,YCM会找寻当前路径以及上层路径的.ycm_extra_conf.py
.在~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py
中提供了默认的模板.也可以参考我的(就在模板上改改而已).不过这个解决了标准库提示找不到的问题.
一般来说,我会在~
目录下放一个默认的模板,而后再根据不同的项目在当前目录下再拷贝个.ycm_extra_conf.py
.
# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here‘s the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
import os
import ycm_core
# These are the compilation flags that will be used in case there‘s no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
‘-Wall‘,
‘-Wextra‘,
#‘-Werror‘,
#‘-Wc++98-compat‘,
‘-Wno-long-long‘,
‘-Wno-variadic-macros‘,
‘-fexceptions‘,
‘-stdlib=libc++‘,
# 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++‘,
‘-I‘,
‘.‘,
‘-isystem‘,
‘/usr/include‘,
‘-isystem‘,
‘/usr/local/include‘,
‘-isystem‘,
‘/Library/Developer/CommandLineTools/usr/include‘,
‘-isystem‘,
‘/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1‘,
]
# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of ‘flags‘. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Most projects will NOT need to set this to anything; you can just change the
# ‘flags‘ list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ‘‘
if os.path