Ubuntu|Ubuntu18.04配置vim——利用Vundle安装YouCompleteMe(有彩蛋)

喵哥前段时间尝试在centos6上安装YCM失败了,主要是因为众多依赖软件的版本太低,搞得喵哥全无兴致,好歹也是记录了一下路上遇到的坑位。详情可以到:centos6.10安装VIM插件YOUCOMPLETEME的坑位标记参考。
今天,喵哥打算把宿舍笔记本的vim给配置了。
笔记本的系统是Ubuntu18.04,之前用的时候把机器上的vim、Python都更新了一遍,目前的状况大致如下:

软件 版本
Python 3.7.1
gcc 7.3.0
vim 8.0
git 2.17.1
cmake 未安装
llvm 未安装
除了cmake外、llvm,其余的版本在我印象中是够用了,这些软件的安装教程在网上还是蛮多的,如果发现自己的版本不对,建议先去安装好。
先安装cmake:
wget --no-check-certificate https://cmake.org/files/v3.8/cmake-3.8.0-rc2.tar.gz tar xf cmake-3.8.0-rc2.tar.gz cdcmake-3.8.0-rc2 ./bootstrap make -j 4 sudo make install

插一句,make的时候用-j 4加速是真的爽,之前一直在单核的容器中跑,用-j 4反而更慢。
【Ubuntu|Ubuntu18.04配置vim——利用Vundle安装YouCompleteMe(有彩蛋)】安装llvm
mkdir llvm-3.9.1 cd llvm-3.9.1 wget http://releases.llvm.org/3.9.1/llvm-3.9.1.src.tar.xz wget http://releases.llvm.org/3.9.1/cfe-3.9.1.src.tar.xz wget http://releases.llvm.org/3.9.1/compiler-rt-3.9.1.src.tar.xz wget http://releases.llvm.org/3.9.1/clang-tools-extra-3.9.1.src.tar.xz

编译lang,要记得先把gcc、python、camke安装好。
tar xf llvm-3.9.1.src.tar.xz tar xf clang-tools-extra-3.9.1.src.tar.xz tar xf compiler-rt-3.9.1.src.tar.xz tar xf cfe-3.9.1.src.tar.xz mv cfe-3.9.1.src llvm-3.9.1.src/tools/clang mv clang-tools-extra-3.9.1.src llvm-3.9.1.src/tools/clang/tools/extra mv compiler-rt-3.9.1.src llvm-3.9.1.src/projects/compiler-rt cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$HOME/clang3.9 -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" llvm-3.9.1.src && make -j 4

#2019.7.3
安装好lang之后,在编译YCM的时候还是出了点问题:
Downloading libclang 8.0.0 from https://dl.bintray.com/micbou/libclang/libclang-8.0.0-x86_64-unknown-linux-gnu.tar.bz2 CMake Error at ycm/CMakeLists.txt:107 (file): file DOWNLOAD HASH mismatchfor file: [/home/catlincao/.vim/plugged/YouCompleteMe/third_party/ycmd/cpp/../clang_archives/libclang-8.0.0-x86_64-unknown-linux-gnu.tar.bz2] expected hash: [e81a186cd1180ae80c17d67d8d0c101248f8ee032d138cf6f1e95001e733249c] actual hash: [e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855] status: [1; "Unsupported protocol"]CMake Error at ycm/CMakeLists.txt:123 (message): Cannot find path to libclang in prebuilt binaries-- Configuring incomplete, errors occurred! See also "/tmp/ycm_build_h6en0mfh/CMakeFiles/CMakeOutput.log". ERROR: the build failed.

按照提示,手动下载libclang 8.0.0
wget https://dl.bintray.com/micbou/libclang/libclang-8.0.0-x86_64-unknown-linux-gnu.tar.bz2

把下载的文件放到~/.vim/plugged/YouCompleteMe/third_party/ycmd/clang_archives下。就ok了

从github上下载Vundle
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

以官方(https://github.com/VundleVim/Vundle.vim)的提供的.vimrc作为初始模板做了部分添加,主角是:
Plugin 'Valloric/YouCompleteMe'
.vimrc通常保存在当前用户的目录下,便于管理。其实在/etc/vim目录下有一个vimrc的全局配置文件,也可以在这里添加vim的配置命令。喵哥把文件保存在~/下,vim~/.vimrc。
set nocompatible" be iMproved, required filetype off" required set nu set hls syntax enablesyntax onset ruler " 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 'VundleVim/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 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/'} " Install L9 and avoid a Naming conflict if you've already installed a " different version somewhere else. " Plugin 'ascenator/L9', {'name': 'newL9'}"YCM Plugin 'Valloric/YouCompleteMe'" 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

在终端输入vim,进入到vim在普通模式(非插入模式)下输入
:PluginInstall 报错Not an editor command: PluginInstall,执行git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/plugin/Vundle.vim解决

就可以开始下载.vimrc中配置的插件了,如果想快点的话,可以只在配置文件中保留
Plugin 'Valloric/YouCompleteMe'

下载YCM的大小是303M左右,为了查看下载进度,可以用
du -h ~/.vim/bundle/YouCompleteMe

查看下载了多少。
下载YCM完成后,进入到YCM的目录下,开始安装:
cd ~/.vim/bundle/YouCompleteMe ./install.py --clang-completer

安装成功后,在~/.vimrc中添加:
" YouCompleteMe set runtimepath+=~/.vim/bundle/YouCompleteMe 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_comments = 1 let g:ycm_confirm_extra_conf = 0 let g:ycm_key_list_select_completion = ['', '']" 映射按键, 没有这个会拦截掉tab, 导致其他插件的tab不能用. let g:ycm_key_list_previous_completion = ['', ''] 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='~/.ycm_extra_conf.py' let g:ycm_show_diagnostics_ui = 0" 禁用语法检查

我把YCM的配置文件放在当前用户根目录下,这里原本是没有的文件的,自己添加就好了:
vim ~/.ycm_extra_conf.py

由于喵哥主要用C++,所以这里暂时添加C++的相关项目:
flags = [ '-Wall', '-Wextra', '-Werror', '-Wno-long-long', '-Wno-variadic-macros', '-fexceptions', '-DNDEBUG', '-std=c++11', '-x', 'c++', '-I', '/usr/include', '-isystem', '/usr/lib/gcc/x86_64-linux-gnu/5/include', '-isystem', '/usr/include/x86_64-linux-gnu', '-isystem' '/usr/include/c++/5', '-isystem', '/usr/include/c++/5/bits' ]

对于vim找不到的头文件就可以在这里面添加路径。
到这里基本YCM就配置好了,Ctrl+Space弹出补全对象,Ctrl+J跳转至定义处。
Ubuntu|Ubuntu18.04配置vim——利用Vundle安装YouCompleteMe(有彩蛋)
文章图片

孤陋寡闻了,很墙裂地推荐大家参考这两篇文章,真的很强!
https://www.jianshu.com/p/75cde8a80fd7
https://blog.csdn.net/Mason_Mao/article/details/80984726










    推荐阅读