ubuntu下安装自动补全YouCompleteMe

一、安装预备软件。
#vim要带python2.7的支持,curl是下载插件必须用到的软件,还有git

apt install vim-nox-py2 curl git

#安装python头文件

apt install python-dev python3-dev

#安装c/c++编译包

apt install build-essential

#安装cmake,编译YCM时候要用到。

apt install cmake

二、安装vim插件管理工具Vundle

1、下载Vundle到制定目录

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

2、编写适用Vundle最小的配置文件:vim ~/.vim/vimrc

set nocompatible              " 去除VI一致性,必须
filetype off                  " 必须

" 设置包括vundle和初始化相关的runtime path
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" 让vundle管理插件版本,必须
Plugin ‘VundleVim/Vundle.vim‘

" 以下范例用来支持不同格式的插件安装.
" 请将安装插件的命令放在vundle#begin和vundle#end之间.
" Github上的插件
" 格式为 Plugin ‘用户名/插件仓库名‘
Plugin ‘tpope/vim-fugitive‘
" 来自 http://vim-scripts.org/vim/scripts.html 的插件
" Plugin ‘插件名称‘ 实际上是 Plugin ‘vim-scripts/插件仓库名‘ 只是此处的用户名可以省略
Plugin ‘L9‘
" 由Git支持但不再github上的插件仓库 Plugin ‘git clone 后面的地址‘
Plugin ‘git://git.wincent.com/command-t.git‘
" 本地的Git仓库(例如自己的插件) Plugin ‘file:///+本地插件仓库绝对路径‘
Plugin ‘file:///home/gmarik/path/to/plugin‘
" 插件在仓库的子目录中.
" 正确指定路径用以设置runtimepath. 以下范例插件在sparkup/vim目录下
Plugin ‘rstacruz/sparkup‘, {‘rtp‘: ‘vim/‘}
" 安装L9,如果已经安装过这个插件,可利用以下格式避免命名冲突
Plugin ‘ascenator/L9‘, {‘name‘: ‘newL9‘}

" 你的所有插件需要在下面这行之前
call vundle#end()            " 必须
filetype plugin indent on    " 必须 加载vim自带和插件相应的语法和文件类型相关脚本
" 忽视插件改变缩进,可以使用以下替代:
"filetype plugin on
"
" 简要帮助文档
" :PluginList       - 列出所有已配置的插件
" :PluginInstall    - 安装插件,追加 `!` 用以更新或使用 :PluginUpdate
" :PluginSearch foo - 搜索 foo ; 追加 `!` 清除本地缓存
" :PluginClean      - 清除未使用插件,需要确认; 追加 `!` 自动批准移除未使用插件
"
" 查阅 :h vundle 获取更多细节和wiki以及FAQ
" 将你自己对非插件片段放在这行之后

3、安装插件:
运行 vim 再运行 :PluginInstall
通过命令行直接安装 vim +PluginInstall +qall
查阅 :h vundle Vimdoc 以获取更多细节.

三、安装YouCompleteMe

1、在~/.vim/vimrc中添加

Plugin ‘Valloric/YouCompleteMe‘

2、退出后,运行vim,并在命令行模式中运行:

:PluginInstall

四、YCM不同于以往其他vim插件,YCM是一款编译型的插件。在下载完后,需要手动编译后才能使用。

cd ~/.vim/bundle/YouCompleteMe

./install --clang-completer

如果不需要c-family的补全,可以去掉--clang-completer。如果需要c#的补全,请加上--omnisharp-completer。
正常来说,YCM会去下载clang的包,如果已经有,也可以用系统--system-libclang。
就这样,安装结束。打开vim,如果没有提示YCM未编译,则说明安装已经成功了。

五、配置

不同于很多vim插件,YCM首先需要编译,另外还需要有配置。在vim启动后,YCM会找寻当前路径以及上层路径的.ycm_extra_conf.py.在~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py中提供了默认的模板。也可以参考我的(就在模板上改改而已)。不过这个解决了标准库提示找不到的问题。
一般来说,我会在~目录下放一个默认的模板,而后再根据不同的项目在当前目录下再拷贝个.ycm_extra_conf.py。

# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here‘s the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>

import os
import ycm_core

# 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‘,
‘-stdlib=libc++‘,
# 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++‘,
‘-I‘,
‘.‘,
‘-isystem‘,
‘/usr/include‘,
‘-isystem‘,
‘/usr/local/include‘,
‘-isystem‘,
‘/Library/Developer/CommandLineTools/usr/include‘,
‘-isystem‘,
‘/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1‘,
]

# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of ‘flags‘. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Most projects will NOT need to set this to anything; you can just change the
# ‘flags‘ list of compilation flags. Notice that YCM itself uses that approach.
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 ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  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:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = GetCompilationInfoForFile( filename )
    if not compilation_info:
      return None

    final_flags = MakeRelativePathsInFlagsAbsolute(
      compilation_info.compiler_flags_,
      compilation_info.compiler_working_dir_ )

    # NOTE: This is just for YouCompleteMe; it‘s highly likely that your project
    # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
    # ycm_extra_conf IF YOU‘RE NOT 100% SURE YOU NEED IT.
    #try:
    #  final_flags.remove( ‘-stdlib=libc++‘ )
    #except ValueError:
    #  pass
  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return {
    ‘flags‘: final_flags,
    ‘do_cache‘: True
  }

六、

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中配置。具体请参考

let g:ycm_error_symbol = ‘>>‘
let g:ycm_warning_symbol = ‘>*‘

同时,YCM可以打开location-list来显示警告和错误的信息:YcmDiags。个人关于ycm的配置如下:

" for 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。

时间: 2024-10-05 14:40:25

ubuntu下安装自动补全YouCompleteMe的相关文章

Ubuntu Myeclipse alt /自动补全快捷键失效

解决办法: 1.启动MyEclipse 选择菜单栏 Windows ->Preperences ->General ->keys 2.把word completion 的快捷键设置alt+/删掉!//如果这里不删除的话,你就算设置了也没用的. 3.把Content Assist 的快捷键 改成alt+/ Ubuntu Myeclipse alt /自动补全快捷键失效

Mac下终端自动补全功能

记录一下终端的一些使用命令,跟自动补全的配置,主要怕以后忘记了. 1.终端自动补全的配置 打开终端,输入 : nano .inputrc 在文件里面写上: set completion-ignore-case onset show-all-if-ambiguous onTAB: menu-complete ctrl + o ,回车,重启终端,自动补全按tap键就ok. 2.常用命令 pwd     当前工作目录 cd(不加参数) 进root cd(folder)      进入文件夹 cd ..

linux下git自动补全命令

自动补全命令需要用到源码包: branch master tags 选择相应的版本克隆岛本地 #git clone https://github.com/git/git 也可以直接下载zip的源码包 cp /contrib/completion/git-completion.bash   ~/.git-completion.bash source ~/.git-completion.bash 或者将source写入~/.bashrc 自动加载

为Python添加交互模式下TAB自动补全以及命令历史功能

接上篇文章新建Python环境变量配置文件:在宿主用户目录下vim .pystartup# Add auto-completion and a stored history file of commands to your Python# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is# bound to the Esc key by default (you can change it – see

linux-python在vim下的自动补全功能

安装配置: wget https://github.com/rkulla/pydiction/archive/master.zipunzip -q mastermv pydiction-master pydictionmkdir -p ~/.vim/tools/pydictioncp -r pydiction/after ~/.vimcp pydiction/complete-dict ~/.vim/tools/pydiction 确保文件结构如下: # tree ~/.vim /root/.v

python在命令行和vim编辑器模式下自动补全

vim编辑器模式下自动补全 下载pydiction插件 unzip -q pydiction.zip mkdir -p ~/.vim/after/ftplugin/ mkdir -p ~/.vim/tools/pydiction/ cd pydiction-1.2 cp python_pydiction.vim  ~/.vim/after/ftplugin/ cp complete-dict  ~/.vim/tools/pydiction/ vim ~/.vimrc 添加: filetype p

Centos 6.6命令行实现自动补全

公司同事用的ubuntu系统,自动补全功能不错,但是本人的Centos 6.6系统使用sudo的时候就不能自动补全了,git命令也不能自动补全. 网上搜索一番解决方法如下. 一  git不能补全问题 下载git源码 $ git clone https://www.kernel.org/pub/scm/git/ 找到git-completion.bash文件拷贝到 -/目录下 $ sudo cp contrib/completion/git-completion.bash -/.git-compl

vim 自动补全功能:

记录下自己的YouCompleteMe安装步骤: vim没有代码自动补全功能是非常不爽的. 1.先明确一点: MAC 自带的VIM 是7.3版本,而YouCompleteMe安装需要7.3版以上的vim. 使用MAC自带的vim安装YouCompleteMe会出现问题如下: 2.安装YouCompleteMe之前要先安装Budle插件(vundle插件): vundle插件安装比较简单: 在$HOME/.vimrc 文件顶部贴入下列代码: set nocompatible " required

Python2.7.12开发环境构建(自动补全)

一.安装readline-devel包 Python的编译安装依赖于这个包 yum -y install readline-devel 二.安装Python2.7.12 Python官方网站(到此处下载):https://www.python.org/ # tar xf Python-2.7.12.tgz # cd Python-2.7.12 # ./configure --prefix=/usr/local/python27 # make && make install # ln -sv