用vim打开一个文件时,自动给文件头部添加注释信息

     给源码文件头部添加注释信息,有很多方法,这里介绍一种在文件打开时,自动给文件添加头部信息的方法,网上很多文章,都只能在文件打开后,通过映射快捷键到指定函数来添加头部信息,本文章中的部分函数,有参考此博客http://www.cnblogs.com/mfryf/p/3643349.html,但具体思路是自己所想。

     具体.vimrc中的配置代码如下:

 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  "打开文件时自动添加标题
  """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  autocmd BufRead *.c,*.cc,*.cpp exec ":call SetTitle()"
  function AddTitle()
      call append(0,"/********************************************************************************")
      call append(1,"** ")
      call append(2,"** Email : \t\t  jiashadidai110@gmail.com")
      call append(3,"** ")
      call append(4,"** Author: \t\t  contestjia")
      call append(5,"** ")
      call append(6,"** Filename: \t  ".expand("%:t"))
      call append(7,"** ")
      call append(8,"** Last modified: ".strftime("%Y-%m-%d %H:%M:%S"))
      call append(9,"** ")
      call append(10,"********************************************************************************/")
      "redraw可以去掉提示信息 Press Enter or type command to continue
      "这个redraw意义不大
      redraw
      if &filetype == 'c'
          call append(11,"#include <stdio.h>")
      endif
      if &filetype == 'cc'
          call append(11,"#include <iostream>")
          call append(11,"#include <stdio.h>")
      endif
      if &filetype == 'cc'
      call append(11,"#include <iostream>")
      endif
      if &filetype == 'cpp'
          call append(11,"#include <iostream>")
      endif
      echohl WarningMsg | echo "Successful in adding the title." | echohl none
  endf
  function UpdateTitle()
          execute '/\*\* *Last modified:/s@:.*$@\=strftime(": %Y-%m-%d %H:%M:%S")@ '
          execute '/\*\* *Filename:/s@:.*$@\=":\t  ".expand("%:t")@ '
          "redraw可以去掉提示信息 Press Enter or type command to continue
          "可以去掉redraw感受一下不同之处
          redraw
         echohl WarningMsg | echo "Successful in updating the title." | echohl None
  endf                                                                                                                                     
  func SetTitle()
      let n = 1
      while n < 10
          let line = getline(n)
          if  line =~'^\*\*\s*\S*Last\smodified:\S*.*$'
              call UpdateTitle()
              return
          endif
          let n = n + 1
      endwhile
      call AddTitle()
      return
  endfunc
同样,如果头部注释信息已经存在,则只会更新当前修改时间,不会重复进行添加。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值