`

使用 vimrc 配置

阅读更多

vi是linux及类unix环境下使用的最多的文本编辑器,而使用vi编写代码几乎是程序员在linux下做的最多的事情。

 

vim的原始环境只是一个黑黑的面板,一切的一切都需要我们手动敲入,包括用来进行代码对其的空格。

 

本文分享一个vim的配置文件,可以为coder初始化一个相对友好的vim开发环境。

帮我们做一些简单的事情,比如代码对齐、tab键转空格、代码高亮、自动注释等,可以明显地减少无谓的手工劳动。

 

代码如下:

set nocompatible
set backspace=indent,eol,start
filetype on
syntax on

set autoindent
set smartindent

set ts=4
set tabstop=4
set expandtab
set shiftwidth=4
set encoding=utf8

set showmatch
set ruler
set incsearch
set nobackup
set fileencoding=utf8
set fileencodings=ucs-bom,gb18030,utf-8,default

filetype plugin indent on
autocmd FileType python setlocal et sta sw=4 sts=4

autocmd BufNewFile *.[ch],*.hpp,*.cpp exec ":call SetTitle()" 
autocmd BufNewFile *.py exec ":call SetCommentPy()" 

func SetCommentPy()
    call setline(1,          "#coding=utf8")
    call append(line("."),   "# ========================================================") 
    call append(line(".")+1, "#   Copyright (C) ".strftime("%Y")." All rights reserved.")
    call append(line(".")+2, "#   ") 
    call append(line(".")+3, "#   filename : ".expand("%:t")) 
    call append(line(".")+4, "#   author   : ***")
    call append(line(".")+5, "#   date     : ".strftime("%Y-%m-%d")) 
    call append(line(".")+6, "#   desc     : ") 
    call append(line(".")+7, "# ======================================================== ")
endfunc



func SetComment()
    call setline(1,          "/* ========================================================") 
    call append(line("."),   " *   Copyright (C) ".strftime("%Y")." All rights reserved.")
    call append(line(".")+1, " *   ") 
    call append(line(".")+2, " *   filename : ".expand("%:t")) 
    call append(line(".")+3, " *   author   : ***")
    call append(line(".")+4, " *   date     : ".strftime("%Y-%m-%d")) 
    call append(line(".")+5, " *   info     : ") 
    call append(line(".")+6, " * ======================================================== */")
    call append(line(".")+7, "")
endfunc

func SetTitle()
    call SetComment()
    if expand("%:e") == 'hpp' 
        call append(line(".")+8, "#ifndef _".toupper(expand("%:t:r"))."_H") 
        call append(line(".")+9, "#define _".toupper(expand("%:t:r"))."_H") 
        call append(line(".")+10, "#ifdef __cplusplus") 
        call append(line(".")+11, "extern \"C\"") 
        call append(line(".")+12, "{") 
        call append(line(".")+13, "#endif") 
        call append(line(".")+14, "") 
        call append(line(".")+15, "#ifdef __cplusplus") 
        call append(line(".")+16, "}") 
        call append(line(".")+17, "#endif") 
        call append(line(".")+18, "#endif //".toupper(expand("%:t:r"))."_H") 
    elseif expand("%:e") == 'h' 
        call append(line(".")+8, "#ifndef _".toupper(expand("%:t:r"))."_H") 
        call append(line(".")+9, "#define _".toupper(expand("%:t:r"))."_H") 
        call append(line(".")+10, "")
        call append(line(".")+11, "")
        call append(line(".")+12, "")
        call append(line(".")+13, "")
        call append(line(".")+14, "#endif //".toupper(expand("%:t:r"))."_H") 
    elseif expand("%:e") == 'c'
        call append(line(".")+8,"#include \"".expand("%:t:r").".h\"") 
    elseif &filetype == 'cpp' 
        call append(line(".")+8, "#include \"".expand("%:t:r").".h\"") 
    endif
endfunc

 

将该代码保存在一个叫“.vimrc”的文件中,放在用户的主目录下即可。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics