Ubuntu 14.04下Vim安装YouCompleMe插件

最权威的原始步骤可以参考github中关于此插件的README.md,如果时间允许的话,尽量多看几遍可以避免很多不必要的麻烦。

版本检测,一般新系统都满足,保证Vim>= 7.3.584,支持python就可以了。

第一步:下载Vundle和YouCompleteMe插件

输入以下指令,下载Vundle

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

下载成功后,在用户根目录下面,修改.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

然后在vim中先按Esc建,并且输入以下指令安装插件:

:PluginInstall

或在终端中输入:

vim +PluginInstall +qall

第二步:下载其他必要文件

  • 下载最新版本的LLVM

强烈建议下载已经编译好的二级制文件包,如果下载源文件自己编译的话,你永远都不知道会出什么奇葩错误

然后解压到指定文件夹,过程如下:

cd ~
mkdir ycm_temp
cd ycm_temp
xz -d clang+llvm-3.6.0-x86_64-linux-gnu--14.04.tar.xz
tar -xvf clang+llvm-3.6.0-x86_64-linux-gnu-ubuntu-14.04.tar

修改clang+llvm-3.6.0-x86_64-linux-gnu文件夹名字为llvm_root_dir

  • 下载最新版本的cmake

强烈建议下载已经编译好的二级制文件包,如果下载源文件自己编译的话,你永远都不知道会出什么奇葩错误

然后将cmake连接至/usr/bin,比如我的是放在Downloads文件夹下面的,就地解压,并链接

tar zxvf cmake-3.2.2-Linux-x86_64.tar.gz
ln -s /home/li/Downloads/cmake-3.2.2/bin/cmake /usr/bin/cmake

第三步:编译文件

运行如下指令,编译文件

cd ~
mkdir ycm_build
cd ycm_build
cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
make ycm_support_libs 

这样以来,就算是安装基本上完成了。

第四步:配置vim

虽然安装编译完成了,但距离成功还差一步,配置vim(修改.vimrc),这个根据需要配置便可,比如我的如下:

let g:ycm_global_ycm_extra_conf = '/home/li/.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) 

出现的问题:

  • E492: Not an editor command:

可能的原因有很多,比如我遇到的是权限问题,为.vim的追加写入权限

  • 不能正常启动,有很多红色的错误

可能的原因同样很多,比如我遇到的是使用编译器版本不合适,比如gcc5.1.0编译运行完成之后,错的不知其所以然

  • 没有.ycm_extra.conf.py文件

可以参考YCM作者的文件自己修改或者直接使用它亦或者使用我的(见附件)。将它放在项目根目录或者指定目录,比如我放在

$HOME/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py

当然记得修改.vimrc中相应的那行哦!

  • 出现其他未知错误

vim中输入

:YcmDebugInfo

查看相关信息

  • 如果完全按照本教程步骤来,还是出现了大量错误

此时要做的不是搜索其他教程,而是详细阅读README.md

 

附件

# 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
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
  }

猜你喜欢

转载自www.linuxidc.com/Linux/2016-06/131930.htm