本文将自己的~/.vimrc文件,拷贝记录到自己的博客,方便以后在不同的电脑上快速配置,也方便初学者可以借鉴。欢迎大家提出指正!
注:
~家目录下,.vimrc为配置文件,.vim/为插件管理目录,若没有请自行创建。本文使用Vundle管理插件(推荐),也可自行从github上下载到.vim/bundle/目录中,按照Readme文件要求拷贝到.vim/文件夹中。
#windows下vim插件配置,可参考博客:
全世界最好的编辑器VIM之Windows配置(gvim)
"基本配置
set autoread
set number
syntax on
set cursorline
set colorcolumn=85
set showtabline=2
set linebreak
set list
set listchars=tab:| ,trail:-
set scrolloff=3
set encoding=utf-8
set clipboard=unnamed
set modelines=0
set nobackup
set noswapfile
set hidden
"底部状态栏
set laststatus=2
set statusline= %F%m%r%h%w [TYPE=%Y] [%{&fileformat}] [%{&encoding}]%=[POS=%l/%L, %c] [%p%%] %{strftime("%Y-%m-%d %H:%M")}
"undo文件配置
set undofile
set undodir=/tmp/vim_undo
set undolevels=1000
set undoreload=10000
"键盘重映射
let mapleader=","
nnoremap T :%s/s+$//
nnoremap L :%s/s*n+/r/
nnoremap h ^
nnoremap l $
nnoremap dh d^
nnoremap dl d$
nnoremap ch c^
nnoremap cl c$
nnoremap w vl
nnoremap h
nnoremap l
nnoremap ggVG
nnoremap a gg=G
nnoremap %
vnoremap %
inoremap
nnoremap
vnoremap
vnoremap / :normal I//
vnoremap / :s/////
"搜索:/,替换:%s/,查找并列出:%g/,配置magic(^$.*不用转义)
nnoremap :noh
set magic
set ignorecase
set incsearch
set hlsearch
set smartcase
"自动缩进
set autoindent
set smartindent
set shiftwidth=4
set softtabstop=4
set tabstop=4
set expandtab
"括号自动补全
inoremap ' ''i
inoremap " ""i
inoremap ` ``i
inoremap ( ()i
inoremap ) =ClosePair(')')
inoremap [ []i
inoremap ] =ClosePair(']')
inoremap [ {}i
inoremap { {}O
inoremap } =ClosePair('}')
func! ClosePair(char)
if getline('.')[col('.')-1]==a:char
return ""
else
return a:char
endif
endfunc
"根据缩进折叠,5级缩进,不显示折叠标记
"set foldmethod=indent
"set foldlevel=4
"set foldcolumn=0
"插件管理
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'vim-scripts/changesPlugin'
Plugin 'nathanaelkane/vim-indent-guides'
Plugin 'tomasr/molokai'
Plugin 'scrooloose/nerdtree'
Plugin 'majutsushi/tagbar'
Plugin 'kien/ctrlp.vim'
Plugin 'luochen1990/rainbow'
Plugin 'vim-scripts/javacomplete'
Plugin 'rkulla/pydiction'
call vundle#end()
filetype plugin indent on
"indent-guides
let g:indent_guides_enable_on_vim_startup = 1
let g:indent_guides_guide_size = 1
let g:indent_guides_tab_guides = 0
let g:indent_guides_exclude_filetypes = ['help', 'nerdtree']
let g:indent_guides_default_mapping = 0
let g:indent_guides_auto_colors = 0
autocmd VimEnter,Colorscheme * :hi IndentGuidesOdd guibg=#3E3D32 ctermbg=236
autocmd VimEnter,Colorscheme * :hi IndentGuidesEven guibg=#3E3D32 ctermbg=236
"molokai界面
colorscheme molokai
"let g:molokai_original = 1
let g:rehash256 = 1
"set background=light
"set guifont=DejaVu_Sans_Mono:h12
"set guioptions-=T
"set guioptions-=m
"set lines=35
"set columns=90
"winpos 500 200
"autocmd GUIEnter * simalt ~x
"通过:hi查看颜色配置,添加以下语句修改颜色配置
"highlight cursorline term=underline ctermbg=234 guibg=#293739
"nerdtree目录树状图
nnoremap :NERDTreeToggle
let NERDTreeWinSize=25
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
let NERDTreeIgnore=['.pyc','~$','.swp']
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
"rainbow彩虹括号
let g:rainbow_active=1
let g:rainbow_conf={
'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick'],
'ctermfgs': ['lightblue', 'lightyellow', 'lightcyan', 'lightmagenta'],
'operators': '_,|=|+|-_',
'parentheses': ['start=/(/ end=/)/ fold', 'start=/[/ end=/]/ fold', 'start=/{/ end=/}/ fold'],
'separately': {
'*': {},
'tex': {'parentheses': ['start=/(/ end=/)/', 'start=/[/ end=/]/'],},
'lisp': {'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick', 'darkorchid3'],},
'vim': {'parentheses': ['start=/(/ end=/)/', 'start=/[/ end=/]/', 'start=/{/ end=/}/ fold', 'start=/(/ end=/)/ containedin=vimFuncBody', 'start=/[/ end=/]/ containedin=vimFuncBody', 'start=/{/ end=/}/ fold containedin=vimFuncBody'],},
'html': {'parentheses': ['start=/v<((area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)[ >])@!z([-_:a-zA-Z0-9]+)(s+[-_:a-zA-Z0-9]+(=("[^"]*"|'."'".'[^'."'".']*'."'".'|[^ '."'".'"><=`]*))?)*>/ end=## fold'],},
'css': 0,
}
}
"自动补全
set completeopt=menu,longest
set wildmenu
set wildmode=list:longest
"tagbar标签窗口
let g:tagbar_ctags_bin='/usr/bin/ctags'
nnoremap :TagbarToggle
let g:tagbar_width=30
let g:tagbar_autopreview=1
let g:tagbar_autofocus=1
let g:tagbar_sort=0
"ctags标签生成器
set tags+=./tags
nnoremap g]
nnoremap :call CreateTags()
func! CreateTags()
exec "w"
if &filetype == 'c' || &filetype == 'cpp' || &filetype == 'h' || &filetype == 'hpp'
exec "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+fq --language-force=C++"
elseif &filetype == 'python'
exec "!ctags -R --python-kinds --fields=+iaS --extra=+fq"
elseif &filetype == 'java'
exec "!ctags -R --java-kinds=+p --fields=+iaS --extra=+fq"
endif
endfunc
"c/c++自动补全
autocmd Filetype c,cpp,h,hpp set omnifunc=ccomplete#Complete
autocmd Filetype c,cpp,h,hpp inoremap . .
autocmd Filetype c,cpp,h,hpp inoremap -> ->
autocmd Filetype c,cpp,h,hpp inoremap :: ::
autocmd Filetype c,cpp,h,hpp set tags+=/usr/include/c++/4.8.2/stdcpp.tags
"python2.0自动补全,if use python=3.0, then set omnifunc=python3complete#Complete
autocmd Filetype python set omnifunc=pythoncomplete#Complete
autocmd Filetype python inoremap . .
autocmd Filetype python set tags+=/usr/local/python/anaconda3/envs/py2/lib/python2.7/python2.tags
let g:pydiction_location='~/.vim/bundle/pydiction/complete-dict'
let g:pydiction_menu_height=20
"java自动补全
autocmd Filetype java set omnifunc=javacomplete#Complete
autocmd Filetype java set completefunc=javacomplete#CompleteParamsInfo
autocmd Filetype java inoremap . .
autocmd Filetype java set tags+=/usr/local/java/jdk1.8.0_241/src/java.tags
"代码编译运行,语法检查
nnoremap :call Compile()
nnoremap :call Run()
func! Compile()
exec "w"
if &filetype == 'c' || &filetype == 'cpp' || &filetype == 'h' || &filetype == 'hpp'
exec "!g++ % -g -Wall -o ../build/%<"
elseif &filetype == 'java'
exec "!javac -g -d ../bin %"
endif
endfunc
func! Run()
exec "w"
if &filetype == 'c' || &filetype == 'cpp' || &filetype == 'h' || &filetype == 'hpp'
exec "!time ../build/%<"
elseif &filetype == 'python'
exec "!time python %"
elseif &filetype == 'java'
exec "!time java -cp ../bin %<"
elseif &filetype == 'sh'
exec "!time bash %"
endif
endfunc
"python快捷代码
autocmd Filetype python vnoremap / :normal I#
autocmd Filetype python vnoremap / :s/#//
"java快捷代码
autocmd Filetype java inoremap p System.out.println()i
autocmd Filetype java nnoremap 1 :call JavaCode1()
func! JavaCode1()
call setline(line("."), "public static void main(String[] args) {")
call append(line(".")+1, "}")
normal Vj=
endfunc
"自动添加文件头
autocmd BufNewFile *.c,*.cpp,*.h,*.hpp,*.py,*.java,*.sh exec ":call SetTitle()"
func! SetTitle()
if expand("%:e") == 'h' || expand("%:e") == 'hpp'
call setline(line("."), "#pragma once")
call append(line("."), "#ifndef _".toupper(expand("%:r"))."_H")
call append(line(".")+1, "#define _".toupper(expand("%:r"))."_H")
call append(line(".")+2, "")
call append(line(".")+3, "#ifdef __cplusplus")
call append(line(".")+4, "extern "C" {")
call append(line(".")+5, "#endif")
call append(line(".")+6, "")
call append(line(".")+7, "#ifdef __cplusplus")
call append(line(".")+8, "}")
call append(line(".")+9, "#endif")
call append(line(".")+10, "")
call append(line(".")+11, "#endif ")
normal 8G
elseif &filetype == 'c'
call setline(line("."), "#include ")
call append(line("."), "#include ")
call append(line(".")+1, "")
normal G
elseif &filetype == 'cpp'
call setline(line("."), "#include ")
call append(line("."), "using namespace std;")
call append(line(".")+1, "")
normal G
elseif &filetype == 'python'
call setline(line("."), "#! /usr/bin/python2")
call append(line("."), "# coding=utf-8")
call append(line(".")+1, "#")
call append(line(".")+2, "")
normal G
elseif &filetype == 'java'
call setline(line("."), "public class ".expand("%:r")." {")
call append(line("."), "")
call append(line(".")+1, "}")
normal j
elseif &filetype == 'sh'
call setline(line("."), "#! /bin/bash")
call append(line("."), "#")
call append(line(".")+1, "")
normal G
endif
endfunc
打开C++文件效果如下:
打开python文件效果:
(持续更新中……)



