vim创建程序文件自动添加头部注释/自动文件头注释与模板定义

Vim 自动文件头注释与模板定义

  • 在vim的配置文件.vimrc添加一些配置可以实现创建新文件时自动添加文件头注释,输入特定命令可以生成模板。

使用方法


  • 插入模式输入模式输入seqlogic[Enter]创建时序逻辑框架
  • 新创建一个文件 gvim test.c 自动添加头部注释
  • F2映射文件头注释,命令行模式文件内按F2自动添加
  • F11映射注释,命令模式按F11出现注释行

Verilog模板生成


vim中输入seqlogic或者comlogic点击回车即可替代为模板

"###################    verilog   ##########################
:ab seqlogic always@(posedge clk or negedge rst_n)<Enter>begin<Enter>if(rst_n==1'b0)<Enter>begin<Enter>end<Enter>else<Enter>begin<Enter>end<Enter>end
"生成时序逻辑框架块

:ab comlogic always@(*)<Enter>begin<Enter>end
"生成组合逻辑框架块
"###################    verilog   ##########################

文件头注释自动生成


"###################    set file head start  #########################
"autocmd创建新文件自动调用setfilehead()函数
autocmd BufNewFile *.v,*.sv,*.cpp,*.c,*.h exec ":call Setfilehead()"
func Setfilehead()
    call append(0, '/***********************************************')
    call append(1, '#')
    call append(2, '#      Filename: '.expand("%"))
    call append(3, '#')
    call append(4, '#        Author: Clough - clough@gmail.com')
    call append(5, '#   Description: ---')
    call append(6, '#        Create: '.strftime("%Y-%m-%d %H:%M:%S"))
    call append(7, '# Last Modified: '.strftime("%Y-%m-%d %H:%M:%S"))
    call append(8, '***********************************************/')
"    call append(9, '')
endfunc

"map F2 to creat file head comment
"映射F2快捷键,生成后跳转至第10行,然后使用o进入vim的插入模式
map <F2> :call Setfilehead()<CR>:10<CR>o
"###################    set file head end   ##########################

文件内部注释快捷键生成


"###################    set comment start  #########################
func SetComment()
    call append(line(".")  , '//**************** comment start ********************')
    call append(line(".")+1, '//**************** comment end   ********************')
endfunc

"映射F11快捷键,生成后跳转至下行,然后使用O进入vim的插入模式
map <F11> :call SetComment()<CR>j<CR>O
"###################    set comment end   ##########################

修改 ~/.vimrc,在文件最后添加以下内容:
" 当新建 .h .c .hpp .cpp .mk .sh等文件时自动调用SetTitle 函数
autocmd BufNewFile *.[ch],*.hpp,*.cpp,Makefile,*.mk,*.sh exec ":call SetTitle()" 
" 加入注释 
func SetComment()
    call setline(1,"/*================================================================") 
    call append(line("."),   "*   Copyright (C) ".strftime("%Y")." Sangfor Ltd. All rights reserved.")
    call append(line(".")+1, "*   ") 
    call append(line(".")+2, "*   文件名称:".expand("%:t")) 
    call append(line(".")+3, "*   创 建 者:LuZhenrong")
    call append(line(".")+4, "*   创建日期:".strftime("%Y年%m月%d日")) 
    call append(line(".")+5, "*   描    述:") 
    call append(line(".")+6, "*")
    call append(line(".")+7, "================================================================*/") 
    call append(line(".")+8, "")
    call append(line(".")+9, "")
endfunc
" 加入shell,Makefile注释
func SetComment_sh()
    call setline(3, "#================================================================") 
    call setline(4, "#   Copyright (C) ".strftime("%Y")." Sangfor Ltd. All rights reserved.")
    call setline(5, "#   ") 
    call setline(6, "#   文件名称:".expand("%:t")) 
    call setline(7, "#   创 建 者:LuZhenrong")
    call setline(8, "#   创建日期:".strftime("%Y年%m月%d日")) 
    call setline(9, "#   描    述:") 
    call setline(10, "#")
    call setline(11, "#================================================================")
    call setline(12, "")
    call setline(13, "")
endfunc 
" 定义函数SetTitle,自动插入文件头 
func SetTitle()
    if &filetype == 'make' 
        call setline(1,"") 
        call setline(2,"")
        call SetComment_sh()
 
    elseif &filetype == 'sh' 
        call setline(1,"#!/system/bin/sh") 
        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
endfunc

 






posted @ 2018-10-19 10:17  CharyGao  阅读(3840)  评论(0编辑  收藏  举报