Vim自动补全神器:YouCompleteMe
安装这个插件因为一些错误的操作费了不少时间,把过程记录下,以后需要安装时避免浪费时间。
可参考http://blog.jobbole.com/58978/
一些准备工作:
1.首先安装vim:
sudo apt-get install vim
2.安装完成后,查看vim对python的支持
vim --version | grep python
发现是有对Python3的支持而没有对Python2的支持。
如果需要换成对python2的支持的话,可以安装py2包:
sudo apt-get install vim-nox-py2
安装完成后,再查看一下,可以发现现在换成对Python2的支持了。
如果需要的切换的话可以输入命令:
sudo update-alternatives --config vim
输入编号切换就可以了。
3.安装官方文档上说的,安装CMake和python-dev。
sudo apt-get install build-essential cmake
sudo apt-get install python-dev python3-dev
安装YouCompleteMe
1.首先安装vim的扩展管理器Vundle,利用Vundle来安装YCM。
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
安装完成后,可以安装Vundle的官方文档来配置.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.
" 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
然后使用:PluginInstall命令 就可以开始用Vundle进行管理了。
附一些Vundle指令:
:PluginList //列举
:PluginInstall //安装
:PluginUpdate //更新
:PluginSearch //查找
:PluginClean //清除
详细部分见文章末尾。
添加begin/end之间添加PluginInstall ‘Valloric/YouCompleteMe’,保存后,再执行命令:PluginInstall就可以开始安装YouCompleteMe了,但是由于YouCompleteMe直接安装是一件很不容易的事(别问我为什么!)。所以推荐先git下来,下载到~/.vim/bundle/:
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
git clone https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
注意:YouCompleteMe下载速度很慢,而且没有那么容易一次下载成功(如果有那是意外)。所以多次执行git submodule update –init –recursive ,直到没有报错。这样下载完成后,再添加PluginInstall ‘Valloric/YouCompleteMe’,保存后,再执行命令:PluginInstall就可以瞬间完成了。
如果需要添加C系语言支持,可以执行命令:
cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer
如果需要完全支持,可以执行命令:
cd ~/.vim/bundle/YouCompleteMe
./install.py --all
要注意的是,必须保证xbuild, go, tsserver, node, npm, and cargo这些工具包都已经安装了。
配置YCM
安装完成后,还需要对YCM进行配置。在官方文档中写有
YCM looks for a .ycm_extra_conf.py file in the directory of the opened
file or in any directory above it in the hierarchy (recursively);
when the file is found, it is loaded (only once!) as a Python module.
在~/.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' "配置全局路径
let g:ycm_confirm_extra_conf=0 "每次直接加载该文件,不提示是否要加载
同时要补全语言,要对.ycm_extra_conf.py进行修改:
在flags下添加
'-isystem',
'/usr/include',
'-isystem',
'usr/include/c++/5.4.0',
'-isystem',
'usr/include/x86_64-linux-gnu/c++',
并注释掉这一段:
try:
final_flags.remove( ‘-stdlib-libc++‘ )
except ValueError:
pass
其他配置及~/.vimrc中的配置可根据个人喜好。
到这里YCM就可以使用了
补充:
语法:PluginList
列举插件列表,这里面包括两类:
- vimrc文件里配置的插件
- 已经安装,但是没有在.vimrc文件里体现的插件。比如直接通过PluginInstall
{name}直接安装,如果已经安装成功,虽然.vimrc文件里没有,但是依然会在PluginList里显示
注意事项: - 要把PluginList的所有插件同步到.vimrc配置文件。这样下一次就可以直接安装了。
语法:PluginInstall
安装BundleList里的所有插件
注意事项:
1. PluginInstall后面如果加”!”,此时表示的是更新而不是安装,相当于下面的PluginUpdate
语法:PluginUpdate
更新BundleList里的所有插件
语法: PluginSearch
这里查询的是Vim scripts类型插件。其他插件都不在可搜索范围
这里的搜索,支持前后模糊,但是不支持正则
查找到后可以直接对插件进行生命周期的管理。
但是如果此时安装的插件,并不会在.vimrc配置文件里面,所以要同步BundleList列表到.vimrc
语法: PluginClean
清除没有用的插件
旧命令(仍然可以使用)
:BundleList //-列举出列表中(.vimrc中)配置的所有插件
:BundleInstall //-安装列表中全部插件
:BundleInstall! //-更新列表中全部插件
:BundleSearch foo /-查找foo插件
:BundleSearch! foo //-刷新foo插件缓存
:BundleClean //-清除列表中没有的插件
:BundleClean! //-清除列表中没有的插件过
.vimrc文件里设置需要添加的插件,但是这些插件的位置需要注意一下。
"所有需要添加的插件都必须放在这行之后
call vundle#begin()
"...其他设置
Plugin 'VundleVim/Vundle.vim'
"...其他设置
"所有需要添加的插件都必须放在这行之前
call vundle#end()
YouCompleteMe提供的其他功能
YCM除了提供了基本的补全功能,自动提示错误的功能外,还提供了类似tags的功能:
- 跳转到定义GoToDefinition
- 跳转到声明GoToDeclaration
以及两者的合体GoToDefinitionElseDeclaration
可以在.vimrc中配置相应的快捷键。
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
另外,YCM也提供了丰富的配置选项,同样在.vimrc中配置。
具体请参考https://github.com/Valloric/YouCompleteMe#options
let g:ycm_error_symbol = '>>'
let g:ycm_warning_symbol = '>*'
同时,YCM可以打开location-list来显示警告和错误的信息:YcmDiags。个人关于ycm的配置如下:
let g:ycm_error_symbol = '>>'
let g:ycm_warning_symbol = '>*'
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
nmap <F4> :YcmDiags<CR>
YCM提供的跳跃功能采用了vim的jumplist,往前跳和往后跳的快捷键为Ctrl+O以及Ctrl+I。