vim 插件YouCompleteMe安装归纳

基本环境

  • 操作系统:ubuntu18.04.5
  • vim 版本:8.2
  • python版本: python3.6.9
  • gcc 版本: gcc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
  • vundle : https://github.com/VundleVim/Vundle.vim.git
  • YouCompleteMe: https://github.com/ycm-core/YouCompleteMe.git

由于YouCompleteMe 一直在更新, 所以需要在github 上查看安装的具体要求。我使用的是ycm-core/YouCompleteMe。需要根据GitHub上的要求对环境进行配置。

Vundle

Vundle 的安装我参考了这篇博客:
https://linux.cn/article-9416-1.html?pr
还是非常容易安装的。推荐去阅读原文。

修改或者新建 ~/.vimrc 文件。

call vundle#begin()
Plugin '  '
" 将插件的安装命令放在begin 和end 之间
call vundle#end()

" 将 插件的配置放在end 之后。
let : 

在vim 中命令行模式下输入

  • :PluginInstall
    会自动从GitHub下载位于begin 和end 直接的插件。
    下载的文件就位于目录 ~/.vim/bundle/ 下,不同的插件对应于不同的文件夹名。
    这个时候vundle 就已经安装好了。

YouCompleteMe

下载

在begin 和end 之间添加

Plugin 'ycm-core/YouCompleteMe'

然后打开vim, 在vim 命令行模式 输入

:PluginInstall

会自动将YouComplete 下载到 ~/.vim/bundle/ 目录下。
下载可能会失败, 多试几次,保证网络畅通, 可以访问GitHub。
如果下载失败了,可以参考提示输入 L(小写) 即可看到是将’ycm-core/YouCompleteMe’ 补充为一个完整的下载地址。

安装

下载完成后, 进入 ~/.vim/bundle/YouCompleteMe/ 目录内。
安装github 上的安装提示输入

python3 install.py --all

会输出缺少文件的 错误提示, 提醒使用
git submodule update --init --recursive 去下载其他的关联文件。
我多次使用,最终才将所有文件下载下来。网络状态差会增加难度。

这一步完成后, 再一次

python3 install.py --all

如果继续提示失败, 参考失败信息去补全。
我遇到的失败信息:
我的gcc 不能满足完整的c++17 功能, 参考这个问题解答下面的命令是摘录出来的:

sudo apt-get install g++-8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 700 --slave /usr/bin/g++ g++ /usr/bin/g++-7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8
  • https://stackoverflow.com/questions/65284572/your-c-compiler-does-not-fully-support-c17

CMake 版本过低

 CMake 3.14 or higher is required.  You are running version 3.10.2

(从cmake 官网下载最新的版本, 参考其他的博客, 是先下载, 再编译和安装)基本的命令如下: 参考博客:

  • https://www.cnblogs.com/tyche116/p/13792577.html

在这里插入图片描述

无法新建文件夹(加sudo 即可)
这些问题解决后, 便开始进入编译,下载相关的文件,输出一路绿最好了。

最后输出这些内容代表编译完成了。可以使用了。
在这里插入图片描述

配置

YouCompleteMe 已经编译完成了,但是此时还是无法使用,需要进行一些配置: 参考博客:

  • http://blog.sina.com.cn/s/blog_153192d430102wzn6.html
  • https://www.jianshu.com/p/d908ce81017a?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weibo
 1、复制 .ycm_extra_conf.py  文件到~/.vim/ 目录下
 cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py 
 ~/.vim/
 2、 添加内容到~/.vimrc   

let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
let g:ycm_confirm_extra_conf=0 "每次直接加载该文件,不提示是否要加载

3、修改 .ycm_extra_conf.py

在flags下添加(不能照搬, 需要结合自己的c/cpp头文件位置, 选择性添加)

 '-isystem',
 '/usr/include',
 '-isystem',
 'usr/include/c++/5.4.0',
 '-isystem',
 'usr/include/x86_64-linux-gnu/c++',
并注释掉这一段:

try: 
    final_flags.remove( ‘-stdlib-libc++‘ ) 
except ValueError: 
    pass

到此,vim使用Vundle 安装YouCompleteMe 插件就已经完成。
可以使用vim 打开*.c 或者 *.cpp 文件进行测试了。
这是我的提示:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
可以正常提示。

语法提示

语法提示我使用的是ale , 根据这篇博客做的, 可以参考下。
https://blog.csdn.net/Demorngel/article/details/69052789

我的~/.vimrc

  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
  " 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'}
  
  "ale
  Plugin 'dense-analysis/ale'
  
  Plugin 'ycm-core/YouCompleteMe'
  
  " 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
  
  
  
  "进行版权声明的设置
  "添加或更新头
  map <F4> :call TitleDet()<cr>'s
  function AddTitle()
      call append(0,"/****************************************************************************")
      call append(1,"# Author: P- ******@qq.com")
      call append(2,"# QQ : ********")
      call append(3,"# Last modified: ".strftime("%Y-%m-%d %H:%M"))
      call append(4,"# Filename: ".expand("%:t"))
      call append(5,"# Description: ")
      call append(6,"***************************************************************************/")
      echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
  endf
  "更新最近修改时间和文件名
  function UpdateTitle()
      normal m'
      execute '/# *Last modified:/s@:.*$@\=strftime(":\t%Y-%m-%d %H:%M")@'
      normal ''
      normal mk
      execute '/# *Filename:/s@:.*$@\=":\t\t".expand("%:t")@'
      execute "noh"
      normal 'k
      echohl WarningMsg | echo "Successful in updating the copy right." | echohl None
  endfunction
  "判断前10行代码里面,是否有Last modified这个单词,
  "如果没有的话,代表没有添加过作者信息,需要新添加;
  "如果有的话,那么只需要更新即可
  function TitleDet()
      let n=1
      "默认为添加
      while n < 10
          let line = getline(n)
          if line =~ '^\#\s*\S*Last\smodified:\S*.*$'
              call UpdateTitle()
              return
          endif
          let n = n + 1
      endwhile
      call AddTitle()
  endfunction
  
  :set nu

" 语法纠错
  let g:ale_sign_column_always = 1
  let g:ale_set_highlights = 0
  
  let g:ale_sign_error = 'x'
  let g:ale_sign_warning = '!'
  
  let g:ale_linters = {
  \   'c++': ['clang'],
  \   'c': ['clang'],
  \   'python': ['pylint'],
  \}
  
  " YouCompleteMe
  let g:ycm_server_python_interpreter='/usr/bin/python3'
  let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
  let g:ycm_confirm_extra_conf=0

版权声明:本文为qq_34128332原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。