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