vim建立程序文件自動添加頭部註釋/自動文件頭註釋與模板定義

Vim 自動文件頭註釋與模板定義

  • 在vim的配置文件.vimrc添加一些配置能夠實現建立新文件時自動添加文件頭註釋,輸入特定命令能夠生成模板。

使用方法


  • 插入模式輸入模式輸入seqlogic[Enter]建立時序邏輯框架
  • 新建立一個文件 gvim test.c 自動添加頭部註釋
  • F2映射文件頭註釋,命令行模式文件內按F2自動添加
  • F11映射註釋,命令模式按F11出現註釋行

Verilog模板生成


vim中輸入seqlogic或者comlogic點擊回車便可替代爲模板shell

"###################    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
相關文章
相關標籤/搜索