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
在用户家目录下执行命令: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
",就会从网络上下载插件并安装。
(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插件的安装和配置
"---------------------------------------------------
" 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 gl :YcmCompleter GoToDeclaration
nnoremap gf :YcmCompleter GoToDefinition
nnoremap gg :YcmCompleter GoToDefinitionElseDeclaration
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
let g:ycm_key_invoke_completion = '' " 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 ( ()i
:inoremap ) =ClosePair(')')
:inoremap { {}O
:inoremap } =ClosePair('}')
:inoremap [ []i
:inoremap ] =ClosePair(']')
":inoremap " ""i
":inoremap ' ''i
function! ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\"
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 ")
call append(line(".")+7, "using namespace std;")
call append(line(".")+8, "")
endif
if &filetype == 'c'
call append(line(".")+6, "#include ")
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