安装Vim插件 YouCompleteMe

时间:2022-07-10 17:25:46

Linux系统:Ubuntu 15.04,为Vim安装YouCompleteMe插件,有需要的朋友可以参考下。

0x00. 简介

YouCompleteMe号称Vim的自动补全神器,YouCompleteMe: a code-completion engine for Vim,该项目在github的地址:YouCompleteMe

点击链接进去是下载界面:

https://github.com/Valloric/YouCompleteMe#ubuntu-linux-x64

下面是安装界面:

Ubuntu Linux x64

Please refer to the full Installation Guide below; the following commands are provided on a best-effort basis and may not work for you.

Make sure you have Vim 7.3.598 with python2 or python3 support. Ubuntu 14.04 and later have a Vim that's recent enough. You can see the version of Vim installed by running vim
--version
. If the version is too old, you may need to compile Vim from source (don't worry, it's easy).

Install YouCompleteMe with Vundle.

下面是安装Vundle  网页:https://github.com/VundleVim/Vundle.vim#about

  1. Introduction:

    Installation requires Git and triggers git
    clone
     for each configured repository to ~/.vim/bundle/ by default. Curl is required for search.

    If you are using Windows, go directly to Windows setup. If you run into any issues, please consult the FAQ. See Tips for some advanced configurations.

    Using non-POSIX shells, such as the popular Fish shell, requires additional setup. Please check the FAQ.

  2. Set up Vundle:

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

  3. Configure Plugins:

    Put this at the top of your .vimrc to use Vundle. Remove plugins you don't need, they are for illustration purposes.

    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'

    " 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/'}
    " Install L9 and avoid a Naming conflict if you've already installed a
    " different version somewhere else.
    Plugin 'ascenator/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. Install Plugins:

    Launch vim and run :PluginInstall

    To install from command line: vim
    +PluginInstall +qall

Remember: YCM is a plugin with a compiled component. If you update YCM using Vundle and the ycm_core library APIs have changed (happens rarely), YCM will notify you to recompile it. You should then rerun the install process.

Install development tools and CMake: sudo apt-get install build-essential cmake

Make sure you have Python headers installed: sudo apt-get install python-dev python3-dev.

Compiling YCM with semantic support for C-family languages:

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

Compiling YCM without semantic support for C-family languages:

cd ~/.vim/bundle/YouCompleteMe
./install.py

The following additional language support options are available:

  • C# support: add --omnisharp-completer when calling ./install.py.
  • Go support: ensure go is installed and add --gocode-completer when calling ./install.py.
  • TypeScript support: install nodejs and npm then install the TypeScript SDK with npm install -g typescript.
  • JavaScript support: install nodejs and npm and add --tern-completer when calling ./install.py.
  • Rust support: install rustc and cargo and add --racer-completer when calling ./install.py.

To simply compile with everything enabled, there's a --all flag. So, to install with all language features, ensure npmgo,monorust, and typescript API are installed and in your PATH, then simply run:

cd ~/.vim/bundle/YouCompleteMe
./install.py --all

That's it. You're done. Refer to the User Guide section on how to use YCM. Don't forget that if you want the C-family semantic completion engine to work, you will need to provide the compilation flags for your project to YCM. It's all in the User Guide.

YCM comes with sane defaults for its options, but you still may want to take a look at what's available for configuration. There are a few interesting options that are conservatively turned off by default that you may want to turn on.



0x01. Requirements

Vim is at least 7.3.584 and it has support for python2 scripting.

0x02. 用Vundle来安装YCM(YouCompleteMe)

安装其实非常简单,在.vimrc文件中加入语句 Bundle 'Valloric/YouCompleteMe' 即可,保存,退出,然后再次打开Vim,normal模式下输入 :BundleInstall 即开始下载。如下图:

安装Vim插件 YouCompleteMe

也可以切换至 .vim/bundle 下手动下载,输入如下命令:

git clone https://github.com/Valloric/YouCompleteMe.git

手动下载完后检查仓库的完整性,切换到 YouCompleteMe 目录下,输入如下命令:

git submodule update --init --recursive

0x03.  下载最新的libclang

Clang是一个开源的编译器,它可以编译C/C++/Objective-C/Objective-C++. Clang提供的libclang库是用来给YCM为这些语言提供语义补全的引擎。和YCM配合的libclang版本必须 >=3.6 ,但理论上也是可以和 3.2+ 版本的libclang配合使用的。下载最新的libclang如下:

sudo apt-get install libclang-dev

0x04. 编译YCM需要的ycm_support_libs库

首先要确保你已经安装类cmake。如果没有,请按如下安装:

sudo apt-get install cmake

其次需要确保你有一些 Python 头文件。如果没有,请按如下安装:

sudo apt-get install python-dev

接下来,创建新的文件夹 ycm_build 进行编译,如下:

1 cd ~
2 mkdir ycm_build
3 cd ycm_build

不需要语义补全的童鞋们,直接执行下面的语句:

cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

然并卵啊,俺们是需要语义补全的呀~,那么请先去 llvm.org 下载最新的 clang+llvm 包,然后把它们解压到 ~/ycm_temp/llvm_root_dir 文件夹下。接着,执行如下语句:

cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

再接着要开始编译 ycm_support_libs 库,执行如下语句:

make ycm_support_libs

安装Vim插件 YouCompleteMe

这样就编译完了。

0x05. 配置YCM

本渣开始以为编译完了就完事了,谁知道再次打开Vim,它来了这么一句:No .ycm_extra_conf.py file detected, so no compile flags are available. Thus no semantic support for C/C++/ObjC/ObjC++. 哦呵呵呵~

进 ~/.vim/bundle/YouCompleteMe 文件夹下一看。。。没有 cpp !没有 cpp !没有 cpp !

咋办?新建呗。。。然后把 YCM‘S own .ycm_extra_conf.py 文件拷贝到相应的文件夹下。

接着再次打开 .vimrc 配置YCM,如下:

"YouCompleteMe
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py'

安装Vim插件 YouCompleteMe

之后再次打开Vim即可。

为了补全,我们还需要在 .ycm_extra_conf.py 文件中进行配置,如下:

1 '-isystem',
2 '/usr/include',
3 '-isystem',
4 '/usr/include/c++/4.9.2',
5 '-isystem',
6 '/usr/include',
7 '/usr/include/x86_64-linux-gnu/c++',

安装Vim插件 YouCompleteMe

0x06. Summary

城里人真会玩:)

安装Vim插件 YouCompleteMe

自己再配置配置可以起飞了~