Hello World!

程序员那点事儿

首页 新随笔 联系 聚合 管理
  20 Posts :: 6 Stories :: 0 Comments :: 0 Trackbacks

-.YoucompleteMe还有很多强大的功能,有兴趣可以继续探索。我们需要以下几步

  •  先检查一下自己的虚拟机中是否有安装python,用vim试一下1 :echo has('python') 如果得到结果为1 就说明有(其实有没有都无所谓,再执行一遍安装命令绝对没错)

 

yum install python

  

  • 安装vundle,vundle是一款vim插件管理工具,使用它安装youcomplete很简单

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

  • 配置vundle

在vimrc中添加这样的配置语句

set nocompatible   filetype off   set rtp+=~/.vim/bundle/Vundle.vim   call vundle#begin()   Plugin 'gmarik/Vundle.vim'   Plugin 'Valloric/YouCompleteMe'   call vundle#end()   filetype plugin indent on  

 

  • 安装youcompleteme

去github上clone一下youcompleteme的代码

git clone https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe

然后在vim里面安装一下,执行

:PluginInstall 

看到有DONE!显示就好了

  • 编译youcomplete

这一步坑了我好久,网上好多抄袭的全是apt命令和dnf命令,不太适合我的centos7,用yum就好了!

yum install automake gcc gcc-c++ kernel-devel cmake yum install python-devel python3-devel

不管安没安装python在这两句命令执行之后你都有了

接下来就是编译的重头戏

cd ~/.vim/bundle/YouCompleteMe 
如果需要支持C类的补全,用下面的命令。
.
/install.py --clang-completer
如果需要支持golang的补全,用
./install.py --gocode-completer

其他:

  • C# support: install Mono and add --omnisharp-completer when calling ./install.py.
  • Go support: install Go and add --gocode-completer when calling ./install.py.
  • TypeScript support: install Node.js and npm then install the TypeScript SDK with npm install -g typescript.
  • JavaScript support: install Node.js and npm and add --tern-completer when calling ./install.py.
  • Rust support: install Rust and add --racer-completer when calling ./install.py.


  • 配置vim

修改vimrc文件

let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py   let g:ycm_seed_identifiers_with_syntax=1    " 语法关键字补全   let g:ycm_confirm_extra_conf=0   " 打开vim时不再询问是否加载ycm_extra_conf.py配置   inoremap <expr> <CR>  pumvisible() ? "\<C-y>" : "\<CR>"    "回车即选中当前项   set completeopt=longest,menu    "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)  


  • (可能会有)附件

有可能.ycm_extra_conf.py这个文件会自动就有,也许会没有,find一下,没找到的话就自己vim修改一下,以下是该文件内容,复制之前,友情提示,在vim输入

set paste

进入复制模式,这样子复制之后格式就不会乱了~

import os import ycm_core flags = [ '-Wall', '-Wextra', '-Wno-long-long', '-Wno-variadic-macros', '-fexceptions', '-stdlib=libc++', '-std=c++11', '-x', 'c++', '-I', '.', '-isystem', '/usr/include', '-isystem', '/usr/local/include', '-isystem', '/Library/Developer/CommandLineTools/usr/include', '-isystem', '/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1', ]  compilation_database_folder = ''  if os.path.exists( compilation_database_folder ):   database = ycm_core.CompilationDatabase( compilation_database_folder ) else:   database = None  SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]  def DirectoryOfThisScript():   return os.path.dirname( os.path.abspath( __file__ ) )  def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):   if not working_directory:     return list( flags )   new_flags = []   make_next_absolute = False   path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]   for flag in flags:     new_flag = flag      if make_next_absolute:       make_next_absolute = False       if not flag.startswith( '/' ):         new_flag = os.path.join( working_directory, flag )      for path_flag in path_flags:       if flag == path_flag:         make_next_absolute = True         break        if flag.startswith( path_flag ):         path = flag[ len( path_flag ): ]         new_flag = path_flag + os.path.join( working_directory, path )         break      if new_flag:       new_flags.append( new_flag )   return new_flags  def IsHeaderFile( filename ):   extension = os.path.splitext( filename )[ 1 ]   return extension in [ '.h', '.hxx', '.hpp', '.hh' ]  def GetCompilationInfoForFile( filename ):    if IsHeaderFile( filename ):     basename = os.path.splitext( filename )[ 0 ]     for extension in SOURCE_EXTENSIONS:       replacement_file = basename + extension       if os.path.exists( replacement_file ):         compilation_info = database.GetCompilationInfoForFile(           replacement_file )         if compilation_info.compiler_flags_:           return compilation_info     return None   return database.GetCompilationInfoForFile( filename )  def FlagsForFile( filename, **kwargs ):   if database:       compilation_info = GetCompilationInfoForFile( filename )     if not compilation_info:       return None      final_flags = MakeRelativePathsInFlagsAbsolute(       compilation_info.compiler_flags_,       compilation_info.compiler_working_dir_ )    else:     relative_to = DirectoryOfThisScript()     final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )    return {     'flags': final_flags,     'do_cache': True   }




