Ubuntu18.04配置vim——利用Vundle安裝YouCompleteMe(有彩蛋)

喵哥前段時間嘗試在centos6上安裝YCM失敗了,主要是因爲衆多依賴軟件的版本太低,搞得喵哥全無興致,好歹也是記錄了一下路上遇到的坑位。詳情可以到:centos6.10安裝VIM插件YOUCOMPLETEME的坑位標記參考。

今天,喵哥打算把宿舍筆記本的vim給配置了。

筆記本的系統是Ubuntu18.04,之前用的時候把機器上的vim、Python都更新了一遍,目前的狀況大致如下:

軟件 版本
Python 3.7.1
gcc 7.3.0
vim 8.0
git 2.17.1
cmake 未安裝

除了cmake外,其餘的版本在我印象中是夠用了,這些軟件的安裝教程在網上還是蠻多的,如果發現自己的版本不對,建議先去安裝好。

先安裝cmake:

wget --no-check-certificate https://cmake.org/files/v3.8/cmake-3.8.0-rc2.tar.gz
tar xf cmake-3.8.0-rc2.tar.gz
cd  cmake-3.8.0-rc2
./bootstrap
make -j 4
sudo make install 

插一句,make的時候用-j 4加速是真的爽,之前一直在單核的容器中跑,用-j 4反而更慢。

從github上下載Vundle

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

以官方(https://github.com/VundleVim/Vundle.vim)的提供的.vimrc作爲初始模板做了部分添加,主角是:

Plugin 'Valloric/YouCompleteMe'

.vimrc通常保存在當前用戶的目錄下,便於管理。其實在/etc/vim目錄下有一個vimrc的全局配置文件,也可以在這裏添加vim的配置命令。喵哥把文件保存在~/下,vim  ~/.vimrc。

set nocompatible              " be iMproved, required
filetype off                  " required
set nu
set hls
syntax enable

syntax on

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

"YCM
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

在終端輸入vim,進入到vim在普通模式(非插入模式)下輸入

:PluginInstall

就可以開始下載.vimrc中配置的插件了,如果想快點的話,可以只在配置文件中保留

Plugin 'Valloric/YouCompleteMe'

下載YCM的大小是303M左右,爲了查看下載進度,可以用

du -h ~/.vim/bundle/YouCompleteMe

查看下載了多少。

下載YCM完成後,進入到YCM的目錄下,開始安裝:

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

安裝成功後,在~/.vimrc中添加:

" YouCompleteMe
set runtimepath+=~/.vim/bundle/YouCompleteMe
let g:ycm_collect_identifiers_from_tags_files = 1           " 開啓 YCM 基於標籤引擎
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 註釋與字符串中的內容也用於補全
let g:syntastic_ignore_files=[".*\.py$"]
let g:ycm_seed_identifiers_with_syntax = 1                  " 語法關鍵字補全
let g:ycm_complete_in_comments = 1
let g:ycm_confirm_extra_conf = 0
let g:ycm_key_list_select_completion = ['<c-n>', '<Down>']  " 映射按鍵, 沒有這個會攔截掉tab, 導致其他插件的tab不能用.
let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
let g:ycm_complete_in_comments = 1                          " 在註釋輸入中也能補全
let g:ycm_complete_in_strings = 1                           " 在字符串輸入中也能補全
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 註釋和字符串中的文字也會被收入補全
let g:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'
let g:ycm_show_diagnostics_ui = 0                           " 禁用語法檢查

我把YCM的配置文件放在當前用戶根目錄下,這裏原本是沒有的文件的,自己添加就好了:

vim ~/.ycm_extra_conf.py

由於喵哥主要用C++,所以這裏暫時添加C++的相關項目:

flags = [
'-Wall',
'-Wextra',
'-Werror',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-DNDEBUG',
'-std=c++11',
'-x',
'c++',
'-I',
'/usr/include',
'-isystem',
'/usr/lib/gcc/x86_64-linux-gnu/5/include',
'-isystem',
'/usr/include/x86_64-linux-gnu',
'-isystem'
'/usr/include/c++/5',
'-isystem',
'/usr/include/c++/5/bits'
]

對於vim找不到的頭文件就可以在這裏面添加路徑。

到這裏基本YCM就配置好了,Ctrl+Space彈出補全對象,Ctrl+J跳轉至定義處。


孤陋寡聞了,很牆裂地推薦大家參考這兩篇文章,真的很強!

https://www.jianshu.com/p/75cde8a80fd7

https://blog.csdn.net/Mason_Mao/article/details/80984726

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章