Vimrc 基础 VIM配置之常用基本配置

时间:2022-04-12 01:54:32

"===================================================================================================================
"一些全局设置
"===================================================================================================================
"VimScript globle settings
"不兼容vi模式
set nocompatible
" 保存全局变量
set viminfo+=!
" 设置当文件被改动时自动载入
set autoread
"共享剪贴板 
set clipboard+=unnamed
"从不备份 
set nobackup
"自动保存
set autowrite
" 设置魔术
set magic
" 去掉输入错误的提示声音
set noeb
" 在处理未保存或只读文件的时候,弹出确认
set confirm
" 历史记录数
set history=1000
"禁止生成临时文件
set noswapfile
" 侦测文件类型
filetype on
" 载入文件类型插件
filetype plugin on
" 为特定文件类型载入相关缩进文件
filetype indent on
" 带有如下符号的单词不要被换行分割
set iskeyword+=_,@,%,#
" 增强模式中的命令行自动完成操作
set wildmenu
" 使回格键(backspace)正常处理indent, eol, start等
set backspace=2
" 允许backspace和光标键跨越行边界
set whichwrap+=<,>,h,l,b,s,[,]
" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位)
set mouse=a
set selection=exclusive
set selectmode=mouse,key
" 通过使用: commands命令,告诉我们文件的哪一行被改变过
set report=0
" 光标移动到buffer的顶部和底部时保持3行距离
set scrolloff=3
"自动格式化
set formatoptions=tcrqn  
"自动切换当前目录为当前文件所在的目录
"set autochdir            


"===================================================================================================================
" 显示相关 
"===================================================================================================================
"VimScript display settings {{{
" 设置配色方案,vim自带下列配色方案,在colors目录下,也可以自定义
"colorscheme delek
colorscheme desert
"colorscheme elflord
"colorscheme evening
"colorscheme koehler
"colorscheme morning
"colorscheme murphy
"colorscheme pablo
"colorscheme peachpuff
"colorscheme ron
"colorscheme shine
"colorscheme slate
"colorscheme torte
"colorscheme zellner

"set go=             " 不要图形按钮 
"set guioptions-=T           " 隐藏工具栏
"set guioptions-=m           " 隐藏菜单栏
set guifont=Courier_New:h10:cANSI   " 设置字体 
syntax on           " 语法高亮 
set showcmd         " 输入的命令显示出来,看的清楚些 
"set novisualbell    " 不要闪烁(不明白) 
 
set number                "是否显示行号
set cursorline              "突出显示光标所在行,默认下划线
"设置为下划线和粗体显示
highlight Cursorline gui=bold,underline term=bold,underline cterm=bold,underline
"设置显示颜色
highlight Cursorline guibg=NONE guifg=NONE ctermfg=NONE ctermbg=NONE
"set cursorcolumn           " 突出显示当前列
"highlight CursorColumn guibg=lightblue ctermbg=lightgray

" 在被分割的窗口间显示空白,便于阅读
set fillchars=stl:\ ,stlnc:-,vert:\|,fold:-,diff:-
set nowrap                "设置不换行,wrap是换行

"}}}


"===================================================================================================================
"字符编码格式相关设置
"encoding 默认显示编码
"fileencodings 编码列表,读取文件时依次检测,
"fileencoding
"新建文件和保存文件时的编码,如果与encoding的值不一样,保存时则会把文件内容由encoding的编码转换为fileencoding的值再保存
"读取文件时会自动同步为fileencodings的值
"===================================================================================================================
"Vimscript encode settings {{{
"set encoding=utf-8
if has("multi_byte")
    " Set fileencoding priority
    if getfsize(expand("%")) > 0
        set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin1
    else
        set fileencodings=cp936,big5,euc-jp,euc-kr,latin1
    endif
    " CJK environment detection and corresponding setting
    if v:lang =~ "^zh_CN"
        " Use cp936 to support GBK, euc-cn == gb2312
        set encoding=cp936
        set termencoding=cp936
        "set fileencoding=cp936
    elseif v:lang =~ "^zh_TW"
        " cp950, big5 or euc-tw
        " Are they equal to each other?
        set encoding=big5
        set termencoding=big5
        "set fileencoding=big5
    elseif v:lang =~ "^ko"
        " Copied from someone's dotfile, untested
        set encoding=euc-kr
        set termencoding=euc-kr
        "set fileencoding=euc-kr
    elseif v:lang =~ "^ja_JP"
        " Copied from someone's dotfile, unteste
        set encoding=euc-jp
        set termencoding=euc-jp
        "set fileencoding=euc-jp
    endif
    " Detect UTF-8 locale, and replace CJK setting if needed
    if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
        set encoding=utf-8
        set termencoding=utf-8
        "set fileencoding=utf-8
    endif
