Ubuntu20.04安装YouCompleteMe自动补齐插件
写在开头,为了能够有更好的写代码体验于是安装了YouCompleteMe这个插件,安装成功效果图
若有错误请各位指正,欢迎多多交流。

安装git
sudo apt-get install git
下载Vundle插件管理器
mkdir -p ~/.vim/bundle
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
然后在.vimrc中进行设置 (完整.vimrc见文末)
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim' //插件管理
call vundle#end()
完成这一步则vim ,底行模式下输入 :PluginInstall进行 Vundle 的安装,安装成功后底行显示Done!
获取YouCompleteMe
cd ~/.vim/bundle
git clone https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe

进入 ~/.vim/bundle/YouCompleteMe 执行
git submodule update --init --recursive
此处git若显示错误:fatal: Out of memory, malloc failed (tried to allocate 5242880000 bytes)
我设置了http.postbuffer=5242880000 https.postbuffer=5242880000
然后将虚拟机内存从2G增大到4G-->问题解决
环境安装
sudo apt-get install build-essential cmake python-dev python3-dev
sudo apt-get install llvm clang libclang-10-dev libboost-all-dev
下载clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz.sig包
https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz
解压到 ~/ycm_temp/llvm_root_dir (自己创建此目录)
此时目录下应有bin、include、lib、libexec、share文件目录,如图所示

准备编译ycm_core
cd ~
mkdir ycm_build
cd ycm_build
cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
cmake --build . --target ycm_core --config Release
安装watchdog库和构建正则表达式模块
此部分内容来自 https://github.com/ycm-core/YouCompleteMe/wiki/Full-Installation-Guide 完全安装手册
5、Install watchdog library
The watchdog dependency is located in YouCompleteMe/third_party/ycmd/third_party/watchdog_deps/watchdog/.
进入 ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/watchdog_deps/watchdog/目录
5.1. For users with setuptools available (如果你的setuptools 可用的话)
Go to the `watchdog` directory, remove `build/lib3` and run
进入watchdog文件夹,将build/lib3文件夹删掉,运行下列命令
python setup.py build --build-base=build/3 --build-lib=build/lib3.
5.2. Install watchdog when setuptools isn’t available (如果你的setuptools 不可用的话)
Go to the `watchdog` directory, remove the `build/lib3` directory and them copy the entire `src/watchdog`into `build/lib3`.
进入watchdog文件夹,将build/lib3文件夹删掉,再将watchdog下的src文件夹拷贝到 build/lib3
6、This step is optional. (此步骤可选,构建正则表达式模块)
Build the regex module for improved Unicode support and better performance with regular expressions. The procedure is similar to compiling the ycm_core library:
cd ~
mkdir regex_build
cd regex_build
cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/cregex
cmake --build . --target _regex --config Release
此时进入 ~/.vim/bundle/YouCompleteMe文件夹下,运行
python3 install.py --clang-completer
注:其中--clang-completer表示对其C族语言的支持,也可加sudo执行

完成后将~/.vim/bundle/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py 拷贝到 ~/.vim/ 下,当然你也可以不用,但是需要在.vimrc中对其路径进行设定,其中一些设定如下:
"YoucomPleteMe:语句补全插件"
set runtimepath+=~/.vim/bundle/YouCompleteMe
let g:ycm_server_python_interpreter='/usr/bin/python3.8' "python版本在3以上
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py' "加载文件路径
"let g:ycm_clangd_binary_path = "~/ycm_temp/llvm_root_dir/bin/clangd"
let g:clang_library_path='/usr/lib/llvm-10/lib/libclang.so' "libclang路径
autocmd InsertLeave * if pumvisible() == 0|pclose|endif " 离开插入模式后自动关闭预览窗口"
let g:ycm_collect_identifiers_from_tags_files = 1 " 开启YCM基于标签引擎
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释与字符串中的内容也用于补全
let g:syntastic_ignore_files=[".*\.py$"]
let g:ycm_seed_identifiers_with_syntax = 1 " 语法关键字补全
let g:ycm_complete_in_strings = 1
let g:ycm_confirm_extra_conf = 0 " 关闭加载.ycm_extra_conf.py提示
let g:ycm_key_list_select_completion = ['<c-n>', '<Down>'] " 映射按键,没有这个会拦截掉tab, 导致其他插件的tab不能用.
let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
let g:ycm_key_invoke_completion = '<C-a>'
let g:ycm_complete_in_comments = 1 " 在注释输入中也能补全
let g:ycm_complete_in_strings = 1 " 在字符串输入中也能补全
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释和字符串中的文字也会被收入补全
"let g:ycm_global_ycm_extra_conf='~/.vim/bundle/You

本文详细介绍了如何在Ubuntu20.04系统中安装YouCompleteMe代码自动补全插件,涵盖了Vundle插件管理器的配置、YouCompleteMe的获取与编译、依赖库的安装及正则表达式模块的构建,同时提供了.vimrc文件的配置示例。
最低0.47元/天 解锁文章
2852

被折叠的 条评论
为什么被折叠?



