1. 程式人生 > >CENTOS7安裝VIM外掛YOUCOMPLETEME

CENTOS7安裝VIM外掛YOUCOMPLETEME

YouCompleteMe這個Vim外掛還真不好安裝,挺多坑的,折騰了挺久終於最後用上了。

測試環境:

[broly@localhost ~]$ cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)

一、更新Vim

我安裝這個外掛的時候,提示:YouCompleteMe unavailable : YouCompleteMe unavailable: requires Vim 7.4.1578+

所以不得不把系統自帶的Vim更新到8.0

# 移除舊版本
sudo yum remove vim -y
# 安裝必要元件
sudo yum install ncurses-devel python-devel -y # 下載原始碼編譯安裝 git clone https://github.com/vim/vim.git cd vim/src # 根據自己實際情況設定編譯引數 ./configure --with-features=huge --enable-pythoninterp=yes --enable-cscope --enable-fontset --with-python-config-dir=/usr/lib64/python2.7/config make sudo make install

編譯引數說明:
–with-features=huge:支援最大特性
–enable-rubyinterp:開啟對ruby編寫的外掛的支援
–enable-pythoninterp:開啟對python編寫的外掛的支援
–enable-python3interp:開啟對python3編寫的外掛的支援
–enable-luainterp:開啟對lua編寫的外掛的支援
–enable-perlinterp:開啟對perl編寫的外掛的支援
–enable-multibyte:開啟多位元組支援,可以在Vim中輸入中文
–enable-cscope:開啟對cscope的支援
–with-python-config-dir=/usr/lib64/python2.7/config 指定python 路徑
–with-python-config-dir=/usr/lib64/python3.5/config 指定python3路徑

注意:必須帶上Python編寫外掛支援,最好帶上Python路徑,否則使用時會報這個錯誤:YouCompleteMe unavailable: requires Vim compiled with Python (2.6+ or 3.3+) support

編譯安裝好Vim後,默然是安裝在/usr/local/bin目錄下,所以把該目錄加入到PATH方便terminal全域性使用:

sudo nano /etc/profile

在尾部新增:

export PATH=$PATH:/usr/local/bin/vim

然後使環境變數生效:

source /etc/profile

二、安裝Vundle和YouCompleteMe

安裝步驟很簡單:

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

編輯配置檔案

vim ~/.vimrc
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'

" 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'}

Bundle 'altercation/vim-colors-solarized'
Plugin 'vim-scripts/OmniCppComplete'
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

syntax enable
colorscheme monokai

let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'

這裡要注意加上, 在我自己的vimrc上

let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'

執行外掛安裝

vim +PluginInstall +qall

三、增加配色方案

下載對應的monokai.vim 檔案 放到 ~/.vim/colors/ 即可

mkdir ~/.vim/colors
wget -O ~/.vim/colors/monokai.vim https://raw.githubusercontent.com/sickill/vim-monokai/master/colors/monokai.vim

四、安裝Clang

(此步驟可選,因為安裝這個是為了C語言家族語法補全功能)
原文說的是這個步驟可選,但是如果不安裝Clang,可能會出現 bug:the ycmd server SHUT DOWN (restart with ‘:…low the instructions in the documentation.

sudo yum install cmake -y
cd ~/.vim/plugin/YouCompleteMe  
./install.py --clang-completer

五、測試

打開個cpp檔案,測試下是否還會報錯吧~~

vim demo.cpp