else
    echoerr "Sorry, this version of (g)vim was not compiled with multi_byte"
endif
"}}}

"===================================================================================================================
"语言设置
"===================================================================================================================
"VimScript lang settings {{{
set langmenu=zh_CN.UTF-8
set helplang=cn
"set helplang=en
if isdirectory("$HOME/.VIM/doc")
    helptags $HOME/.vim/doc/
endif
"}}}

"===================================================================================================================
"my noremaps learned from Learn Vimscript the Hard Way
"===================================================================================================================
"VimScript noremap settings {{{
"自定义noremap前缀
let mapleader = ','

"disable the arrow key
"inoremap <left> <nop>
nnoremap <left> <nop>
"inoremap <right> <nop>
nnoremap <right> <nop>
"inoremap <up> <nop>
nnoremap <up> <nop>
"inoremap <down> <nop>
nnoremap <down> <nop>
"insert 模式下保存
inoremap <leader>w <esc>:w<cr>a

"打开vim配置文件
nnoremap <leader>ev :split $MYVIMRC<cr>
"使vim配置生效
nnoremap <leader>sv :source $MYVIMRC<cr>

"c-u to uppercase a hole word in insert mode
inoremap <c-u> <esc>viw U i
"c-l to lowercase a hole word in insert mode
inoremap <c-l> <esc>viw u i

"add "" to select word
nnoremap <leader>" viw<esc>a"<esc>hbi"<esc>lel

"use jk to escape insert mode
inoremap jk <esc>


"VimScript windowsplit noremap  {{{
nnoremap wv     <C-w>v     " 垂直分割当前窗口
nnoremap wc     <C-w>c     " 关闭当前窗口
nnoremap ws     <C-w>s     " 水平分割当前窗口
nnoremap wh     <C-w>h
nnoremap wj     <C-w>j
nnoremap wk     <C-w>k
nnoremap wl     <C-w>l
nnoremap wo     <C-w>o     "关闭所有其他窗口
"}}}

"}}}

"===================================================================================================================
"my abbreviations
"===================================================================================================================
"VimScript abbreviation settings {{{
:iabbrev msg@ //modified by Jason

