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

 







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM