当你有意读完这篇文章的时候,说明你已经知道YouCompleteMe的神奇了。我的系统是Ubuntu,语言是Python,如果你是C系列的,请继续Google吧。
Vundle
说起安装插件,少不了Vundle。
安装
创建目录:
cd ~
mkdir .vim
cd .vim
mkdir bundle
源码安装:
sudo git clone git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
配置vimrc
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
call vundle#end()
filetype plugin indent on
安装插件
重启vim,输入命令:
:PluginInstall
这样就安装好了Vundle。
YouCompleteMe
安装
为了保险,我们使用源码安装。
源码安装
命令行输入:
sudo git clone https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
配置vimrc
在上一步的配置vimrc中,将
Plugin 'Valloric/YouCompleteMe'
插入到call vundle#begin()
和call vundle#end()
之间。
然后重复上一步安装插件的命令,安装好会提示Done!
编译
输入命令:
cd ~/.vim/bundle/YouCompleteMe
sudo ./install.sh
等待编译完成。
配置文件
安装好YCM后,还不能代码补全,我们还需要配置.ycm_extra_conf.py
文件,如果没有特殊要求,YCM自带的配置文件已经够用,输入命令:
cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/
最后一步
再次配置vimrc
"默认配置文件路径"
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py'
"打开vim时不再询问是否加载ycm_extra_conf.py配置"
let g:ycm_confirm_extra_conf=0
set completeopt=longest,menu
"python解释器路径"
let g:ycm_path_to_python_interpreter='/usr/bin/python'
"是否开启语义补全"
let g:ycm_seed_identifiers_with_syntax=1
"是否在注释中也开启补全"
let g:ycm_complete_in_comments=1
let g:ycm_collect_identifiers_from_comments_and_strings = 0
"开始补全的字符数"
let g:ycm_min_num_of_chars_for_completion=2
"补全后自动关机预览窗口"
let g:ycm_autoclose_preview_window_after_completion=1
" 禁止缓存匹配项,每次都重新生成匹配项"
let g:ycm_cache_omnifunc=0
"字符串中也开启补全"
let g:ycm_complete_in_strings = 1
"离开插入模式后自动关闭预览窗口"
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
"上下左右键行为"
inoremap <expr> <Down> pumvisible() ? '\<C-n>' : '\<Down>'
inoremap <expr> <Up> pumvisible() ? '\<C-p>' : '\<Up>'
inoremap <expr> <PageDown> pumvisible() ? '\<PageDown>\<C-p>\<C-n>' : '\<PageDown>'
inoremap <expr> <PageUp> pumvisible() ? '\<PageUp>\<C-p>\<C-n>' : '\<PageUp>'
这样就大功告成了,赶快打开vim试试吧!
网友评论