Step1 : 在家目录下创建 .vimrc 文件(任意目录均可)
set si
autocmd BufNewFile *.sh exec ":call SeTitle()"
func SeTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"########################################")
call setline(3, "#Author:Maxwell")
call setline(4, "#time:" .strftime("%c"))
call setline(5,"#filename:" .expand("%"))
call setline(6,"#Script description:")
call setline(7,"########################################")
endif
endfunc
[maxwell@MaxwellDBA ~]$ cat .vimrc
set si
autocmd BufNewFile *.sh exec ":call SeTitle()"
func SeTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"########################################")
call setline(3, "#Author:Maxwell")
call setline(4, "#time:" .strftime("%c"))
call setline(5,"#filename:" .expand("%"))
call setline(6,"#Script description:")
call setline(7,"########################################")
endif
endfunc
[maxwell@MaxwellDBA ~]$
创建测试脚本
[maxwell@MaxwellDBA ~]$ vim maxwell_test.sh
[maxwell@MaxwellDBA ~]$ cat maxwell_test.sh
#!/bin/bash
########################################
#Author:Maxwell
#time:Tue 11 Oct 2022 07:06:38 AM CST
#filename:maxwell_test.sh
#Script description:
########################################
[maxwell@MaxwellDBA ~]$
验证脚本的正确性:
[maxwell@MaxwellDBA ~]$ vim maxwell_test.sh
[maxwell@MaxwellDBA ~]$ chmod +x maxwell_test.sh
[maxwell@MaxwellDBA ~]$ cat maxwell_test.sh
#!/bin/bash
########################################
#Author:Maxwell
#time:Tue 11 Oct 2022 07:06:38 AM CST
#filename:maxwell_test.sh
#Script description:
########################################
echo 'This is first shell script That be added comment automatically'
[maxwell@MaxwellDBA ~]$ ./maxwell_test.sh
This is first shell script That be added comment automatically
[maxwell@MaxwellDBA ~]$