"operator-pending noremappings
onoremap p i(
onoremap q i"
onoremap ip :<c-u>normal! f(vi(<cr>
onoremap iq :<c-u>normal! f(vi(<cr>

"}}}

"===================================================================================================================
"autocmd group
"===================================================================================================================
"VimScript autocmd settings {{{
"设置vim脚本折叠
"Vimscript file settings {{{
 augroup filetype_vim
     autocmd!
     autocmd FileType vim setlocal foldmethod=marker
 augroup END
" }}}

"编译c,cpp文件
"VimScript autocmd make settings {{{
augroup cFileMake
    autocmd!
    autocmd FileType c,cpp noremap <buffer> <leader><space> :w<cr>:make<cr>
    "make 运行
    set makeprg=g++\ -Wall\ \ %
augroup END
"}}}

"自动补全
"VimScript auto complement settings {{{
augroup autoComplet
    autocmd!
    autocmd FileType c,cpp,java,h call MyAutoCompletMap()
    function! MyAutoCompletMap()
        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
    endfunction
    function! ClosePair(char)
     if getline('.')[col('.') - 1] == a:char
      return "\<Right>"
     else
      return a:char
     endif
    endfunction
augroup END
"}}}

"高亮显示设置
"VimScript auto higtlight settings {{{
augroup myLineHighlight
    autocmd!
    autocmd InsertLeave * highlight Cursorline guibg=NONE guifg=NONE ctermbg=NONE ctermfg=NONE    " 非编辑状态不变色

    autocmd InsertEnter * highlight Cursorline guibg=NONE guifg=red  ctermbg=NONE ctermfg=NONE    " 编辑状态变色显示
augroup END
"}}}

"user <leader>c to comment a line
"VimScript auto c comment settings {{{
augroup myCComment
    autocmd!
    autocmd FileType c nnoremap <buffer> <leader>c I//<esc>
augroup END
"}}}

"set javacomplete onmifunc
"VimScript autocmd javacomplete settings {{{
augroup myJavaComplete
    autocmd!
    autocmd FileType java setlocal omnifunc=javacomplete#Complete
    autocmd FileType java setlocal completefunc=javacomplete#CompleteParamsInfo
augroup END
"}}}

"===================================================================================================================
" 状态栏相关的设置
"===================================================================================================================
"Vimscript statusline settings {{{
" 命令行(在状态行下)的高度,默认为1
set cmdheight=2

" 多个窗口时显示状态行(1),总是显示状态行(2)
set laststatus=2

" 在编辑过程中,在右下角显示光标位置的状态行,被statusline的设置覆盖
set ruler

" 设置在状态行显示的信息
set statusline=[%n]                                               "缓冲区号
set statusline+=[%<%F]                                            "文件完整路径
set statusline+=%=                                                "左右对齐项目的分割点
set statusline+=[%{&fileformat}]                                  "文件格式
set statusline+=%y                                                "文件类型
set statusline+=[%{&encoding}/%{&fileencoding}]                   "编码格式
set statusline+=%r                                                "只读标志位
set statusline+=%1*%m%*                                           "修改标志位,并指定为组1
set statusline+=%h                                                "帮助缓冲区标志位
set statusline+=%w                                                "预览标识位
set statusline+=[0x%04.4B]                                        "光标所在字节的ascii码值,十六进制表示
set statusline+=%*%=[L:%l/%L,C:%c]                                "行数
"set statusline+=[%o]                                              "光标所在字节在文件中的偏移,十进制表示
set statusline+=[%p%%]                                            "百分比
"set statusline+=[%{strftime(\"%c\")}]                            "时间
highlight User1 term=inverse,bold cterm=inverse,bold ctermfg=red        "高亮指定组

"}}}

"===================================================================================================================
" 查找/替换相关的设置
"===================================================================================================================
"VimScript search settings {{{
set hlsearch        " 高亮显示搜索结果
set incsearch       " 查询时非常方便,如要查找book单词,当输入到/b时,会自动找到
                    " 第一个b开头的单词,当输入到/bo时,会自动找到第一个bo开头的
                    " 单词,依次类推,进行查找时,使用此设置会快速找到答案,当你
                    " 找要匹配的单词时,别忘记回车
set gdefault        " 替换时所有的行内匹配都被替换,而不是只有第一个
set ignorecase smartcase  "搜索时忽略大小写,但是有一个大写存在时仍保持大小写敏感
"}}}

"===================================================================================================================
" 代码折叠
"===================================================================================================================
"Vimscript fold settings {{{
"set foldmarker={,}      "默认是{{{,}}}
set foldmethod=syntax     "设置语法折叠
"set foldopen-=search   " don't open folds when you search into them
"set foldopen-=undo     " don't open folds when you undo stuff
"set foldcolumn=4
set foldlevel=100       " Don't autofold anything (but I can still fold manually)
set foldenable            "启用折叠

"}}}

"===================================================================================================================
" 编程相关的设置
"===================================================================================================================
"VimScript coding settings {{{
" 关掉智能补全时的预览窗口
set completeopt=longest,menu   
" 智能对齐方式
set smartindent    
" 自动缩进
set autoindent
"C语言缩进风格
set cindent
"用空格代替制表符
set expandtab 
" Tab键的宽度
set tabstop=4
" 统一缩进为4
set softtabstop=4
set shiftwidth=4
" 高亮显示匹配的括号
set showmatch
" 匹配括号高亮的时间(单位是十分之一秒)
set matchtime=1

"}}}