VIM 经典配置

前端之家收集整理的这篇文章主要介绍了VIM 经典配置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

VIM 经典配置

" =============================================================================
" << 判断操作系统是 Windows 还是 Linux 和判断是终端还是 Gvim >>
" =============================================================================


" -----------------------------------------------------------------------------
" < 判断操作系统是否是 Windows 还是 Linux >
" -----------------------------------------------------------------------------
let g:iswindows = 0
let g:islinux = 0
if(has("win32") || has("win64") || has("win95") || has("win16"))
    let g:iswindows = 1
else
    let g:islinux = 1
endif


" -----------------------------------------------------------------------------
" < 判断是终端还是 Gvim >
" -----------------------------------------------------------------------------
if has("gui_running")
    let g:isGUI = 1
else
    let g:isGUI = 0
endif


" =============================================================================
" << 以下为软件默认配置 >>
" =============================================================================


" -----------------------------------------------------------------------------
" < Windows Gvim 默认配置> 做了一点修改
" -----------------------------------------------------------------------------
if (g:iswindows && g:isGUI)
    source $VIMRUNTIME/vimrc_example.vim
    source $VIMRUNTIME/mswin.vim
    behave mswin
    set diffexpr=MyDiff()
    function MyDiff()
        let opt = '-a --binary '
        if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
        if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
        let arg1 = v:fname_in
        if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
        let arg2 = v:fname_new
        if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
        let arg3 = v:fname_out
        if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
        let eq = ''
        if $VIMRUNTIME =~ ' '
            if &sh =~ '\<cmd'
                let cmd = '""' . $VIMRUNTIME . '\diff"'
                let eq = '"'
            else
                let cmd = substitute($VIMRUNTIME,' ','" ','') . '\diff"'
            endif
        else
            let cmd = $VIMRUNTIME . '\diff'
        endif
        silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
    endfunction
endif


" -----------------------------------------------------------------------------
" < Linux Gvim/Vim 默认配置> 做了一点修改
" -----------------------------------------------------------------------------
if g:islinux
    set nocompatible
    set hlsearch        "高亮搜索
    set incsearch       "在输入要搜索文字时,实时匹配


    " Uncomment the following to have Vim jump to the last position when
    " reopening a file
    if has("autocmd")
        au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
    endif


    if g:isGUI
        " Source a global configuration file if available
        if filereadable("/etc/vim/gvimrc.local")
            source /etc/vim/gvimrc.local
        endif
    else
        " This line should not be removed as it ensures that varIoUs options are
        " properly set to work with the Vim-related packages available in Debian.
        runtime! debian.vim


        " Vim5 and later versions support Syntax highlighting. Uncommenting the next
        " line enables Syntax highlighting by default.
        if has("Syntax")
            Syntax on
        endif


        set mouse=a                    " 在任何模式下启用鼠标
        set t_Co=256                   " 在终端启用256色
        set backspace=2                " 设置退格键可用


        " Source a global configuration file if available
        if filereadable("/etc/vim/vimrc.local")
            source /etc/vim/vimrc.local
        endif
    endif
endif


" -----------------------------------------------------------------------------
" < 编码配置 >
" -----------------------------------------------------------------------------
" 注:使用utf-8格式后,软件与程序源码、文件路径不能有中文,否则报错
set encoding=utf-8                                    "设置gvim内部编码
set fileencoding=utf-8                                "设置当前文件编码
set fileencodings=ucs-bom,utf-8,gbk,cp936,latin-1     "设置支持打开的文件的编码


" 文件格式,默认 ffs=dos,unix
if(g:iswindows)
    set fencs=gbk
    set fileformat=dos                                   "设置新文件的<EOL>格式
    set fileformats=dos,unix,mac                          "给出文件的<EOL>格式类型
else
    set fileformat=unix                                   "设置新文件的<EOL>格式
    set fileformats=unix,dos,mac                          "给出文件的<EOL>格式类型
endif


if (g:iswindows && g:isGUI)
    "解决菜单乱码
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vim


    "解决consle输出乱码
    language messages zh_CN.utf-8
endif


" -----------------------------------------------------------------------------
" < 界面配置 >
" -----------------------------------------------------------------------------
set nobackup
set ai
set ci
set ts=4
set sw=4
set expandtab
filetype plugin indent on
set ruler                                             "显示标尺 
set number                                            "显示行号
set laststatus=2                                      "启用状态栏信息
set cmdheight=2                                       "设置命令行的高度为2,默认为1
set cursorline                                        "突出显示当前行
" set guifont=YaHei_Consolas_Hybrid:h10 "设置字体:字号(字体名称空格用下划线代替)
set nowrap                                            "设置不自动换行
set shortmess=atI                                     "去掉欢迎界面


