ubuntu 16.04 安装Vundle和YouCompleteMe实现C/C++的代码提示和补全功能

Vundle是一个插件管理工具,它可以在 .vimrc 中跟踪、管理和自动更新插件。接下来的操作会用到 git 工具,如果没有安装过 git 使用命令sudo apt-get install git安装 git 工具。在用户家目录下使用命令touch .vimrc建立 .vimrc 文件。该插件最好配合vim8可以执行一下命令将vim升级到vim8(如果你用的vim低于版本8,vim --version可查看vim版本)使用命令

sudo add-apt-repository ppa:jonathonf/vim 
sudo apt-get update 
sudo apt-get install vim 

一.安装插件管理工具Vundle

在用户家目录下执行命令:git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
如下图
在这里插入图片描述
.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()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

保存退出,重启 vim 然后执行命令" :PluginInstall",就会从网络上下载插件并安装。

二.安装自动补全插件YouCompleteMe

(1)安装软件包,执行命令

sudo apt-get install build-essential cmake python-dev python3-dev

(2)安装YouCompleteMe(安装时间有点长可以去喝好几杯茶了)
.vimrc文件中添加(添加到之前 .vimrc文件中Plugin 'VundleVim/Vundle.vim'的后面)

Plugin 'Valloric/YouCompleteMe'

方法一:重启 vim 然后执行命令" :PluginInstall",这个过程从网络上下载代码,需要等待很长的时间,并且没有进度条,不知道下载了多少,直到出现 Done 表示下载完成。
方法二:使用下面的命令(能够看到下载进度条)

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

下载完成如图
在这里插入图片描述
切换到~/.vim/bundle/YouCompleteMe目录下执行命令
./install.py --clang-completer --clang-completer表示对c/c++的支持
有如下提示
在这里插入图片描述
输入命令git submodule update --init --recursive,然后等待大概四五十分钟,相比于方法一的安装方法虽然都会等带很长时间,但是这个会有进度条(看到进度条感觉心里有底,能看到系统在运行,没有死机)。如图
在这里插入图片描述
再次执行./install.py --clang-completer如图
在这里插入图片描述
然后将~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples目录下的.ycm_extra_conf.py文件拷贝到~/.vim目录下,
在这里插入图片描述
使用命令cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim
然后在 .vimrc文件中添加如下配置(添加到filetype plugin indent on " required的后面)

let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'

这样就完成了YouCompleteMe插件的安装和配置

三.测试

如图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四.我的.vimrc文件


"---------------------------------------------------
"                 Vundle插件管理
"---------------------------------------------------
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()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required


"--------------------------------------------------
"                     YCM
"--------------------------------------------------
let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'

  " YCM 查找定义
let mapleader=','
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
let g:ycm_collect_identifiers_from_tags_files = 1

set completeopt=menu,menuone
let g:ycm_add_preview_to_completeopt = 0  " 关闭函数原型提示

let g:ycm_show_diagnostics_ui = 0 " 关闭诊断信息
let g:ycm_server_log_level = 'info'
let g:ycm_min_num_identifier_candidate_chars = 2  " 两个字符触发 补全
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 收集
let g:ycm_complete_in_strings=1

noremap <c-z> <NOP>
let g:ycm_key_invoke_completion = '<c-z>'   " YCM 里触发语义补全有一个快捷键
let g:ycm_max_num_candidates = 15  " 候选数量

let g:ycm_semantic_triggers =  {
                        \ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
                        \ 'cs,lua,javascript': ['re!\w{2}'],
                        \ }

"----------------------------------------
"              ctags: ctags -R
"----------------------------------------
if filereadable("tags")
        set tags=tags
endif


"------------------------------------------------------
"                       实用设置
"------------------------------------------------------
set nu

" 为C程序提供自动缩进
set smartindent

"自动补全
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {<CR>}<ESC>O
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
":inoremap " ""<ESC>i
":inoremap ' ''<ESC>i
function! ClosePair(char)
        if getline('.')[col('.') - 1] == a:char
                return "\<Right>"
        else
                return a:char
        endif
endfunction

"--------------------------------------------------------
          "新建.c,.h,.sh,.java文件,自动插入文件头
"--------------------------------------------------------
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()"
""定义函数SetTitle,自动插入文件头
func SetTitle()
        "如果文件类型为.sh文件
        if &filetype == 'sh'
                call setline(1,"\#########################################################################")
                call append(line("."), "\# File Name: ".expand("%"))
                call append(line(".")+1, "\# Author: madara")
                call append(line(".")+2, "\# mail: xxxxxxxx.com")
                call append(line(".")+3, "\# Created Time: ".strftime("%c"))
                call append(line(".")+4, "\#########################################################################")
                call append(line(".")+5, "\#!/bin/bash")
                call append(line(".")+6, "")
        else
                call setline(1, "/*************************************************************************")
                call append(line("."), "        > File Name: ".expand("%"))
                                 call append(line(".")+1, "      > Author: madara")
                 call append(line(".")+2, "      > Mail: xxxxxxxx.com ")
                 call append(line(".")+3, "      > Created Time: ".strftime("%c"))
                 call append(line(".")+4, " ************************************************************************/")
                 call append(line(".")+5, "")
         endif
         if &filetype == 'cpp'
                 call append(line(".")+6, "#include <iostream>")
                 call append(line(".")+7, "using namespace std;")
                 call append(line(".")+8, "")
         endif
         if &filetype == 'c'
                 call append(line(".")+6, "#include <stdio.h>")
                 call append(line(".")+7, "")
         endif
         "       if &filetype == 'java'
         "               call append(line(".")+6,"public class ".expand("%"))
         "               call append(line(".")+7,"")
         "       endif
         "新建文件后,自动定位到文件末尾
 "       autocmd BufNewFile * normal G
 endfunc
 "新建文件后,自动定位到文件末尾
 autocmd BufNewFile * normal G
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值