在你的~/.vimrc的结尾添加:
let mapleader=","
nnoremap <leader>gc :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gd :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>

需要跳转到函数定义的时候,光标定位到函数,进入普通模式(狂按esc后的模式),输入,gd
其实直接输入,gg就可以了。

如果不添加上面这4句话,每次跳转需要 普通模式下输入
:YcmCompleter GoToDeclaration才可以,太长了。

其他命令 参考:
https://github.com/Valloric/YouCompleteMe#goto-commands

另外,前跳后跳就是Ctrl+o Ctrl+i

至此,基本上够用了。记录下。



(2)找到配置文件 .ycm_extra_conf.py 
网上大多说这个文件在YouCompleteMe/cpp/ycm下面,但是YouCompleteMe下面就没有cpp文件夹,其实它是在third_party/ycmd/cpp/ycm目录下。

$ cd  ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/
  • 1
  • 1

ls -a 即可看到。 
(3)自行在YoucompleteMe/中创建cpp/ycm目录,将 .ycm_extra_conf.py拷贝进去

$ cd  ~/.vim/bundle/YouCompleteMe
  • 1
  • 1
$ mkdir  cpp
  • 1
  • 1
$ mkdir  cpp/ycm
  • 1
  • 1
$ cp third_party/ycmd/cpp/ycm/.ycm_extra_conf.py  cpp/ycm/
  • 1
  • 1

4.修改.vimrc配置文件 
将下面的内容添加到.vimrc里面

" 寻找全局配置文件
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py'
" 禁用syntastic来对python检查
let g:syntastic_ignore_files=[".*\.py$"] 
" 使用ctags生成的tags文件
let g:ycm_collect_identifiers_from_tag_files = 1
" 开启语义补全
" 修改对C语言的补全快捷键,默认是CTRL+space,修改为ALT+;未测出效果
"let g:ycm_key_invoke_completion = '<M-;>'
" 设置转到定义处的快捷键为ALT+G,未测出效果
"nmap <M-g> :YcmCompleter GoToDefinitionElseDeclaration <C-R>=expand("<cword>")<CR><CR> 
"关键字补全
"let g:ycm_seed_identifiers_with_syntax = 1
" 在接受补全后不分裂出一个窗口显示接受的项
set completeopt-=preview
" 让补全行为与一般的IDE一致
set completeopt=longest,menu
" 不显示开启vim时检查ycm_extra_conf文件的信息
let g:ycm_confirm_extra_conf=0
" 每次重新生成匹配项,禁止缓存匹配项
let g:ycm_cache_omnifunc=0
" 在注释中也可以补全
let g:ycm_complete_in_comments=1
" 输入第一个字符就开始补全
let g:ycm_min_num_of_chars_for_completion=1
" 错误标识符
let g:ycm_error_symbol='>>'
" 警告标识符
let g:ycm_warning_symbol='>*'
" 不查询ultisnips提供的代码模板补全,如果需要,设置成1即可
" let g:ycm_use_ultisnips_completer=0
"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

上面的内容中,除了第一句寻找全局配置文件,其他的语句可以根据自己的需要更改、删除或添加。 
注:如果没有在第(3)步中自己创建cpp/ycm目录拷贝.ycm_extra_conf.py文件,则需要将第一句中的路径改为全局配置文件所在的具体路径,如下:

let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
  • 1
  • 2
  • 1
  • 2

5.保存退出.vimrc ,打开一个C/C++源程序,体验其自动补全效果。 
这里写图片描述 
还可以对C++的STL库智能补全: 
这里写图片描述

6. 
(1)配合上面安装的syntastic还可以语法检测 
这里写图片描述 
‘>>’指出有语法错误,但是检测速度太慢,没什么大用。

自我感觉这个语法自动检测很烦,可以禁用它: 
进入 /bundle/YouCompleteMe/plugin 
修改youcompleteme.vim中的: 
这里写图片描述
将如上图中的第141行的参数改为0就可以了。 
(2)YcmDiags插件可以显示错误或警告信息,可以设置F9为打开窗口的快捷键,在.vimrc中添加语句: 
这里写图片描述 
显示效果: 
这里写图片描述

7.添加头文件 
目前在include中,无法补全stdio.h等头文件,我们需要将/usr/include添加进去。路径添加到 ~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py 文件中的flags 数组中,每增加一个路径,前面要写’-isystem’。 
这里写图片描述

以后需要boost库等其他的补全,也需要将相应的路径添加进去。

-.YoucompleteMe还有很多强大的功能,有兴趣可以继续探索。




posted on 2017-05-08 09:47 hello wold! 阅读(4421) 评论(0)  编辑 收藏 引用 所属分类: 编程相关

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理