" 设置 gVim 窗口初始位置及大小
if g:isGUI
    "au GUIEnter * simalt ~x "窗口启动时自动最大化
    winpos 450 300                                     "指定窗口出现的位置,坐标原点在屏幕左上角
    set lines=28 columns=120                          "指定窗口大小,lines为高度,columns为宽度
endif


" 设置代码配色方案
if g:isGUI
    colorscheme evening                               "Gvim配色方案
else
    colorscheme Tomorrow-Night-Eighties               "终端配色方案
endif


" 显示/隐藏菜单栏、工具栏、滚动条,可用 Ctrl + F11 切换
if g:isGUI
    set guioptions-=m
    set guioptions-=T
    set guioptions-=r
    set guioptions-=L
    map <silent> <c-F11> :if &guioptions =~# 'm' <Bar>
        \set guioptions-=m <Bar>
        \set guioptions-=T <Bar>
        \set guioptions-=r <Bar>
        \set guioptions-=L <Bar>
    \else <Bar>
        \set guioptions+=m <Bar>
        \set guioptions+=T <Bar>
        \set guioptions+=r <Bar>
        \set guioptions+=L <Bar>
    \endif<CR>
endif


" -----------------------------------------------------------------------------
" < 编写文件时的配置 >
" -----------------------------------------------------------------------------
filetype on                                           "启用文件类型侦测
filetype plugin on                                    "针对不同的文件类型加载对应的插件
filetype plugin indent on                             "启用缩进
set smartindent                                       "启用智能对齐方式
set expandtab                                         "将Tab键转换为空格
set tabstop=4                                         "设置Tab键的宽度
set shiftwidth=4                                      "换行时自动缩进4个空格
set smarttab                                          "指定按一次backspace就删除shiftwidth宽度的空格
"set foldenable "启用折叠
"set foldmethod=indent "indent 折叠方式
"set foldmethod=marker "marker 折叠方式

imap <F7> <C-R>=strftime("%Y_%m_%d-%H:%M")<CR>
" 用空格键来开关折叠
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
" 插入空行
nnoremap tt  o<esc>
nnoremap TT  O<esc>


" 当文件在外部被修改自动更新该文件
set autoread


" 常规模式下输入 cS 清除行尾空格
nmap cS :%s/\s\+$//g<CR>:noh<CR>


" 常规模式下输入 cM 清除行尾 ^M 符号
nmap cM :%s/\r$//g<CR>:noh<CR>


set ignorecase                                        "搜索模式里忽略大小写
set smartcase                                         "如果搜索模式包含大写字符,不使用 'ignorecase' 选项,只有在输入搜索模式并且打开 'ignorecase' 选项时才会使用
" set noincsearch "在输入要搜索文字时,取消实时匹配


" Ctrl + I 替换ESC键进入normal模式
imap <C-I> <Esc>

" Ctrl + K 插入模式下光标向上移动
imap <c-k> <Up>


" Ctrl + J 插入模式下光标向下移动
imap <c-j> <Down>


" Ctrl + H 插入模式下光标向左移动
imap <c-h> <Left>


" Ctrl + L 插入模式下光标向右移动
imap <c-l> <Right>


" 启用每行超过80列的字符提示(字体变蓝并加下划线)
au BufWinEnter * let w:m2=matchadd('Underlined','\%>' . 80 . 'v.\+',-1)


" 分屏窗口光标移动
nmap <C-h> <C-w>h
nmap <C-l> <C-w>l
nmap <C-k> <C-w>k
nmap <C-j> <C-w>j


