一、 YouCompleteMe 介绍
YouCompleteMe(简称 YCM) 是一款 vim 的智能补全插件,支持 C/C++, Go, Python...
等多种代码类型补全。
它强大的功能吸引了不少人的使用,但有无数人因为安装它 「折腰」,因为它的安装过程确实很麻烦。
花了一个下午的时间,来回装了两次,终于算是勉强搞定。
首先假定你已经安装好了 vim 和对应的插件管理器:升级安装 vim 8.0 并添加 vundle 插件管理
要注意的是,vim 编辑器要求编译的时候添加了 python
支持。
根据测试,选择 python2
支持会比 python3
省事一些,因为后面安装 cmake
的时候貌似只能使用 python2
(具体是不是这样没有去深入研究,目前暂且按 python2 的来) 。
先上一张效果图:

代码的主页为 YouCompelteMe,先把代码拷贝到插件目录下:
|
git clone --recursive https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe git submodule update --init --recursive |
二、安装 clang
clang 下载地址,找到对应的二进制包下载:

|
wget https://releases.llvm.org/6.0.0/clang+llvm-6.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz tar -Jxvf clang+llvm-6.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz -C /usr/local ln -s /usr/local/clang+llvm-6.0.0-x86_64-linux-gnu-ubuntu-14.04/ /usr/local/clang |
需要用到的是 libclang.so
库文件,位于安装目录下的 lib/libclang.so
。
三、安装 cmake
从源码编译安装:
|
wget https://cmake.org/files/v3.11/cmake-3.11.0-rc3.tar.gz tar -zxvf cmake-3.11.0-rc3.tar.gz cd cmake-3.11.0-rc3 ./bootstrap make && make install |
校验是否安装成功:
|
> cmake --version cmake version 3.11.0-rc3 CMake suite maintained and supported by Kitware (kitware.com/cmake). |
四、编译 ycm_core
建立一个临时文件夹~/ycm_build
用来作为临时编译文件夹:
|
mkdir ~/ycm_build cd ~/ycm_build |
使用 cmake
生成 Makefile
文件:
|
cmake -G "Unix Makefiles" -DEXTERNAL_LIBCLANG_PATH=/usr/local/clang/lib/libclang.so . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp |
问题一
如果安装 vim 时指定 python 的支持版本为 python3,则在上面还要加上-DUSE_PYTHON2=OFF
选项关掉默认使用 python2
编译。
否则安装完成后会出现以下错误:
|
The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). YCM core library compiled for Python 2 but loaded in Python 3. Set the 'g:ycm_server_python_interpreter' option to a Python 2 interpreter path. |
问题二
在 ubuntu 16.04
中执行这一步骤时遇到 boost
库缺失的问题:
|
CMake Warning at /usr/local/share/cmake-3.11/Modules/FindBoost.cmake:564 (message): Imported targets and dependency information not available for Boost version (all versions older than 1.33) Call Stack (most recent call first): /usr/local/share/cmake-3.11/Modules/FindBoost.cmake:903 (_Boost_COMPONENT_DEPENDENCIES) /usr/local/share/cmake-3.11/Modules/FindBoost.cmake:1571 (_Boost_MISSING_DEPENDENCIES) ycm/CMakeLists.txt:205 (find_package) Please set them or make sure they are set and tested correctly in the CMake files: Boost_INCLUDE_DIR (ADVANCED) used as include directory in directory /home/ma/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm used as include directory in directory /home/ma/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm used as include directory in directory /home/ma/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm -- Configuring incomplete, errors occurred! See also "/home/ma/ycm_build/CMakeFiles/CMakeOutput.log". |
解决方案:
然后重新执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
> cmake -G "Unix Makefiles" -DEXTERNAL_LIBCLANG_PATH=/usr/local/clang/lib/libclang.so . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp # 以下为输出 -- The C compiler identification is GNU 5.4.0 -- The CXX compiler identification is GNU 5.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found suitable version "2.7.12", minimum required is "2.7") -- Using libclang to provide semantic completion for C/C++/ObjC -- Using external libclang: /usr/local/clang+llvm-6.0.0-x86_64-linux-gnu-ubuntu-14.04/lib/libclang.so.6.0 -- Configuring done -- Generating done -- Build files have been written to: /home/ma/ycm_build |
如果以上步骤都没有问题,开始下面的步骤
构建 ycm_core
:
|
cmake --build . --target ycm_core --config Release |
成功的结果:

五、配置
5.1 基础配置
|
cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/ |
在~/.vimrc
中添加配置:
添加插件支持,在 vundle 插件代码块中加入以下内容:
|
set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'VundleVim/Vundle.vim' " 新加的 YCM 插件 Plugin 'Valloric/YouCompleteMe' call vundle#end() |
然后添加配置文件:
|
"如果编译时指定的 python 版本为 python3,这里也要改成对应的 python 版本 let g:ycm_server_python_interpreter='/usr/bin/python' let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py' |
创建一个*.c
文件测试:

5.2 其他配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
" 添加 C++11 支持 let g:syntastic_cpp_compiler='g++' let g:syntastic_cpp_compiler_options='std=c++11 -stdlib=libc++' "离开插入模式后自动关闭预览窗口 set completeopt=longest,menu autocmd InsertLeave * if pumvisible() == 0|pclose|endif "回车即选中当前项 inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" inoremap <expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>" inoremap <expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>" inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>" inoremap <expr> <PageUp> pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>" "youcompleteme 默认 tab s-tab 和自动补全冲突 "let g:ycm_key_list_select_completion=['<c-n>'] let g:ycm_key_list_select_completion = ['<Down>'] "let g:ycm_key_list_previous_completion=['<c-p>'] let g:ycm_key_list_previous_completion = ['<Up>'] "关闭加载.ycm_extra_conf.py 提示 let g:ycm_confirm_extra_conf=0 " 开启 YCM 基于标签引擎 let g:ycm_collect_identifiers_from_tags_files=1 " 从第 2 个键入字符就开始罗列匹配项 let g:ycm_min_num_of_chars_for_completion=2 " 禁止缓存匹配项, 每次都重新生成匹配项 let g:ycm_cache_omnifunc=0 " 语法关键字补全 let g:ycm_seed_identifiers_with_syntax=1 "force recomile with syntastic nnoremap <F5> :YcmForceCompileAndDiagnostics<CR> "nnoremap <leader>lo :lopen<CR> "open locationlist "nnoremap <leader>lc :lclose<CR> "close locationlist inoremap <leader><leader> <C-x><C-o> "在注释输入中也能补全 let g:ycm_complete_in_comments = 1 "在字符串输入中也能补全 let g:ycm_complete_in_strings = 1 "注释和字符串中的文字也会被收入补全 let g:ycm_collect_identifiers_from_comments_and_strings = 0 let g:clang_user_options='|| exit 0' "nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR> " 跳转到定义处 |
六、其他
6.1 C++头文件没有智能提示
添加都头文件目录到~/.vim/.ycm_extra_conf.py
的 flags
数组中即可。
|
# 找到头文件所在的目录 > sudo find / -name iostream /usr/include/c++/5/iostream /usr/include/boost/tr1/tr1/iostream /usr/local/clang+llvm-6.0.0-x86_64-linux-gnu-ubuntu-14.04/include/c++/v1/iostream find: 『/run/user/1000/gvfs』: Permission denied |
添加到 flags
数组:
|
flags=[ '...', '-isystem', '/usr/local/clang+llvm-6.0.0-x86_64-linux-gnu-ubuntu-14.04/include/c++/v1', ] |
评论