Vim 自动补全--YouCompleteMe

前端之家收集整理的这篇文章主要介绍了Vim 自动补全--YouCompleteMe前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Vim 自动补全--YouCompleteMe

一、安装vim的插件管理器Vundle
1.如果自己用户目录下不存在.vim/bundle,可以使用如下命令进行创建

mkdir -p ~/.vim/bundle

2.把Vbundle从github上clone下来,命令如下(如果之前未安装git,请安装后执行如下命令)

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

3.在自己用户目录下的.vimrc中添加如下内容

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vundle 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' " 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/'}
" Avoid a name conflict with L9 "Plugin 'user/L9',{'name': 'newL9'}

" 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


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

4.保存上述内容后运行,在vim中执行如下命令

:PluginInstall

5.出现的界面如下图

选定你要安装的YouCompleteMe插件回车,安装其他插件的方式雷同。
6.等待插件安装的过程比较漫长,当装太由Processing变为done!时说明插件安装成功了。
安装过程如下图

成功后如下图
那个
7.如果上一步那个漫长过程成功了,说明你的YouCompleteMe已经下载成功了,可以对其进行编译了
(1)首先安装编译需要的工具(最好提前安装上python,如果安装过程中出现失败,请结合系统单个工具单独安装,或百度查找其他安装方式),已经安装的请忽略这步。

sudo apt-get install gcc g++ cmake python-dev ctags

(2)工具安装完成后开始进行编译工作

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

这个过程也是比较慢的,因为在编译过程中他会去下载clang相关内容并对其进行编译最终生成ycm_support_libs
编译配置过程

下载过程

下载并编译完成

8.上述过程都成功执行之后,你应该处于如下目录里面

~/.vim/bundle/YouCompleteMe/

如果不在那个目录下执行如下命令

cd ~/.vim/bundle/YouCompleteMe/

把执行如下命令,把.ycm_extra_conf.py(隐藏文件)文件拷贝到你自己的代码目录下:

cp third_party/ycmd/examples/.ycm_extra_conf.py /home/fas/workplace/FAS/

其中FAS为我的C++开源服务器框架代码文件夹名。
到自己的代码目录下配置.ycm_extra_conf.py文件比葫芦画瓢就行了,一下是.ycm_extra_conf.py的部分内容,其中自己的代码路径可以有n个。

# These are the compilation flags that will be used in case there's no
# compilation database set (by default,one is not set).
# CHANGE THIS LIST OF FLAGS. YES,THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall','-Wextra','-Werror','-Wc++98-compat','-Wno-long-long','-Wno-variadic-macros','-fexceptions','-DNDEBUG',# You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM
# source code needs it.
'-DUSE_CLANG_COMPLETER',# THIS IS IMPORTANT! Without a "-std=<something>" flag,clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project,you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project,you would set this to 'c' instead of 'c++'.
'-x','c++','-isystem','../BoostParts',# This path will only work on OS X,but extra paths that don't exist are not
# harmful
'/System/Library/Frameworks/Python.framework/Headers','../llvm/include','../llvm/tools/clang/include','-I','自己代码路径1','自己代码路径2','自己代码路径3','./ClangCompleter','./tests/gmock/gtest','./tests/gmock/gtest/include','./tests/gmock','./tests/gmock/include',]

9拓展内容,你在使用vim的过程中,如果有什么地方感觉不太符合自己的需求可以参考下拓展内容

" YCM " 允许自动加载.ycm_extra_conf.py,不再提示                         
let g:ycm_confirm_extra_conf=0                                     
" 补全功能在注释中同样有效 let g:ycm_complete_in_comments=1 " 开启tags补全引擎                                                 
let g:ycm_collect_identifiers_from_tags_files=1                    
" 键入第一个字符时就开始列出匹配项 let g:ycm_min_num_of_chars_for_completion=1 " YCM相关快捷键,分别是\gl,\gf,\gg                                                    
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>              
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>               
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>

其中,

g:ycm_confirm_extra_conf如果不设为0的话,用vim每次打开代码文件,YCM都会提示是否确认加载某某.ycm_extra_conf.py文件;
ycm_collect_identifiers_from_tags_files设为1,指定YCM收集传统tags文件的信息,另外当tags改变时,YCM会重新索引它们。YCM对tags文件的格式有特殊的要求:The only supported tag format is the Exuberant Ctags format. The format from "plain" ctags is NOT supported. Ctags needs to be called with the --fields=+l option (that's a lowercase L,not a one) because YCM needs the language:<lang> field in the tags output。如有问题请参阅YCM官网相关FAQ;
比如引入C++tags:
 " 引入 C++ 标准库tags set tags+=/data/misc/vim/stdcpp.tags
3个快捷键设置是用来快速跳转到符号声明或定义的。我往往只用gg,即将光标定位到某个符号上,按\键后(默认的leader键是\),快速按两次g键,将跳转到该符号的声明或定义位置(Tips: 按<ctrl>+o返回)

其他技巧:

执行:YcmDiags显示所有错误,它其实是打开了一个location list窗口,也可能通过:lopen打开. 使用:lclose可以关闭
有些系统函数如fopen,strcpy如果不智能提示,可以按<Ctrl>+<Space>键。
如果有的文件死活无法正确提示(如果结构体之类),请检查一下文件编码,改成utf-8。(vim中 :set fileencoding=utf8)

猜你在找的Bash相关文章