" 定义快捷键的前缀,即<Leader>
let mapleader=";"
" -----------------------------------------------------------------------------
" < 编程配置 >
" -----------------------------------------------------------------------------
"""""文件标题""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"新建.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: ma6174") 
        call append(line(".")+2,"\# mail: ma6174@163.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: ma6174") 
        call append(line(".")+2," > Mail: ma6174@163.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
    "新建文件后,自动定位到文件末尾
    autocmd BufNewFile * normal G
endfunc 
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" ---------{<([符号自动补全--------------------------------------------------------------------
:inoremap < <><ESC>i
:inoremap > <c-r>=ClosePair('>')<CR>
: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
filetype plugin indent on 


"打开文件类型检测,加了这句才可以用智能补全
set completeopt=longest,menu

set wrap
map j gj
map k gk
" -----------------------------------------------------------------------------
" < 编译、连接、运行配置 >
"------------------------------------------------------------------------------
" F9 一键保存、编译、连接存并运行
map <F9> :call Run()<CR>
imap <F9> <ESC>:call Run()<CR>


" Ctrl + F9 一键保存并编译
map <c-F9> :call Compile()<CR>
imap <c-F9> <ESC>:call Compile()<CR>


" Ctrl + F10 一键保存并连接
map <c-F10> :call Link()<CR>
imap <c-F10> <ESC>:call Link()<CR>


let s:LastShellReturn_C = 0
let s:LastShellReturn_L = 0
let s:ShowWarning = 1
let s:Obj_Extension = '.o'
let s:Exe_Extension = '.exe'
let s:Sou_Error = 0


let s:windows_CFlags = 'gcc\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
let s:linux_CFlags = 'gcc\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'


let s:windows_CPPFlags = 'g++\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
let s:linux_CPPFlags = 'g++\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'


func! Compile()
    exe ":ccl"
    exe ":update"
    if expand("%:e") == "c" || expand("%:e") == "cpp" || expand("%:e") == "cxx"
        let s:Sou_Error = 0
        let s:LastShellReturn_C = 0
        let Sou = expand("%:p")
        let Obj = expand("%:p:r").s:Obj_Extension
        let Obj_Name = expand("%:p:t:r").s:Obj_Extension
        let v:statusmsg = ''
        if !filereadable(Obj) || (filereadable(Obj) && (getftime(Obj) < getftime(Sou)))
            redraw!
            if expand("%:e") == "c"
                if g:iswindows
                    exe ":setlocal makeprg=".s:windows_CFlags
                else
                    exe ":setlocal makeprg=".s:linux_CFlags
                endif
                echohl WarningMsg | echo " compiling..."
                silent make
            elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"
                if g:iswindows
                    exe ":setlocal makeprg=".s:windows_CPPFlags
                else
                    exe ":setlocal makeprg=".s:linux_CPPFlags
                endif
                echohl WarningMsg | echo " compiling..."
                silent make
            endif
            redraw!
            if v:shell_error != 0
                let s:LastShellReturn_C = v:shell_error
            endif
            if g:iswindows
                if s:LastShellReturn_C != 0
                    exe ":bo cope"
                    echohl WarningMsg | echo " compilation Failed"
                else
                    if s:ShowWarning
                        exe ":bo cw"
                    endif
                    echohl WarningMsg | echo " compilation successful"
                endif
            else
                if empty(v:statusmsg)
                    echohl WarningMsg | echo " compilation successful"
                else
                    exe ":bo cope"
                endif
            endif
        else
            echohl WarningMsg | echo ""Obj_Name"is up to date"
        endif
    else
        let s:Sou_Error = 1
        echohl WarningMsg | echo " please choose the correct source file"
    endif
    exe ":setlocal makeprg=make"
endfunc


func! Link()
    call Compile()
    if s:Sou_Error || s:LastShellReturn_C != 0
        return
    endif
    let s:LastShellReturn_L = 0
    let Sou = expand("%:p")
    let Obj = expand("%:p:r").s:Obj_Extension
    if g:iswindows
        let Exe = expand("%:p:r").s:Exe_Extension
        let Exe_Name = expand("%:p:t:r").s:Exe_Extension
    else
        let Exe = expand("%:p:r")
        let Exe_Name = expand("%:p:t:r")
    endif
    let v:statusmsg = ''
if filereadable(Obj) && (getftime(Obj) >= getftime(Sou))
        redraw!
        if !executable(Exe) || (executable(Exe) && getftime(Exe) < getftime(Obj))
            if expand("%:e") == "c"
                setlocal makeprg=gcc\ -o\ %<\ %<.o
                echohl WarningMsg | echo " linking..."
                silent make
            elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"
                setlocal makeprg=g++\ -o\ %<\ %<.o
                echohl WarningMsg | echo " linking..."
                silent make
            endif
            redraw!
            if v:shell_error != 0
                let s:LastShellReturn_L = v:shell_error
            endif
            if g:iswindows
                if s:LastShellReturn_L != 0
                    exe ":bo cope"
                    echohl WarningMsg | echo " linking Failed"
                else
                    if s:ShowWarning
                        exe ":bo cw"
                    endif
                    echohl WarningMsg | echo " linking successful"
                endif
            else
                if empty(v:statusmsg)
                    echohl WarningMsg | echo " linking successful"
                else
                    exe ":bo cope"
                endif
            endif
        else
            echohl WarningMsg | echo ""Exe_Name"is up to date"
        endif
    endif
    setlocal makeprg=make
endfunc


func! Run()
    let s:ShowWarning = 0
    call Link()
    let s:ShowWarning = 1
    if s:Sou_Error || s:LastShellReturn_C != 0 || s:LastShellReturn_L != 0
        return
    endif
    let Sou = expand("%:p")
    let Obj = expand("%:p:r").s:Obj_Extension
    if g:iswindows
        let Exe = expand("%:p:r").s:Exe_Extension
    else
        let Exe = expand("%:p:r")
    endif
    if executable(Exe) && getftime(Exe) >= getftime(Obj) && getftime(Obj) >= getftime(Sou)
        redraw!
        echohl WarningMsg | echo " running..."
        if g:iswindows
            exe ":!%<.exe"
        else
            if g:isGUI
                exe ":!gnome-terminal -e ./%<"
            else
                exe ":!./%<"
            endif
        endif
        redraw!
        echohl WarningMsg | echo " running finish"
    endif
endfunc

" -----------------------------------------------------------------------------
" < txtbrowser 插件配置 >
" 用于文本文件生成标签与与语法高亮(调用TagList插件生成标签,如果可以)
" -----------------------------------------------------------------------------
"au BufRead,BufNewFile *.vm,*.txt setlocal ft=txt

" -----------------------------------------------------------------------------
" <grep插件配置 >
" 在工程中快速查找
" -----------------------------------------------------------------------------
"nnoremap <silent> <F3> :Grep<CR>

" -----------------------------------------------------------------------------
" < TagList 插件配置 >
" 高效地浏览源码,其功能就像vc中的workpace
" 那里面列出了当前文件中的所有宏,全局变量,函数名等
" 常规模式下输入 tl 调用插件,如果有打开 Tagbar 窗口则先将其关闭
" -----------------------------------------------------------------------------
" nmap tl :Tlist<CR>
" let Tlist_Show_One_File=1 "显示当前文件的tags
" let Tlist_Enable_Fold_Column=0 "使taglist插件不显示左边的折叠行
" let Tlist_Exit_OnlyWindow=1 "如果Taglist窗口是最后一个窗口则退出Vim
" let Tlist_File_Fold_Auto_Close=1 "自动折叠
" let Tlist_WinWidth=30 "设置窗口宽度
" let Tlist_Use_Right_Window=1 "在右侧窗口中显示

" -----------------------------------------------------------------------------
" < NERDTree 插件配置 >
" 有目录村结构的文件浏览插件
" 常规模式下输入 F2 调用插件
" -----------------------------------------------------------------------------
let NERDTreeWinPos = "left"        " where NERD tree window is placed on the screen
let NERDTreeWinSize = 25           " size of the NERD tree
nmap <F2> :NERDTreeToggle<CR>
au VimEnter * NERDTree | wincmd l
au WinEnter * call s:CloseIfOnlyNerdTreeLeft()

" Close all open buffers on entering a window if the only
" buffer that's left is the NERDTree buffer
function! s:CloseIfOnlyNerdTreeLeft()
  if exists("t:NERDTreeBufName")
    if bufwinnr(t:NERDTreeBufName) != -1
      if winnr("$") == 1
        q
      endif
    endif
  endif
endfunction

" -----------------------------------------------------------------------------
" < ctags 插件配置 >
" -----------------------------------------------------------------------------
set tags=tags; 
set autochdir

" -----------------------------------------------------------------------------
" < PowerLine 状态栏增强展示插件配置 >
" -----------------------------------------------------------------------------
"vim有一个状态栏 加上powline则有两个状态栏
set laststatus=2
set t_Co=256
let g:Powline_symbols='fancy'

" -----------------------------------------------------------------------------
" < tagbar 状态栏增强展示插件配置 >
" -----------------------------------------------------------------------------
"nmap <Leader>tb :TagbarToggle<CR> "快捷键设置
let g:tagbar_ctags_bin='ctags'          "ctags程序的路径
let g:tagbar_width=30               "窗口宽度的设置
nmap tl :Tagbar<CR>

" -----------------------------------------------------------------------------
" < minibufexpl.vim 状态栏增强展示插件配置 >
" -----------------------------------------------------------------------------
let g:miniBufExplMapWindowNavVim = 1   
let g:miniBufExplMapWindowNavArrows = 1   
let g:miniBufExplMapCTabSwitchBufs = 1   
let g:miniBufExplModSelTarget = 1  
let g:miniBufExplMoreThanOne=0

map <F11> :MBEbp<CR>
map <F12> :MBEbn<CR>

" -----------------------------------------------------------------------------
" < Mark--Karkat(也就是 Mark 插件配置 >
" 给不同的单词高亮,表明不同的变量时很有用,详细帮助见 :h mark.txt
" -----------------------------------------------------------------------------
set viminfo+=!                     " 保存全局变量
let g:mwAutoSaveMarks = 1
let g:mwAutoLoadMarks = 1
nmap <F3> \*

" -----------------------------------------------------------------------------
" < a.vim 插件配置 >
" -----------------------------------------------------------------------------
nnoremap <silent> <F12> :A<CR>
原文链接:https://www.f2er.com/bash/391891.html

猜你在找的Bash相关文章