介绍:ycm是YouCompleteMe的简称。用处是导入头文件,自动补全和跳转函数定义

一、vim 8.x 编译、安装ycm

参考链接:​​https://blog.csdn.net/superhoner/article/details/111310897​

  1. 先查看vim的版本,及vim对python大版本的支持效果(+代表支持,-表示不支持)
  2. 给vim配置自动补全(插件管理器:vim-plug)_ycm


2.在.vimrc文件里面加入

"定义vim-plug下载的插件包存储位置
call plug#begin('~/.vim/plugged')
"声明插件
Plugin 'Valloric/YouCompleteMe'

call plug#end()


3.重新打开vim,在末行输入,让vim自动安装插件。

:PlugInstall

4.vim-plug 安装完成后,为了避免包缺失,在 ~/.vim/plugged/YouCompleteMe下运行命令。git clone只能够克隆项目本身,不能够克隆项目的依赖模块(子模块)

git submodule update --init --recursive

5.运行安装脚本

./install.py --clang-completer

这个时候最好是在前面指定pyhton版本,博主linux上有三四个python版本,后面在给ycm配置参数的时候才知道,用哪个python编译就要在配置文件里面指定那个pyhton版本。所以最好写成这样

/usr/bin/python3.8 ./install.py --clang-completer

我以前装过很多东西了所以并没出现过如下错误

给vim配置自动补全(插件管理器:vim-plug)_自动补全_02

如果出现了,就安装g++、 build-essential,然后再执行上面的指令

sudo apt-get install -y build-essential

6.然后ycm插件就算是有了。接下来就是配置ycm插件。

二、配置ycm插件(.ycm_extra_conf.py和.vimrc)

参考链接:

​https://blog.csdn.net/weixin_33877092/article/details/93080514​

​https://blog.csdn.net/rikeyone/article/details/95970499​

​https://www.jianshu.com/p/3edc26d9d0b6​

1.把模板文件放到家目录下

cp ~/.vim/plugged/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py ~/

2.修改.ycm_extra_conf.py文件(这个文件里面的flags是最需要修改的——支持语言、语言标准、编译选项、头文件路径)。我在里面修改和添加了C、C++语言支持

(-x代表使用何种语言。-std和-isystem是gcc的编译选项)

flags = [
'-Wall',
'-Wextra',
'-Werror',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-DNDEBUG',
# You 100% do NOT need -DUSE_CLANG_COMPLETER and/or -DYCM_EXPORT in your flags;
# only the YCM source code needs it.
'-DUSE_CLANG_COMPLETER',
'-DYCM_EXPORT=',
# THIS IS IMPORTANT! Without the '-x' 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 the '-x' flag.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c',
'-std=gnu11',
'-isystem',
'/usr/local/include',
'-isystem',
'/usr/lib/llvm-10/lib/clang/10.0.0/include',
'-isystem',
'/usr/include/x86_64-linux-gnu',
'-isystem',
'/usr/include',
'-x',
'c++',
'-isystem',
'cpp/pybind11',
'-isystem',
'cpp/whereami',
'-isystem',
'cpp/BoostParts',
'-isystem',
get_python_inc(),
'-isystem',
'cpp/llvm/include',
'-isystem',
'cpp/llvm/tools/clang/include',
'-I',
'cpp/ycm',
'-I',
'cpp/ycm/ClangCompleter',
'-isystem',
'cpp/ycm/tests/gmock/googlemock/include',
'-isystem',
'cpp/ycm/tests/gmock/googletest/include',
'-isystem',
'cpp/ycm/benchmarks/benchmark/include',
'-std=c++17',
'-isystem',
'/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9',
'-isystem',
'/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9',
'-isystem',
'/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/backward',
'-isystem',
'/usr/local/include',
'-isystem',
'/usr/lib/llvm-10/lib/clang/10.0.0/include',
'-isystem',
'/usr/include/x86_64-linux-gnu',
'-isystem',
'/usr/include',
]

3. 在~/.vimrc里面加入ycm插件配置

"默认配置文件路径"
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py'
""打开vim时不再询问是否加载ycm_extra_conf.py配置"
let g:ycm_confirm_extra_conf=0
set completeopt=longest,menu
"python解释器路径,当你的/usr/bin目录下有多个pyhton版本的时候一定要注意写清楚使用哪个"
let g:ycm_server_python_interpreter='/usr/bin/python3.8'
""是否开启语义补全"
let g:ycm_seed_identifiers_with_syntax=1
"是否在注释中也开启补全"
let g:ycm_complete_in_comments=1
let g:ycm_collect_identifiers_from_comments_and_strings = 0
""开始补全的字符数"
let g:ycm_min_num_of_chars_for_completion=2
"补全后自动关机预览窗口"
let g:ycm_autoclose_preview_window_after_completion=1
"" 禁止缓存匹配项,每次都重新生成匹配项"
let g:ycm_cache_omnifunc=0
"字符串中也开启补全"
let g:ycm_complete_in_strings = 1
""离开插入模式后自动关闭预览窗口"
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
"上下左右键行为"
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>'
" 按 F3键 跳转到定义处
nnoremap <F3> :YcmCompleter GoToDefinitionElseDeclaration


细节:

1.如何知道clang支持哪些C和C++语法、语义标准,如何将头文件路径加入设置。


echo | clang -v -E -x c++ -
echo | clang -v -E -x c -

用命令查看库路径

echo | clang -v -E -x c++ -

#include "..." search starts here:

#include <...> search starts here:

/opt/llvm-4.0.1/bin/../include/c++/v1

/usr/local/include

/opt/llvm-4.0.1/bin/../lib/clang/4.0.1/include

/usr/include

End of search list.

将上面的 include 目录加入到 flags中

'-isystem',

'/opt/llvm-4.0.1/bin/../include/c++/v1',

'-isystem',

'/usr/local/include',

'-isystem',

'/opt/llvm-4.0.1/bin/../lib/clang/4.0.1/include',

'-isystem',

'/usr/include',


'-I',

'/home/user/projects/include', # 此处填写项目头文件路径




效果如图:

给vim配置自动补全(插件管理器:vim-plug)_ycm_03