vim编辑器自动添加注释信息

1.通过编辑.vimrc文件,添加以下内容:
autocmd BufNewFile *.sh exec ":call SetTitle()"

func SetTitle()
  if expand("%:e:r") == 'sh'
    call setline(1,"#!/bin/bash")
    call setline(2,"#########################")
    call setline(3,"# File name:".expand("%"))
    call setline(4,"# Version:v1.0")
    call setline(5,"# Email:admin@test.com")
    call setline(6,"# Created time:".strftime("%F %T"))
    call setline(7,"# Author:xxx")
    call setline(8,"#########################")
    call setline(9,"")
  endif
endfunc
2.比如我vim创建一个11.sh的文件:结果如下
#!/bin/bash
#########################
# File name:11.sh
# Version:v1.0
# Email:admin@test.com
# Created time:2023-12-10 15:38:03
# Author:xxx
#########################

创建新的以 .sh 结尾的 Shell 脚本文件时,自动执行 SetTitle() 函数,该函数会在文件中插入一些基本的脚本注释信息。

---------------------------------------------------------------------------------------------------------------------------------

3.同理还可加入其它后缀的注释,比如.py 

只是要注意相应的语言注释方式可能不同需要小修改一下

autocmd BufNewFile *.py exec ":call SetPythonTitle()"

func SetPythonTitle()
  if expand("%:e:r") == 'py'
    call setline(1,"#!/usr/bin/env python3")
    call setline(2,"#########################")
    call setline(3,"# File name:".expand("%"))
    call setline(4,"# Version:v1.0")
    call setline(5,"# Email:admin@test.com")
    call setline(6,"# Created time:".strftime("%F %T"))
    call setline(7,"# Author:xxx")
    call setline(8,"#########################")
    call setline(9,"")
  endif
endfunc
4.再举例一个C语言的
autocmd BufNewFile *.c exec ":call SetCTitle()"

func SetCTitle()
  if expand("%:e:r") == 'c'
    call setline(1, "/*")
    call setline(2, " * File name: " . expand("%"))
    call setline(3, " * Version: v1.0")
    call setline(4, " * Email: admin@test.com")
    call setline(5, " * Created time: " . strftime("%F %T"))
    call setline(6, " * Author: xxx")
    call setline(7, " */")
    call setline(8, "")
  endif
endfunc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值