vim 学习日志(14) : vimrc 配置,配色

特点:

1、创建代码的时候,自动加版权,作者,创建时间;

2、代码保存的时候,自动修改 Last Modify;

3、对于没有该模板开头的代码,不做任何的修改;

4、vim 打开,显示文件名,下划线的方式显示当前行;

vimrc 位置:~.vimrc

if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
   set fileencodings=ucs-bom,utf-8,utf-16,gbk,big5,gb18030,latin1
   set termencoding=utf-8
   set encoding=utf-8
endif
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set nocompatible    " Use Vim defaults (much better!)
set bs=indent,eol,start     " allow backspacing over everything in insert mode
"set ai         " always set autoindenting on
"set backup     " keep a backup file
set viminfo='20,\"50    " read/write a .viminfo file, don't store more
            " than 50 lines of registers
set history=50      " keep 50 lines of command line history
set ruler       " show the cursor position all the time

" Only do this part when compiled with support for autocommands
if has("autocmd")
  augroup redhat
  autocmd!
  " In text files, always limit the width of text to 78 characters
  autocmd BufRead *.txt set tw=78
  " When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \   exe "normal! g'\"" |
  \ endif
  " don't write swapfile on most commonly used directories for NFS mounts or USB sticks
  autocmd BufNewFile,BufReadPre /media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp
  " start with spec file template
  autocmd BufNewFile *.spec 0r /usr/share/vim/vimfiles/template.spec
  augroup END
endif

if &diff
    hi DiffAdd    cterm=bold ctermfg=12  guibg=LightBlue
    hi DiffDelete cterm=bold ctermfg=13 ctermbg=14  gui=bold guifg=blue guibg=LightCyan
    hi DiffChange cterm=bold ctermbg=green ctermfg=15  guibg=Magenta
    hi DiffText   term=reverse cterm=bold ctermfg=9 gui=bold  guibg=Red
endif

if has("cscope") && filereadable("/usr/bin/cscope")
   set csprg=/usr/bin/cscope
   set csto=0
   set cst
   set nocsverb
   " add any database in current directory
   if filereadable("cscope.out")
      cs add cscope.out
   " else add database pointed to by environment
   elseif $CSCOPE_DB != ""
      cs add $CSCOPE_DB
   endif
   set csverb
endif
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

filetype plugin on

"show file name"


if &term=="xterm"
     set t_Co=8
     set t_Sb=^[[4%dm
     set t_Sf=^[[3%dm
endif



" Don't wake up system with blinking cursor:
" http://www.linuxpowertop.org/known.php
let &guicursor = &guicursor . ",a:blinkon0"
set number
set cursorline
"hi CursorLine   cterm=None ctermbg=lightmagenta  ctermfg=blue
"hi CursorColumn cterm=NONE ctermbg=black ctermfg=blue
syntax on
set softtabstop=4
set tabstop=4
set laststatus=2
set shiftwidth=4
set expandtab
let autosave=5

" file head add signature

autocmd BufNewFile *.conf set filetype=conf
autocmd BufNewFile *.[ch],*.hpp,*.cpp,Makefile,*.mk,*.sh,*.py,*.conf exec ":call SetTitle()"

"autocmd BufNewFile *.conf ":call SetTitle()"

autocmd BufWrite *.[ch],*.hpp,*.cpp,Makefile,*.mk,*.sh,*.py,*.conf exec ":call SetModify()"

func SetComment()
    call setline(1,"/*================================================================")
    call append(line("."),   "*   Copyright (C) ".strftime("%Y"). " ***.cn, All rights reserved.")
    call append(line(".")+1, "*   ")
    call append(line(".")+2, "*   @File Name   : ".expand("%:t"))
    call append(line(".")+3, "*   @Author      : Name@***.cn")
    call append(line(".")+4, "*   @Create Time : ".strftime("%Y-%m-%d %H:%M:%S"))
    call append(line(".")+5, "*   @Last Modify : ".strftime("%Y-%m-%d %H:%M:%S"))
    call append(line(".")+6, "*   @Description : ")
    call append(line(".")+7, "================================================================*/")
    call append(line(".")+8, "")
    call append(line(".")+9, "")
endfunc

func SetComment_sh()
    call setline(3, "#================================================================")
    call setline(4, "#   Copyright (C) ".strftime("%Y"). " ***.cn, All rights reserved.")
    call setline(5, "#   ")
    call setline(6, "#   @File Name   : ".expand("%:t"))
    call setline(7, "#   @Author      :  Name@***.cn")
    call setline(8, "#   @Create Time : ".strftime("%Y-%m-%d %H:%M:%S"))
    call setline(9, "#   @Last Modify : ".strftime("%Y-%m-%d %H:%M:%S"))
    call setline(10,"#   @Description :")
    call setline(11, "#")
    call setline(12, "#================================================================")
    call setline(13, "")
    call setline(14, "")
endfunc

func GETLINENUM()
    let knum = 0
    for EACH in getline(1,20)
        let knum += 1
        let modify=EACH[5:15]
        if modify == "Last Modify"
            return knum
        endif
    endfor
    return 0
endfunc



func SetModify()

     "call setline(17, "# Modify  @File Name   : &LINE:".modify)
     let modi=GETLINENUM()
     if modi != 0
         if &filetype == "h" || &filetype == "c" || &filetype == "cpp"
             "call setline(modi, "#   @Last Modify : ".strftime("%c"))
             call setline(modi, "#   @Last Modify : ".strftime("%Y-%m-%d %H:%M:%S"))
         elseif &filetype == "sh" || &filetype == "python" || &filetype == "make" || &filetype == "conf"
             call setline(modi, "#   @Last Modify : ".strftime("%Y-%m-%d %H:%M:%S"))
         endif
     endif
endfunc


func SetTitle()

    if &filetype == 'make' || &filetype == 'conf'
        let cc=&filetype
        call setline(1,"")
        call setline(2,"")
        call SetComment_sh()

    elseif &filetype == 'sh'
        call setline(1,"#!/system/bin/sh")
        call setline(2,"")
        call SetComment_sh()
    elseif &filetype == "python"
        call setline(1,"#!/usr/bin/python")
        call setline(2,"")
        call SetComment_sh()

    else
         call SetComment()
         if expand("%:e") == 'hpp'
              call append(line(".")+10, "#ifndef _".toupper(expand("%:t:r"))."_H")
              call append(line(".")+11, "#define _".toupper(expand("%:t:r"))."_H")
              call append(line(".")+12, "#ifdef __cplusplus")
              call append(line(".")+13, "extern \"C\"")
              call append(line(".")+14, "{")
              call append(line(".")+15, "#endif")
              call append(line(".")+16, "")
              call append(line(".")+17, "#ifdef __cplusplus")
              call append(line(".")+18, "}")
              call append(line(".")+19, "#endif")
              call append(line(".")+20, "#endif //".toupper(expand("%:t:r"))."_H")

         elseif expand("%:e") == 'h'
              call append(line(".")+10, "#pragma once")
         elseif &filetype == 'c'
              call append(line(".")+10,"#include \"".expand("%:t:r").".h\"")
         elseif &filetype == 'cpp'
              call append(line(".")+10, "#include \"".expand("%:t:r").".h\"")
         endif
    endif
    normal G
endfunc

 

posted on 2024-08-29 18:06  细雨微光  阅读(57)  评论(1编辑  收藏  举报