CentOS7.2安裝Vim8和YouCompleteMe


1.環境

本文使用VMWare虛擬機進行實驗,客戶機系統是CentOS 7.2最小安裝(CentOS-7-x86_64-Minimal-1511.iso)

最終實現效果:安裝vim8 + python2.7(自帶)+ ycm,實現C/C++的智能提示、補全、跳轉。

 

2.需求

  • Git
  • GCC
  • CMake(3.10以上)
  • Vim
  • Vundle
  • YouCompleteMe
  • 各種依賴庫

 

3.安裝

3.1 下載安裝必要的組件

yum install -y gcc gcc-c++ ruby ruby-devel lua lua-devel  \
    ctags git python python-devel \
    tcl-devel ncurses-devel \
    perl perl-devel perl-ExtUtils-ParseXS \
    perl-ExtUtils-CBuilder \
    perl-ExtUtils-Embed

 

3.2 安裝Vim8

訪問https://github.com/vim/vim/releases,下載最新的release版本,我這里是vim-8.0.1645.tar.gz

然后解壓配置編譯安裝

tar zxvf vim-8.0.1645.tar.gz
cd vim-8.0.1645

./configure --with-features=huge \
            --enable-multibyte \
            --enable-rubyinterp=yes \
            --enable-pythoninterp=yes \
            --with-python-config-dir=/usr/lib64/python2.7/config \
            --enable-perlinterp=yes \
            --enable-luainterp=yes \
            --enable-cscope \
            --prefix=/usr/local
            
make VIMRUNTIMEDIR=/usr/local/share/vim/vim80
make install

 注意: --with-python-config-dir這項要看自己的實際路徑,是python-devel帶的目錄

 

更改下系統編輯器

sudo update-alternatives --install /usr/bin/editor editor /usr/local/bin/vim 1
sudo update-alternatives --set editor /usr/local/bin/vim
sudo update-alternatives --install /usr/bin/vi vi /usr/local/bin/vim 1
sudo update-alternatives --set vi /usr/local/bin/vim

 

3.3 安裝YouCompleteMe

3.3.1 下載Vundle

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.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' 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

 

3.3.2 下載YouCompleteMe

打開vim,輸入如下命令

:PluginInstall

注意:這里很考驗網速,可能會比較慢,下載好后,整個.vim文件夾大約150~250MB(根據時間版本不同)。

 

3.3.3 下載CMake(yum安裝的2.8版本不行)

訪問https://cmake.org/download/,我這里是cmake-3.10.3.tar.gz

解壓編譯安裝

tar zxvf cmake-3.10.3.tar.gz
cd cmake-3.10.3
./bootstrap && make && make install

 

 3.3.4 編譯YouCompleteMe(支持C/C++)

cd ~/.vim/bundle/YouCompleteMe/
./install.py --clang-completer

編譯完成后,整個.vim文件夾大約350MB。

 注意:如果使用--clang-completer選項,腳本會判斷當前是否有upstream pre-build libclang.so庫(不建議用系統的libclang):

 如果有則用緩存的(見緩存位置);如果沒有則會去下載庫(見下載地址)。

 看網絡狀況,可能會下載不全(sha256值不對),導致下面編譯YouCompleteMe時出錯。可以先用迅雷下載,再替換過去。

下載地址:https://dl.bintray.com/micbou/libclang/libclang-6.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.bz2

緩存位置:.vim/bundle/YouCompleteMe/third_party/ycmd/clang_archives/

3.3.5 配置YouCompleteMe

編輯~/.vimrc,增加以下內容

let g:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'  "設置全局配置文件的路徑
let g:ycm_seed_identifiers_with_syntax=1    " 語法關鍵字補全
let g:ycm_confirm_extra_conf=0  " 打開vim時不再詢問是否加載ycm_extra_conf.py配置
let g:ycm_key_invoke_completion = '<C-a>' " ctrl + a 觸發補全,防止與其他插件沖突
set completeopt=longest,menu    "讓Vim的補全菜單行為與一般IDE一致(參考VimTip1228)
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR> "定義跳轉快捷鍵

 

第一行所需的.ycm_extra_conf.py,可以從自帶的復制一份過來,再根據需要進行配置

cp .vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py ~

 

4.運行

現在運行vim已經可以對C++進行提示補全了(因為剛才是從cpp子目錄下拷貝的配置樣例),

打開~/.ycm_extra_conf.py可以看到其中flags變量,就是配置項目代碼補全提示的

比如(C++項目)

flags = [
'-Wall',
'-Wextra',
'-Werror',
'-fexceptions',
'-DNDEBUG',
'-std=c++11', '-x', 'c++', '-isystem', '/usr/include', '-isystem', '/usr/local/include', '-I', '/root/my_proj',
]

如果是C項目,需要調整

-std=c99,-x下面的參數改為c。

 

解析C/C++相關的符號提示(系統的、三方庫的、自己的),需要配置-isystem,'-I',參數為頭文件所在目錄。

配置好后,用vim編輯一個C文件,

輸入#include<會自動彈出提示窗

 

 

結構體輸入.時會提示成員,

 

結構體指針輸入->時會提示成員

 

 YouCompleteMe默認不會像IDE那樣主動提示符號補全(如輸入pri提示printf),想要補全時按Ctrl+A(見上面配置)。

 

 

以上彈出提示窗,按Ctrl+N、Ctrl+P上下選擇補全。

 

符號跳轉

光標定位到需要跳轉的符號上,輸入\jd(\是默認leader),實現跳轉(該跳轉有條件,不一定100%成功,可見官網說明

 

在printf上按\jd,跳轉到定義

 

 

按Ctrl+o跳回之前的位置。

 

5.其他

實際使用每個項目單獨一個.ycm_extra_conf.py配置,GitHub上有個項目可以自動生成配置文件, 有需要可以參考下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM