Ubuntu 16.04.04-desktop 中VIM安装YouCompleteMe插件

从了解YCM以来陆陆续续安装过两次了,都是出现各种问题,因为太费时间就没有安装;由于最近需要经常在Linux下写代码,所以在昨天又尝试安装了一次,终于安装成功了;这一次首先是用 full install guide,后来一直出错就回到用脚本安装,通过full install guide也更加了解了YCM的安装。在这里记录一下安装的详细过程,以备后续再次安装可以查阅。

安装环境:宿主机-Win7  VMware虚拟机-Ubuntu 16.04.04-desktop

安装步骤主要分为:①更新VIM版本到8.x ②安装Vundle ③在Vundle中安装YouCompleteMe插件 ④安装依赖并编译YCM ⑤配置.vimrc和.ycm_extra_conf.py文件。

首先进入YCM的官网,选择相应OS,查看相关安装步骤,网站地址如下:

https://valloric.github.io/YouCompleteMe/#ubuntu-linux-x64

 

一、 更新VIM到8.x

  由于YCM要求VIM版本至少要 Vim 7.4.1578以上,我们这里直接上VIIM8。

1、 进入Compile VIM From Source的github页面,查看说明。

2、安装依赖库,这里可能会提示有些库没有安装成功,只要sudo apt-get update更新一下再执行就好了。

sudo apt install -y libncurses5-dev libgnome2-dev libgnomeui-dev \
libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \
libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev \
python3-dev ruby-dev lua5.1 liblua5.1-dev libperl-dev git

3、移除原来老版本的VIM

sudo apt remove vim vim-runtime gvim

4、下载VIM源码

git clone https://github.com/vim/vim.git

5、进入源码编译

注意原文中这句:这里比较坑

Note for Ubuntu users: You can only use Python 2 or Python 3. If you try to compile vim with both python-config-dir and python3-config-dir, YouCompleteMe will give you an error YouCompleteMe unavailable: requires Vim compiled with Python (2.6+ or 3.3+) support, when you start VIM.

然后更改下面的的命令,把原说明中的关于python3的两行去掉,如下:

cd vim
./configure --with-features=huge \
							--enable-multibyte \
							--enable-rubyinterp=yes \
							--enable-pythoninterp=yes \
							--with-python-config-dir=/usr/lib/python2.7/config \

							--enable-perlinterp=yes \
							--enable-luainterp=yes \
							--enable-gui=gtk2 \
							--enable-cscope \
							--prefix=/usr/local
make VIMRUNTIMEDIR=/usr/local/share/vim/vim80

6、make安装

cd ~/vim
sudo make install

7、把VIM设置成默认的编辑器

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

8、测试VIM版本是否有升级到8.x,执行下面命令后如果显示VIM8.x则表示成功

vim --version

成功显示如下:

Ubuntu 16.04.04-desktop 中VIM安装YouCompleteMe插件_第1张图片

9、查看VIM是否支持Python(?是不是这么说),在full install guide有如下这句话,这里测试一下。

After you have made sure that you have Vim 7.4.1578+, type the following in Vim: :echo has('python') || has('python3'). The output should be 1. If it's 0, then get a version of Vim with Python support.

命令:

vim
:echo has('python') || has('python3')

如果你在第5步直接用那个网页上面说的命令执行,这里会输出0;进一步会导致后面出错。

 

 

二、安装Vundle

1、Vundle是一个VIM插件管理器,YCM官方推荐用Vundle来管理插件,我们就按官方的来

2、Vundle的github页面有相关安装步骤,按这个来就好,页面地址如下:

https://github.com/VundleVim/Vundle.vim#about

3、获取Vundle

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

4、在~/目录创建.vimrc(注意是隐藏文件) ,并设置,设置内容如下(上面页面上有讲的哦),注意前后空格比较多的那行,要额外添加那行才会安装YCM插件。

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



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

5、进入VIM,并输入下面命令开始安装插件。如何在VIM中执行命令:命令行输入vim进入vim——>输入:号——>输入命令,按回车键!

