前言
笔者常用vim进行c/c++/go开发,虽说vim也有自带自动补全(control+n,control+p),不过操作上还是比较麻烦,笔者希望可以实现,输入部分单词可以直接弹出下拉框提示所有可能的单词。网上一搜,发现大家都在强推YouCompleteMe,今天跟着官网教程尝试安装了一把,也踩了几个坑,记录下来,希望对大家有所帮助。
先附上官网链接:
https://github.com/ycm-core/YouCompleteMe#installation
官网写的很清楚了,直接上截图
下面具体操作:
一、安装vundle和YouCompleteMe
vundle是vim插件管理器,可以方便的进行插件的安装和移除
vundle官网链接:
https://github.com/VundleVim/Vundle.vim#about
直接把安装和配置过程里面关键的步骤贴出来了,我们需要先git clone下载vundle源码,然后按照后面的步骤,利用vundle安装YouCompleteMe插件。
配置过程其实也很简单,直接在自己的~/.vimrc配置里面复制粘贴上官网里面的一段话,需要注意的是,需要安装的插件必须位于 call vundle#begin()和 call vundle#end() 之间,第一步里面的截图中,官网只是给了几个插件的安装,我们不需要的话可以直接去掉这几个插件,然后补充自己需要的插件(youcompleteme)
然后直接:PluginInstall即可安装配置中的插件。
set nocompatible " be iMproved, required
filetype off " required" 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'
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
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
此时,遇到一个问题:
Error running :helptags /Users/xxx/.vim/bundle/Vundle.vim/doc
解决方案:
https://stackoverflow.com/questions/21080391/fresh-vim-install-vim-permission-errors-vundle-not-functional/21080650#21080650
出现下面这个截图就说明YouCompleteMe安装ok了,接下来准备编译YouCompleteMe
二、安装必备库
编译YouCompleteMe之前需要先安装必备库。
brew安装cmake python mono go nodejs
brew install cmake python mono go nodejs
三、安装vim
这里非常坑,youcompleteme要求必须支持python3.6的vim,下面这个命令可以查看vim是否支持python3.6
vim --version | grep python
带+的就是支持:
推荐brew重新下载一个,不用系统自带的vim
但是仅仅用官网给出来的下面这个命令,搞完之后,在编译的时候会报错(vim不支持python3.6):YouCompleteMe unavailable: requires Vim compiled with Python (3.6.0+) support
brew install vim
百度了一下,看大家都是在install vim的时候指定python3支持,但这种方法已经过时了,从Homebrew的1.6.0(2018-04-09)版本开始,默认的python版本就是3,而且实操输入下面的命令,会报错,不支持的flag
brew install vim --with-python3
后来在参考文献里面找到了解法,仅当python --version为版本3时,brew install vim才会安装具有python3支持的vim,不然默认是支持python2的vim。正确安装步骤走一波:
alias python=python3
brew install vim
vim --version|grep python
unalias python
参考:
https://qastack.cn/superuser/1115159/how-do-i-install-vim-on-osx-with-python-3-support
四、安装和编译YouCompleteMe
如果之前已经在第一步通过Vundle安装了YouCompleteMe源码,则不需要通过git下载源码了。否则需要从git拉下来源码:
cd ~/.vim/bundle
git clone https://github.com/Valloric/YouCompleteMe.git
cd YouCompleteMe
git submodule update --init --recursive
官方推荐下面一键傻瓜式编译(需要sudo权限):
cd ~/.vim/bundle/YouCompleteMe
python3 install.py --all
也可以根据需要支持的语言进行自定义编译:
翻译成脚本就是
cd ~/.vim/bundle/YouCompleteMe
python3 install.py --cs-completer # support C#
python3 install.py --go-completer # support go
python3 install.py --ts-completer # support JavaScript and TypeScript
python3 install.py --rust-completer # support rust
python3 install.py --java-completer # support java
python3 install.py --clangd-completer # support C++/C
运行install.py的时候,遇到了另一个问题:Your C++ compiler does not fully support C++17
参考下面的方式解决的:
sudo CXX=g++-8 python3 ./install.py --all
参考:
https://stackoverflow.com/questions/65284572/your-c-compiler-does-not-fully-support-c17
又来一个问题:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)
解决方案在此:
https://stackoverflow.com/questions/50236117/scraping-ssl-certificate-verify-failed-error-for-http-en-wikipedia-org