一步一步带你安装史上最难安装的 vim 插件

YouCompleteMe is a fast, as-you-type, fuzzy-search code completion engine for Vim.
参考: https://github.com/Valloric/YouCompleteMe#full-installation-guide
本篇文章默认读者知道什么是 unix/linux,vim/vi, YouCompleteMe,若是有不清楚的,Search engine is your friend 或者留言讨论。YouCompleteMe 简称 YCM 如下都称为 YCM。html

YouCompleteMe 的强大想必你们都据说过,简单的用一句话来归纳的话,媲美 VS 的自动补全 固然可能有一点点过,但可见其强大。可是做为一个十分强大的 vim 插件,他但是十分的难以安装,几乎全部的人,都会在安装阶段苦苦挣扎(别问老夫怎么知道的),而后要么成功安装享受工具带来的便利,要么放弃继续使用传统的自动补全工具(固然也没有那么差,但可能没有 YCM 那么强大)。python

通过老夫的辛苦探索,终于在听到 YCM 大名的一年以后成功安装,因此分享出来,但愿可以帮助到更多的人。另外若是在安装过程当中遇到任何问题都欢迎在评论与我讨论。linux

有了你我才是我,你的存在令我完整
—— 老夫git

系统环境:Kali-Rolling on Oracle VirtualBox,能够看成是最新版(或者说实验版)的 Debiangithub

目标:成功安装使用 YCM, 尽可能少的编译软件(即尽可能使用预编译 deb 包),尽可能能够实现更多种语言的自动补全vim

安装步骤:

0. 确保 vim 版本是 7.4.143 或以上,而且支持 python 2/3 脚本


vim version

从上图能够看出老夫的 vim 版本是 8.0.49 且支持 python3 脚本但不支持 python2 脚本(截图的倒数第 7,8 行)
若是你的打印结果中没有相关 python 脚本信息,还能够在 vim 中键入命令 :echo has('python') || has('python3'),若结果是 1 则证实是支持的。dom

若不管是 vim 版本不知足条件或者是不支持 python 脚本,那么就须要从源码编译安装 vim 了,ide

1. 经过 Vundle 来安装 YCM(官方推荐)

快速安装 vim 的插件管理插件 vundle工具

在 vim 的配置文件 ~/.vimrc 中添加一行(在call vundle#begin() 和 call vundle#end() 之间)测试

call vundle#begin()
. . . 
Plugin 'Valloric/YouCompleteMe’
. . .
call vundle#end()

而后保存运行 vim 命令 :PluginInstall 安装 须要特别注意的是这个时候可能等的时间会至关的长

这个时候打开 vim 可能则会有警告以下图


vim warning

1.5 经过 Git 安装 YCM

若是你跟老夫同样,等待 Vundle 安装 YCM 等了很久终于貌似好像成功了,打开 vim 却发现 YouCompleteme unavailable : no module named future (固然没有遇到算你运气好),那么你应该考虑一下换用 Git 来安装 YCM:

# 下载 (在 `~/.vim/bundle` 目录下)
$ git clone --recursive [https://github.com/Valloric/YouCompleteMe.git](https://github.com/Valloric/YouCompleteMe.git)
# 检查完整性(在 `~/.vim/bundle/YouCompleteMe` 目录下)
$ git submodule update --init --recursive

2. 下载安装最新版的 libclang

若是不须要 C 家族的语义化补全,则可跳过这一步

# apt-get install llvm-3.9 clang-3.9 libclang-3.9-dev libboost-all-dev

PS: YCM 官方墙烈推荐使用上游编译的二进制文件代替系统自带的 libclang

You can use the system libclang only if you are sure it is version 3.9 or higher, otherwise don't. Even if it is, we recommend using the official binaries from llvm.org if at all possible. Make sure you download the correct archive file for your OS.

We STRONGLY recommend AGAINST use of the system libclang instead of the upstream compiled binaries. Random things may break. Save yourself the hassle and use the upstream pre-built libclang.

3. 编译构建 ycm_core 库

须要:cmake python3-dev

YCM 的顶层目录或者说根目录应该是 ~/.vim/bundle/YouCompleteMe

  • 建立一个目录放编译过程当中产生的文件
$ mkdir ~/.ycm_build
$ cd ~/.ycm_build
  • 生成 makefile

若是跳过第三步的话,则能够直接运行

$ cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

若是没有跳过第三步,说明须要 c 家族的语义化补全则需运行

$ cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=ON DUSE_SYSTEM_LIBCLANG=ON . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

有问题的编译

编译经过的,没有报错,可是这个编译结果是有问题的,看第 3 行,反复强调的 c 家族的语义补全没有了。通过老夫反复的检查,原来是上面的编译命令少了一个 - 符号

$ cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=ON -DUSE_SYSTEM_LIBCLANG=ON . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

这个时候编译的过程以下图:


编译正常

其实这个仍是有问题的,看倒数第 三、4 行,使用的是外部的 libclang-3.6 ,第 2 步中说的系统安装的 libclang 最低要是是 3.9。
这个时候就须要删除 ~/.ycm_build 目录下的全部文件,使用下面的命令指定 libclang 的版本从新编译。

$ cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=ON -DEXTERNAL_LIBCLANG_PATH=/usr/lib/x86_64-linux-gnu/libclang-3.9.so . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

编译的结果是以前新建的目录(也就是当前目录下生成了一些 Makefile 文件)


Makefile
  • 构建 ycm_core
#  --config Release 这个构建选项进针对 Windows
$ cmake --build . --target ycm_core --config Release

build ycm_core

3.5 配置

  • 复制 .ycm_extra_conf.py 文件
$ cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/
  • 添加 vim 配置
    注意下面的 python 解释器的路径要和编译 ycm_core 的时候使用的 python 解释器是相同的版本(2 或 3)
“ ~/.vimrc
let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'

4. 其余语言的支持

C#、Go、TypeScript、 JavaScript、 Rust support
因为老夫对这些语言没有太多的涉猎就不作介绍了。

5. 测试安装

首先新建一个 hello.c 文件,


hello.c

能够看到自动补全了头文件,厉害了老夫的 YCM

而后在 vim 中运行 :YcmToggleLogs stderr 命令查看错误日志,


stderr

没有看到报错信息,则证实 YCM 已成功安装。

做者:Helperhaps - 极光

原文:使人眼前一亮的下拉式终端 Tilda & Guake

知乎专栏:极光日报

相关文章
相关标签/搜索