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
從上圖能夠看出老夫的 vim 版本是 8.0.49 且支持 python3 腳本但不支持 python2 腳本(截圖的倒數第 7,8 行)
若是你的打印結果中沒有相關 python 腳本信息,還能夠在 vim 中鍵入命令 :echo has('python') || has('python3')
,若結果是 1 則證實是支持的。dom
若不管是 vim 版本不知足條件或者是不支持 python 腳本,那麼就須要從源碼編譯安裝 vim 了,ide
在 vim 的配置文件 ~/.vimrc
中添加一行(在call vundle#begin()
和 call vundle#end()
之間)測試
call vundle#begin() . . . Plugin 'Valloric/YouCompleteMe’ . . . call vundle#end()
而後保存運行 vim 命令 :PluginInstall
安裝 須要特別注意的是這個時候可能等的時間會至關的長
這個時候打開 vim 可能則會有警告以下圖
若是你跟老夫同樣,等待 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
若是不須要 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.
ycm_core
庫須要:cmake python3-dev
YCM 的頂層目錄或者說根目錄應該是 ~/.vim/bundle/YouCompleteMe
$ mkdir ~/.ycm_build $ cd ~/.ycm_build
若是跳過第三步的話,則能夠直接運行
$ 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 文件)
# --config Release 這個構建選項進針對 Windows $ cmake --build . --target ycm_core --config Release
$ cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/
「 ~/.vimrc let g:ycm_server_python_interpreter='/usr/bin/python' let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
C#、Go、TypeScript、 JavaScript、 Rust support
因爲老夫對這些語言沒有太多的涉獵就不作介紹了。
首先新建一個 hello.c 文件,
能夠看到自動補全了頭文件,厲害了老夫的 YCM
而後在 vim 中運行 :YcmToggleLogs stderr
命令查看錯誤日誌,
沒有看到報錯信息,則證實 YCM 已成功安裝。
做者:Helperhaps - 極光
知乎專欄:極光日報