宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

YouCompleteMe 是一款优秀的 vim 自动补全插件。

虽然越来越多的使用 Visual Studio, VSCode, CLion, Android Studio 这些重量级 IDE 来写代码,但偶尔还是要用 vim。我觉得 YouCompleteMe 最有用的地方大概是 路径补全

每次重新配置 Vim 环境,下载 YouCompleteMe 难以成功,其原因包括两方面:所处网络环境不佳;巨大的下载量里很多可以丢弃。

(1)网络不佳问题的解决

我在 gitee.com 上创建了 ycm-core 组织的账号,把 YouCompleteMe 和 ycmd 两个 repo 依赖的 submodule 都导入到这个账号里了(当然,能在 https://gitee.com/mirrors 找到的没有放)。

只要修改 YouCompleteMe 和 ycmd 的 .gitmodules 文件,然后拉取子模块,理论上就没大问题了,直接从 gitee.com 下载是很快的。

唯一的缺点是,submodule 递归出现的时候,手动改起来很麻烦。对于 YCM 来说,还算可以接受;而像 PyTorch 一样的巨无霸项目就比较繁琐了。不过,可以通过自行编写脚本的方式,把手动的过程用程序实现一定的自动化,也就是用 DFS 搜索算法处理;这里就不献丑放出 python 脚本代码了,有需要的朋友可以邮件请求。

(2)避免下载不必要的内容

完整的 YouCompleteMe 下载后大概 252MB 大小,实际上它所有涉及到的 git repo 里的历史 commit,我们一概不感兴趣也用不到。能不能不要这些“历史包袱“的 commit,只拉取最新的(或者 submodule 注册时指定的那个 commit)?

想到 git clone 时可以指定 --depth 1,那么 git submodule 也应该有类似的参数;只不过测试下来,低版本的 git 试了2.25.1)失败的时候比较多;升级 git 到 2.30.0 或更高,只有一个submodule失败(一个在 bitbucket 上的 repo, mrab-regex)。完整命令如下:

export PATH=/home/zz/soft/git-2.30/libexec/git-core:$PATH
cd ~/.vim_runtime/plugged

# git clone --depth 1 https://github.com/ycm-core/YouCompleteMe --shallow-submodules  # 会失败,暂时不用

git clone --depth 1 https://github.com/ycm-core/YouCompleteMe

cd YouCompleteMe

#git submodule update --init --depth 1 --recursive #某一个submodule失败导致整体失败,目前不用它

git submodule update --init --depth 1 third_party/requests_deps/certifi third_party/requests_deps/chardet third_party/requests_deps/idna third_party/requests_deps/requests third_party/requests_deps/urllib3 third_party/ycmd

cd third_party/ycmd

git submodule update --init --depth 1 third_party/bottle third_party/jedi_deps/numpydoc third_party/jedi_deps/jedi third_party/jedi_deps/parso third_party/requests_deps/certifi third_party/requests_deps/chardet third_party/requests_deps/idna third_party/requests_deps/requests third_party/requests_deps/urllib3 third_party/waitress third_party/watchdog_deps/pathtools third_party/watchdog_deps/watchdog

git submodule update --init --depth 100 third_party/mrab-regex

git submodule update --init --depth 1 --recursive

此时如果你的 GCC 版本不满足 YCM 的最低要求,需要装新版 GCC。在Ubuntu 20.04上默认 GCC 已经满足;在Ubuntu 16.04上则需要:

sudo apt install gcc-9 g++-9
alias gcc='/usr/bin/gcc-9'
alias g++='/usr/bin/g++-9'

然后执行安装:

./install.py --clangd

最小化源码下载占了 54MB 空间,编译 clangd 后占据168MB 空间。

最后,在 vimrc 相关配置中开启YCM插件,以及配置必要的配置:

Plug 'ycm-core/YouCompleteMe', { 'do': './install.py'  }

Greatness is never a given, it must be earned.