:Plugin 'Valloric/YouCompleteMe'

安装过程图:

Ubuntu 16.04.04-desktop 中VIM安装YouCompleteMe插件_第2张图片

6、安装完成是最下方会有提示:

The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). YCM core library not detected; you need to compile YCM before using it. Follow the instructions in the documentation.

由于YCM还没有安装好,先不管这个。

 

三、其他依赖安装及YCM编译

1、安装cmake

sudo apt-get install build-essential cmake

2、安装Python库相关

sudo apt-get install python-dev python3-dev

3、编译YCM(Compiling YCM with semantic support for C-family languages),这里可能比价慢,我等了大概十几分钟。

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

4、如果前一步没有报错的话就算安装成功了。下面就是配置.vimrc和.ycm_extra_conf.py。细心的可能会发现编译过程中有这个提示

NOT using clang-tidy for static analysis

通过查google了解到好像并不会有影响:

https://github.com/Valloric/YouCompleteMe/issues/3048

@52101107 YCM doesn't depend on clang-tidy. If you're talking about the cmake output then this is totally unrelated to the original issue and, second, you can just ignore that.

 

安装成功后命令行输出:

Ubuntu 16.04.04-desktop 中VIM安装YouCompleteMe插件_第3张图片

 

四、配置.ycm_extra_conf.py及.vimrc

1、在.vimrc中加入以下两行

let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples'
let g:ycm_server_python_interpreter='/usr/lib/python2.7'

我的.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'}



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

"

set nu



let g:ycm_server_python_interpreter='/usr/bin/python2.7'

let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'



"add C++11 support

let g:syntastic_cpp_compiler = 'g++'

let g:syntastic_cpp_compiler_options = '-std=c++11 -stdlib=libc++'



"开启语法高亮功能

syntax enable

syntax on



"指定配色方案为256色

set t_Co=256



"配置backspace的工作方式
set backspace=indent,eol,start


"选中高亮

set hls


"选中高亮

set hls


"高亮光标所在行

"set cul "cursorline 

"set cuc

"set go=             " 不要图形按钮  

"color desert     " 设置背景主题  

color ron     " 设置背景主题  

"color torte     " 设置背景主题  

set guifont=Courier_New:h10:cANSI   " 设置字体  

"autocmd InsertLeave * se nocul  " 用浅色高亮当前行  

"autocmd InsertEnter * se cul    " 用浅色高亮当前行 



"检测文件类型

filetype on

"针对不同的文件采取不同的缩进方式

filetype indent on

"允许插件

filetype plugin on

"启动智能补全

filetype plugin indent on




" 自动补全配置

set completeopt=longest,menu    "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)

autocmd InsertLeave * if pumvisible() == 0|pclose|endif "离开插入模式后自动关闭预览窗口

inoremap         pumvisible() ? "\" : "\"    "回车即选中当前项



let g:ycm_min_num_of_chars_for_completion=2     " 从第2个键入字符就开始罗列匹配项

let g:ycm_cache_omnifunc=0      " 禁止缓存匹配项,每次都重新生成匹配项

let g:ycm_seed_identifiers_with_syntax=1        " 语法关键字补全





"在注释输入中也能补全

let g:ycm_complete_in_comments = 1

"在字符串输入中也能补全

let g:ycm_complete_in_strings = 1

"注释和字符串中的文字也会被收入补全

let g:ycm_collect_identifiers_from_comments_and_strings = 0



nnoremap jd :YcmCompleter GoToDefinitionElseDeclaration " 跳转到定义处


//括号补全
inoremap ( ()i

inoremap [ []i

inoremap { {}i

inoremap < <>i

 

 

PS: 如果要在vim中让YCM识别你指定的头文件的内容,在~/.vim目录中的.ycm_extra_conf.py文件中(或者这个文件在别的目录也OK),加入:

'/home/ct/workspace/muduo',
'-isystem',

内容。

你可能感兴趣的:(VIM)